4 * Copyright (C) 2012 VMware, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation version 2 and no later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 #include <linux/vmw_vmci_defs.h>
17 #include <linux/vmw_vmci_api.h>
18 #include <linux/highmem.h>
19 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/pagemap.h>
24 #include <linux/pci.h>
25 #include <linux/sched.h>
26 #include <linux/slab.h>
27 #include <linux/uio.h>
28 #include <linux/wait.h>
29 #include <linux/vmalloc.h>
31 #include "vmci_handle_array.h"
32 #include "vmci_queue_pair.h"
33 #include "vmci_datagram.h"
34 #include "vmci_resource.h"
35 #include "vmci_context.h"
36 #include "vmci_driver.h"
37 #include "vmci_event.h"
38 #include "vmci_route.h"
41 * In the following, we will distinguish between two kinds of VMX processes -
42 * the ones with versions lower than VMCI_VERSION_NOVMVM that use specialized
43 * VMCI page files in the VMX and supporting VM to VM communication and the
44 * newer ones that use the guest memory directly. We will in the following
45 * refer to the older VMX versions as old-style VMX'en, and the newer ones as
48 * The state transition datagram is as follows (the VMCIQPB_ prefix has been
49 * removed for readability) - see below for more details on the transtions:
51 * -------------- NEW -------------
54 * CREATED_NO_MEM <-----------------> CREATED_MEM
56 * | o-----------------------o |
59 * ATTACHED_NO_MEM <----------------> ATTACHED_MEM
61 * | o----------------------o |
64 * SHUTDOWN_NO_MEM <----------------> SHUTDOWN_MEM
67 * -------------> gone <-------------
69 * In more detail. When a VMCI queue pair is first created, it will be in the
70 * VMCIQPB_NEW state. It will then move into one of the following states:
72 * - VMCIQPB_CREATED_NO_MEM: this state indicates that either:
74 * - the created was performed by a host endpoint, in which case there is
75 * no backing memory yet.
77 * - the create was initiated by an old-style VMX, that uses
78 * vmci_qp_broker_set_page_store to specify the UVAs of the queue pair at
79 * a later point in time. This state can be distinguished from the one
80 * above by the context ID of the creator. A host side is not allowed to
81 * attach until the page store has been set.
83 * - VMCIQPB_CREATED_MEM: this state is the result when the queue pair
84 * is created by a VMX using the queue pair device backend that
85 * sets the UVAs of the queue pair immediately and stores the
86 * information for later attachers. At this point, it is ready for
87 * the host side to attach to it.
89 * Once the queue pair is in one of the created states (with the exception of
90 * the case mentioned for older VMX'en above), it is possible to attach to the
91 * queue pair. Again we have two new states possible:
93 * - VMCIQPB_ATTACHED_MEM: this state can be reached through the following
96 * - from VMCIQPB_CREATED_NO_MEM when a new-style VMX allocates a queue
97 * pair, and attaches to a queue pair previously created by the host side.
99 * - from VMCIQPB_CREATED_MEM when the host side attaches to a queue pair
100 * already created by a guest.
102 * - from VMCIQPB_ATTACHED_NO_MEM, when an old-style VMX calls
103 * vmci_qp_broker_set_page_store (see below).
105 * - VMCIQPB_ATTACHED_NO_MEM: If the queue pair already was in the
106 * VMCIQPB_CREATED_NO_MEM due to a host side create, an old-style VMX will
107 * bring the queue pair into this state. Once vmci_qp_broker_set_page_store
108 * is called to register the user memory, the VMCIQPB_ATTACH_MEM state
111 * From the attached queue pair, the queue pair can enter the shutdown states
112 * when either side of the queue pair detaches. If the guest side detaches
113 * first, the queue pair will enter the VMCIQPB_SHUTDOWN_NO_MEM state, where
114 * the content of the queue pair will no longer be available. If the host
115 * side detaches first, the queue pair will either enter the
116 * VMCIQPB_SHUTDOWN_MEM, if the guest memory is currently mapped, or
117 * VMCIQPB_SHUTDOWN_NO_MEM, if the guest memory is not mapped
118 * (e.g., the host detaches while a guest is stunned).
120 * New-style VMX'en will also unmap guest memory, if the guest is
121 * quiesced, e.g., during a snapshot operation. In that case, the guest
122 * memory will no longer be available, and the queue pair will transition from
123 * *_MEM state to a *_NO_MEM state. The VMX may later map the memory once more,
124 * in which case the queue pair will transition from the *_NO_MEM state at that
125 * point back to the *_MEM state. Note that the *_NO_MEM state may have changed,
126 * since the peer may have either attached or detached in the meantime. The
127 * values are laid out such that ++ on a state will move from a *_NO_MEM to a
128 * *_MEM state, and vice versa.
132 * VMCIMemcpy{To,From}QueueFunc() prototypes. Functions of these
133 * types are passed around to enqueue and dequeue routines. Note that
134 * often the functions passed are simply wrappers around memcpy
137 * Note: In order for the memcpy typedefs to be compatible with the VMKernel,
138 * there's an unused last parameter for the hosted side. In
139 * ESX, that parameter holds a buffer type.
141 typedef int vmci_memcpy_to_queue_func(struct vmci_queue
*queue
,
142 u64 queue_offset
, const void *src
,
143 size_t src_offset
, size_t size
);
144 typedef int vmci_memcpy_from_queue_func(void *dest
, size_t dest_offset
,
145 const struct vmci_queue
*queue
,
146 u64 queue_offset
, size_t size
);
148 /* The Kernel specific component of the struct vmci_queue structure. */
149 struct vmci_queue_kern_if
{
150 struct mutex __mutex
; /* Protects the queue. */
151 struct mutex
*mutex
; /* Shared by producer and consumer queues. */
152 size_t num_pages
; /* Number of pages incl. header. */
153 bool host
; /* Host or guest? */
158 } g
; /* Used by the guest. */
161 struct page
**header_page
;
162 } h
; /* Used by the host. */
167 * This structure is opaque to the clients.
170 struct vmci_handle handle
;
171 struct vmci_queue
*produce_q
;
172 struct vmci_queue
*consume_q
;
179 unsigned int blocked
;
180 unsigned int generation
;
181 wait_queue_head_t event
;
184 enum qp_broker_state
{
186 VMCIQPB_CREATED_NO_MEM
,
188 VMCIQPB_ATTACHED_NO_MEM
,
189 VMCIQPB_ATTACHED_MEM
,
190 VMCIQPB_SHUTDOWN_NO_MEM
,
191 VMCIQPB_SHUTDOWN_MEM
,
195 #define QPBROKERSTATE_HAS_MEM(_qpb) (_qpb->state == VMCIQPB_CREATED_MEM || \
196 _qpb->state == VMCIQPB_ATTACHED_MEM || \
197 _qpb->state == VMCIQPB_SHUTDOWN_MEM)
200 * In the queue pair broker, we always use the guest point of view for
201 * the produce and consume queue values and references, e.g., the
202 * produce queue size stored is the guests produce queue size. The
203 * host endpoint will need to swap these around. The only exception is
204 * the local queue pairs on the host, in which case the host endpoint
205 * that creates the queue pair will have the right orientation, and
206 * the attaching host endpoint will need to swap.
209 struct list_head list_item
;
210 struct vmci_handle handle
;
218 struct qp_broker_entry
{
219 struct vmci_resource resource
;
223 enum qp_broker_state state
;
224 bool require_trusted_attach
;
225 bool created_by_trusted
;
226 bool vmci_page_files
; /* Created by VMX using VMCI page files */
227 struct vmci_queue
*produce_q
;
228 struct vmci_queue
*consume_q
;
229 struct vmci_queue_header saved_produce_q
;
230 struct vmci_queue_header saved_consume_q
;
231 vmci_event_release_cb wakeup_cb
;
233 void *local_mem
; /* Kernel memory for local queue pair */
236 struct qp_guest_endpoint
{
237 struct vmci_resource resource
;
242 struct ppn_set ppn_set
;
246 struct list_head head
;
247 struct mutex mutex
; /* Protect queue list. */
250 static struct qp_list qp_broker_list
= {
251 .head
= LIST_HEAD_INIT(qp_broker_list
.head
),
252 .mutex
= __MUTEX_INITIALIZER(qp_broker_list
.mutex
),
255 static struct qp_list qp_guest_endpoints
= {
256 .head
= LIST_HEAD_INIT(qp_guest_endpoints
.head
),
257 .mutex
= __MUTEX_INITIALIZER(qp_guest_endpoints
.mutex
),
260 #define INVALID_VMCI_GUEST_MEM_ID 0
261 #define QPE_NUM_PAGES(_QPE) ((u32) \
262 (DIV_ROUND_UP(_QPE.produce_size, PAGE_SIZE) + \
263 DIV_ROUND_UP(_QPE.consume_size, PAGE_SIZE) + 2))
267 * Frees kernel VA space for a given queue and its queue header, and
268 * frees physical data pages.
270 static void qp_free_queue(void *q
, u64 size
)
272 struct vmci_queue
*queue
= q
;
277 /* Given size does not include header, so add in a page here. */
278 for (i
= 0; i
< DIV_ROUND_UP(size
, PAGE_SIZE
) + 1; i
++) {
279 dma_free_coherent(&vmci_pdev
->dev
, PAGE_SIZE
,
280 queue
->kernel_if
->u
.g
.vas
[i
],
281 queue
->kernel_if
->u
.g
.pas
[i
]);
289 * Allocates kernel queue pages of specified size with IOMMU mappings,
290 * plus space for the queue structure/kernel interface and the queue
293 static void *qp_alloc_queue(u64 size
, u32 flags
)
296 struct vmci_queue
*queue
;
297 const size_t num_pages
= DIV_ROUND_UP(size
, PAGE_SIZE
) + 1;
298 const size_t pas_size
= num_pages
* sizeof(*queue
->kernel_if
->u
.g
.pas
);
299 const size_t vas_size
= num_pages
* sizeof(*queue
->kernel_if
->u
.g
.vas
);
300 const size_t queue_size
=
301 sizeof(*queue
) + sizeof(*queue
->kernel_if
) +
304 queue
= vmalloc(queue_size
);
308 queue
->q_header
= NULL
;
309 queue
->saved_header
= NULL
;
310 queue
->kernel_if
= (struct vmci_queue_kern_if
*)(queue
+ 1);
311 queue
->kernel_if
->mutex
= NULL
;
312 queue
->kernel_if
->num_pages
= num_pages
;
313 queue
->kernel_if
->u
.g
.pas
= (dma_addr_t
*)(queue
->kernel_if
+ 1);
314 queue
->kernel_if
->u
.g
.vas
=
315 (void **)((u8
*)queue
->kernel_if
->u
.g
.pas
+ pas_size
);
316 queue
->kernel_if
->host
= false;
318 for (i
= 0; i
< num_pages
; i
++) {
319 queue
->kernel_if
->u
.g
.vas
[i
] =
320 dma_alloc_coherent(&vmci_pdev
->dev
, PAGE_SIZE
,
321 &queue
->kernel_if
->u
.g
.pas
[i
],
323 if (!queue
->kernel_if
->u
.g
.vas
[i
]) {
324 /* Size excl. the header. */
325 qp_free_queue(queue
, i
* PAGE_SIZE
);
330 /* Queue header is the first page. */
331 queue
->q_header
= queue
->kernel_if
->u
.g
.vas
[0];
337 * Copies from a given buffer or iovector to a VMCI Queue. Uses
338 * kmap()/kunmap() to dynamically map/unmap required portions of the queue
339 * by traversing the offset -> page translation structure for the queue.
340 * Assumes that offset + size does not wrap around in the queue.
342 static int __qp_memcpy_to_queue(struct vmci_queue
*queue
,
348 struct vmci_queue_kern_if
*kernel_if
= queue
->kernel_if
;
349 size_t bytes_copied
= 0;
351 while (bytes_copied
< size
) {
352 const u64 page_index
=
353 (queue_offset
+ bytes_copied
) / PAGE_SIZE
;
354 const size_t page_offset
=
355 (queue_offset
+ bytes_copied
) & (PAGE_SIZE
- 1);
360 va
= kmap(kernel_if
->u
.h
.page
[page_index
]);
362 va
= kernel_if
->u
.g
.vas
[page_index
+ 1];
365 if (size
- bytes_copied
> PAGE_SIZE
- page_offset
)
366 /* Enough payload to fill up from this page. */
367 to_copy
= PAGE_SIZE
- page_offset
;
369 to_copy
= size
- bytes_copied
;
372 struct iovec
*iov
= (struct iovec
*)src
;
375 /* The iovec will track bytes_copied internally. */
376 err
= memcpy_fromiovec((u8
*)va
+ page_offset
,
380 kunmap(kernel_if
->u
.h
.page
[page_index
]);
381 return VMCI_ERROR_INVALID_ARGS
;
384 memcpy((u8
*)va
+ page_offset
,
385 (u8
*)src
+ bytes_copied
, to_copy
);
388 bytes_copied
+= to_copy
;
390 kunmap(kernel_if
->u
.h
.page
[page_index
]);
397 * Copies to a given buffer or iovector from a VMCI Queue. Uses
398 * kmap()/kunmap() to dynamically map/unmap required portions of the queue
399 * by traversing the offset -> page translation structure for the queue.
400 * Assumes that offset + size does not wrap around in the queue.
402 static int __qp_memcpy_from_queue(void *dest
,
403 const struct vmci_queue
*queue
,
408 struct vmci_queue_kern_if
*kernel_if
= queue
->kernel_if
;
409 size_t bytes_copied
= 0;
411 while (bytes_copied
< size
) {
412 const u64 page_index
=
413 (queue_offset
+ bytes_copied
) / PAGE_SIZE
;
414 const size_t page_offset
=
415 (queue_offset
+ bytes_copied
) & (PAGE_SIZE
- 1);
420 va
= kmap(kernel_if
->u
.h
.page
[page_index
]);
422 va
= kernel_if
->u
.g
.vas
[page_index
+ 1];
425 if (size
- bytes_copied
> PAGE_SIZE
- page_offset
)
426 /* Enough payload to fill up this page. */
427 to_copy
= PAGE_SIZE
- page_offset
;
429 to_copy
= size
- bytes_copied
;
432 struct iovec
*iov
= (struct iovec
*)dest
;
435 /* The iovec will track bytes_copied internally. */
436 err
= memcpy_toiovec(iov
, (u8
*)va
+ page_offset
,
440 kunmap(kernel_if
->u
.h
.page
[page_index
]);
441 return VMCI_ERROR_INVALID_ARGS
;
444 memcpy((u8
*)dest
+ bytes_copied
,
445 (u8
*)va
+ page_offset
, to_copy
);
448 bytes_copied
+= to_copy
;
450 kunmap(kernel_if
->u
.h
.page
[page_index
]);
457 * Allocates two list of PPNs --- one for the pages in the produce queue,
458 * and the other for the pages in the consume queue. Intializes the list
459 * of PPNs with the page frame numbers of the KVA for the two queues (and
460 * the queue headers).
462 static int qp_alloc_ppn_set(void *prod_q
,
463 u64 num_produce_pages
,
465 u64 num_consume_pages
, struct ppn_set
*ppn_set
)
469 struct vmci_queue
*produce_q
= prod_q
;
470 struct vmci_queue
*consume_q
= cons_q
;
473 if (!produce_q
|| !num_produce_pages
|| !consume_q
||
474 !num_consume_pages
|| !ppn_set
)
475 return VMCI_ERROR_INVALID_ARGS
;
477 if (ppn_set
->initialized
)
478 return VMCI_ERROR_ALREADY_EXISTS
;
481 kmalloc(num_produce_pages
* sizeof(*produce_ppns
), GFP_KERNEL
);
483 return VMCI_ERROR_NO_MEM
;
486 kmalloc(num_consume_pages
* sizeof(*consume_ppns
), GFP_KERNEL
);
489 return VMCI_ERROR_NO_MEM
;
492 for (i
= 0; i
< num_produce_pages
; i
++) {
496 produce_q
->kernel_if
->u
.g
.pas
[i
] >> PAGE_SHIFT
;
497 pfn
= produce_ppns
[i
];
499 /* Fail allocation if PFN isn't supported by hypervisor. */
500 if (sizeof(pfn
) > sizeof(*produce_ppns
)
501 && pfn
!= produce_ppns
[i
])
505 for (i
= 0; i
< num_consume_pages
; i
++) {
509 consume_q
->kernel_if
->u
.g
.pas
[i
] >> PAGE_SHIFT
;
510 pfn
= consume_ppns
[i
];
512 /* Fail allocation if PFN isn't supported by hypervisor. */
513 if (sizeof(pfn
) > sizeof(*consume_ppns
)
514 && pfn
!= consume_ppns
[i
])
518 ppn_set
->num_produce_pages
= num_produce_pages
;
519 ppn_set
->num_consume_pages
= num_consume_pages
;
520 ppn_set
->produce_ppns
= produce_ppns
;
521 ppn_set
->consume_ppns
= consume_ppns
;
522 ppn_set
->initialized
= true;
528 return VMCI_ERROR_INVALID_ARGS
;
532 * Frees the two list of PPNs for a queue pair.
534 static void qp_free_ppn_set(struct ppn_set
*ppn_set
)
536 if (ppn_set
->initialized
) {
537 /* Do not call these functions on NULL inputs. */
538 kfree(ppn_set
->produce_ppns
);
539 kfree(ppn_set
->consume_ppns
);
541 memset(ppn_set
, 0, sizeof(*ppn_set
));
545 * Populates the list of PPNs in the hypercall structure with the PPNS
546 * of the produce queue and the consume queue.
548 static int qp_populate_ppn_set(u8
*call_buf
, const struct ppn_set
*ppn_set
)
550 memcpy(call_buf
, ppn_set
->produce_ppns
,
551 ppn_set
->num_produce_pages
* sizeof(*ppn_set
->produce_ppns
));
553 ppn_set
->num_produce_pages
* sizeof(*ppn_set
->produce_ppns
),
554 ppn_set
->consume_ppns
,
555 ppn_set
->num_consume_pages
* sizeof(*ppn_set
->consume_ppns
));
560 static int qp_memcpy_to_queue(struct vmci_queue
*queue
,
562 const void *src
, size_t src_offset
, size_t size
)
564 return __qp_memcpy_to_queue(queue
, queue_offset
,
565 (u8
*)src
+ src_offset
, size
, false);
568 static int qp_memcpy_from_queue(void *dest
,
570 const struct vmci_queue
*queue
,
571 u64 queue_offset
, size_t size
)
573 return __qp_memcpy_from_queue((u8
*)dest
+ dest_offset
,
574 queue
, queue_offset
, size
, false);
578 * Copies from a given iovec from a VMCI Queue.
580 static int qp_memcpy_to_queue_iov(struct vmci_queue
*queue
,
583 size_t src_offset
, size_t size
)
587 * We ignore src_offset because src is really a struct iovec * and will
588 * maintain offset internally.
590 return __qp_memcpy_to_queue(queue
, queue_offset
, src
, size
, true);
594 * Copies to a given iovec from a VMCI Queue.
596 static int qp_memcpy_from_queue_iov(void *dest
,
598 const struct vmci_queue
*queue
,
599 u64 queue_offset
, size_t size
)
602 * We ignore dest_offset because dest is really a struct iovec * and
603 * will maintain offset internally.
605 return __qp_memcpy_from_queue(dest
, queue
, queue_offset
, size
, true);
609 * Allocates kernel VA space of specified size plus space for the queue
610 * and kernel interface. This is different from the guest queue allocator,
611 * because we do not allocate our own queue header/data pages here but
612 * share those of the guest.
614 static struct vmci_queue
*qp_host_alloc_queue(u64 size
)
616 struct vmci_queue
*queue
;
617 const size_t num_pages
= DIV_ROUND_UP(size
, PAGE_SIZE
) + 1;
618 const size_t queue_size
= sizeof(*queue
) + sizeof(*(queue
->kernel_if
));
619 const size_t queue_page_size
=
620 num_pages
* sizeof(*queue
->kernel_if
->u
.h
.page
);
622 queue
= kzalloc(queue_size
+ queue_page_size
, GFP_KERNEL
);
624 queue
->q_header
= NULL
;
625 queue
->saved_header
= NULL
;
626 queue
->kernel_if
= (struct vmci_queue_kern_if
*)(queue
+ 1);
627 queue
->kernel_if
->host
= true;
628 queue
->kernel_if
->mutex
= NULL
;
629 queue
->kernel_if
->num_pages
= num_pages
;
630 queue
->kernel_if
->u
.h
.header_page
=
631 (struct page
**)((u8
*)queue
+ queue_size
);
632 queue
->kernel_if
->u
.h
.page
=
633 &queue
->kernel_if
->u
.h
.header_page
[1];
640 * Frees kernel memory for a given queue (header plus translation
643 static void qp_host_free_queue(struct vmci_queue
*queue
, u64 queue_size
)
649 * Initialize the mutex for the pair of queues. This mutex is used to
650 * protect the q_header and the buffer from changing out from under any
651 * users of either queue. Of course, it's only any good if the mutexes
652 * are actually acquired. Queue structure must lie on non-paged memory
653 * or we cannot guarantee access to the mutex.
655 static void qp_init_queue_mutex(struct vmci_queue
*produce_q
,
656 struct vmci_queue
*consume_q
)
659 * Only the host queue has shared state - the guest queues do not
660 * need to synchronize access using a queue mutex.
663 if (produce_q
->kernel_if
->host
) {
664 produce_q
->kernel_if
->mutex
= &produce_q
->kernel_if
->__mutex
;
665 consume_q
->kernel_if
->mutex
= &produce_q
->kernel_if
->__mutex
;
666 mutex_init(produce_q
->kernel_if
->mutex
);
671 * Cleans up the mutex for the pair of queues.
673 static void qp_cleanup_queue_mutex(struct vmci_queue
*produce_q
,
674 struct vmci_queue
*consume_q
)
676 if (produce_q
->kernel_if
->host
) {
677 produce_q
->kernel_if
->mutex
= NULL
;
678 consume_q
->kernel_if
->mutex
= NULL
;
683 * Acquire the mutex for the queue. Note that the produce_q and
684 * the consume_q share a mutex. So, only one of the two need to
685 * be passed in to this routine. Either will work just fine.
687 static void qp_acquire_queue_mutex(struct vmci_queue
*queue
)
689 if (queue
->kernel_if
->host
)
690 mutex_lock(queue
->kernel_if
->mutex
);
694 * Release the mutex for the queue. Note that the produce_q and
695 * the consume_q share a mutex. So, only one of the two need to
696 * be passed in to this routine. Either will work just fine.
698 static void qp_release_queue_mutex(struct vmci_queue
*queue
)
700 if (queue
->kernel_if
->host
)
701 mutex_unlock(queue
->kernel_if
->mutex
);
705 * Helper function to release pages in the PageStoreAttachInfo
706 * previously obtained using get_user_pages.
708 static void qp_release_pages(struct page
**pages
,
709 u64 num_pages
, bool dirty
)
713 for (i
= 0; i
< num_pages
; i
++) {
715 set_page_dirty(pages
[i
]);
717 page_cache_release(pages
[i
]);
723 * Lock the user pages referenced by the {produce,consume}Buffer
724 * struct into memory and populate the {produce,consume}Pages
725 * arrays in the attach structure with them.
727 static int qp_host_get_user_memory(u64 produce_uva
,
729 struct vmci_queue
*produce_q
,
730 struct vmci_queue
*consume_q
)
733 int err
= VMCI_SUCCESS
;
735 down_write(¤t
->mm
->mmap_sem
);
736 retval
= get_user_pages(current
,
738 (uintptr_t) produce_uva
,
739 produce_q
->kernel_if
->num_pages
,
741 produce_q
->kernel_if
->u
.h
.header_page
, NULL
);
742 if (retval
< produce_q
->kernel_if
->num_pages
) {
743 pr_warn("get_user_pages(produce) failed (retval=%d)", retval
);
744 qp_release_pages(produce_q
->kernel_if
->u
.h
.header_page
,
746 err
= VMCI_ERROR_NO_MEM
;
750 retval
= get_user_pages(current
,
752 (uintptr_t) consume_uva
,
753 consume_q
->kernel_if
->num_pages
,
755 consume_q
->kernel_if
->u
.h
.header_page
, NULL
);
756 if (retval
< consume_q
->kernel_if
->num_pages
) {
757 pr_warn("get_user_pages(consume) failed (retval=%d)", retval
);
758 qp_release_pages(consume_q
->kernel_if
->u
.h
.header_page
,
760 qp_release_pages(produce_q
->kernel_if
->u
.h
.header_page
,
761 produce_q
->kernel_if
->num_pages
, false);
762 err
= VMCI_ERROR_NO_MEM
;
766 up_write(¤t
->mm
->mmap_sem
);
772 * Registers the specification of the user pages used for backing a queue
773 * pair. Enough information to map in pages is stored in the OS specific
774 * part of the struct vmci_queue structure.
776 static int qp_host_register_user_memory(struct vmci_qp_page_store
*page_store
,
777 struct vmci_queue
*produce_q
,
778 struct vmci_queue
*consume_q
)
784 * The new style and the old style mapping only differs in
785 * that we either get a single or two UVAs, so we split the
786 * single UVA range at the appropriate spot.
788 produce_uva
= page_store
->pages
;
789 consume_uva
= page_store
->pages
+
790 produce_q
->kernel_if
->num_pages
* PAGE_SIZE
;
791 return qp_host_get_user_memory(produce_uva
, consume_uva
, produce_q
,
796 * Releases and removes the references to user pages stored in the attach
797 * struct. Pages are released from the page cache and may become
800 static void qp_host_unregister_user_memory(struct vmci_queue
*produce_q
,
801 struct vmci_queue
*consume_q
)
803 qp_release_pages(produce_q
->kernel_if
->u
.h
.header_page
,
804 produce_q
->kernel_if
->num_pages
, true);
805 memset(produce_q
->kernel_if
->u
.h
.header_page
, 0,
806 sizeof(*produce_q
->kernel_if
->u
.h
.header_page
) *
807 produce_q
->kernel_if
->num_pages
);
808 qp_release_pages(consume_q
->kernel_if
->u
.h
.header_page
,
809 consume_q
->kernel_if
->num_pages
, true);
810 memset(consume_q
->kernel_if
->u
.h
.header_page
, 0,
811 sizeof(*consume_q
->kernel_if
->u
.h
.header_page
) *
812 consume_q
->kernel_if
->num_pages
);
816 * Once qp_host_register_user_memory has been performed on a
817 * queue, the queue pair headers can be mapped into the
818 * kernel. Once mapped, they must be unmapped with
819 * qp_host_unmap_queues prior to calling
820 * qp_host_unregister_user_memory.
823 static int qp_host_map_queues(struct vmci_queue
*produce_q
,
824 struct vmci_queue
*consume_q
)
828 if (!produce_q
->q_header
|| !consume_q
->q_header
) {
829 struct page
*headers
[2];
831 if (produce_q
->q_header
!= consume_q
->q_header
)
832 return VMCI_ERROR_QUEUEPAIR_MISMATCH
;
834 if (produce_q
->kernel_if
->u
.h
.header_page
== NULL
||
835 *produce_q
->kernel_if
->u
.h
.header_page
== NULL
)
836 return VMCI_ERROR_UNAVAILABLE
;
838 headers
[0] = *produce_q
->kernel_if
->u
.h
.header_page
;
839 headers
[1] = *consume_q
->kernel_if
->u
.h
.header_page
;
841 produce_q
->q_header
= vmap(headers
, 2, VM_MAP
, PAGE_KERNEL
);
842 if (produce_q
->q_header
!= NULL
) {
843 consume_q
->q_header
=
844 (struct vmci_queue_header
*)((u8
*)
845 produce_q
->q_header
+
847 result
= VMCI_SUCCESS
;
849 pr_warn("vmap failed\n");
850 result
= VMCI_ERROR_NO_MEM
;
853 result
= VMCI_SUCCESS
;
860 * Unmaps previously mapped queue pair headers from the kernel.
861 * Pages are unpinned.
863 static int qp_host_unmap_queues(u32 gid
,
864 struct vmci_queue
*produce_q
,
865 struct vmci_queue
*consume_q
)
867 if (produce_q
->q_header
) {
868 if (produce_q
->q_header
< consume_q
->q_header
)
869 vunmap(produce_q
->q_header
);
871 vunmap(consume_q
->q_header
);
873 produce_q
->q_header
= NULL
;
874 consume_q
->q_header
= NULL
;
881 * Finds the entry in the list corresponding to a given handle. Assumes
882 * that the list is locked.
884 static struct qp_entry
*qp_list_find(struct qp_list
*qp_list
,
885 struct vmci_handle handle
)
887 struct qp_entry
*entry
;
889 if (vmci_handle_is_invalid(handle
))
892 list_for_each_entry(entry
, &qp_list
->head
, list_item
) {
893 if (vmci_handle_is_equal(entry
->handle
, handle
))
901 * Finds the entry in the list corresponding to a given handle.
903 static struct qp_guest_endpoint
*
904 qp_guest_handle_to_entry(struct vmci_handle handle
)
906 struct qp_guest_endpoint
*entry
;
907 struct qp_entry
*qp
= qp_list_find(&qp_guest_endpoints
, handle
);
909 entry
= qp
? container_of(
910 qp
, struct qp_guest_endpoint
, qp
) : NULL
;
915 * Finds the entry in the list corresponding to a given handle.
917 static struct qp_broker_entry
*
918 qp_broker_handle_to_entry(struct vmci_handle handle
)
920 struct qp_broker_entry
*entry
;
921 struct qp_entry
*qp
= qp_list_find(&qp_broker_list
, handle
);
923 entry
= qp
? container_of(
924 qp
, struct qp_broker_entry
, qp
) : NULL
;
929 * Dispatches a queue pair event message directly into the local event
932 static int qp_notify_peer_local(bool attach
, struct vmci_handle handle
)
934 u32 context_id
= vmci_get_context_id();
935 struct vmci_event_qp ev
;
937 ev
.msg
.hdr
.dst
= vmci_make_handle(context_id
, VMCI_EVENT_HANDLER
);
938 ev
.msg
.hdr
.src
= vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID
,
939 VMCI_CONTEXT_RESOURCE_ID
);
940 ev
.msg
.hdr
.payload_size
= sizeof(ev
) - sizeof(ev
.msg
.hdr
);
941 ev
.msg
.event_data
.event
=
942 attach
? VMCI_EVENT_QP_PEER_ATTACH
: VMCI_EVENT_QP_PEER_DETACH
;
943 ev
.payload
.peer_id
= context_id
;
944 ev
.payload
.handle
= handle
;
946 return vmci_event_dispatch(&ev
.msg
.hdr
);
950 * Allocates and initializes a qp_guest_endpoint structure.
951 * Allocates a queue_pair rid (and handle) iff the given entry has
952 * an invalid handle. 0 through VMCI_RESERVED_RESOURCE_ID_MAX
953 * are reserved handles. Assumes that the QP list mutex is held
956 static struct qp_guest_endpoint
*
957 qp_guest_endpoint_create(struct vmci_handle handle
,
966 struct qp_guest_endpoint
*entry
;
967 /* One page each for the queue headers. */
968 const u64 num_ppns
= DIV_ROUND_UP(produce_size
, PAGE_SIZE
) +
969 DIV_ROUND_UP(consume_size
, PAGE_SIZE
) + 2;
971 if (vmci_handle_is_invalid(handle
)) {
972 u32 context_id
= vmci_get_context_id();
974 handle
= vmci_make_handle(context_id
, VMCI_INVALID_ID
);
977 entry
= kzalloc(sizeof(*entry
), GFP_KERNEL
);
979 entry
->qp
.peer
= peer
;
980 entry
->qp
.flags
= flags
;
981 entry
->qp
.produce_size
= produce_size
;
982 entry
->qp
.consume_size
= consume_size
;
983 entry
->qp
.ref_count
= 0;
984 entry
->num_ppns
= num_ppns
;
985 entry
->produce_q
= produce_q
;
986 entry
->consume_q
= consume_q
;
987 INIT_LIST_HEAD(&entry
->qp
.list_item
);
989 /* Add resource obj */
990 result
= vmci_resource_add(&entry
->resource
,
991 VMCI_RESOURCE_TYPE_QPAIR_GUEST
,
993 entry
->qp
.handle
= vmci_resource_handle(&entry
->resource
);
994 if ((result
!= VMCI_SUCCESS
) ||
995 qp_list_find(&qp_guest_endpoints
, entry
->qp
.handle
)) {
996 pr_warn("Failed to add new resource (handle=0x%x:0x%x), error: %d",
997 handle
.context
, handle
.resource
, result
);
1006 * Frees a qp_guest_endpoint structure.
1008 static void qp_guest_endpoint_destroy(struct qp_guest_endpoint
*entry
)
1010 qp_free_ppn_set(&entry
->ppn_set
);
1011 qp_cleanup_queue_mutex(entry
->produce_q
, entry
->consume_q
);
1012 qp_free_queue(entry
->produce_q
, entry
->qp
.produce_size
);
1013 qp_free_queue(entry
->consume_q
, entry
->qp
.consume_size
);
1014 /* Unlink from resource hash table and free callback */
1015 vmci_resource_remove(&entry
->resource
);
1021 * Helper to make a queue_pairAlloc hypercall when the driver is
1022 * supporting a guest device.
1024 static int qp_alloc_hypercall(const struct qp_guest_endpoint
*entry
)
1026 struct vmci_qp_alloc_msg
*alloc_msg
;
1030 if (!entry
|| entry
->num_ppns
<= 2)
1031 return VMCI_ERROR_INVALID_ARGS
;
1033 msg_size
= sizeof(*alloc_msg
) +
1034 (size_t) entry
->num_ppns
* sizeof(u32
);
1035 alloc_msg
= kmalloc(msg_size
, GFP_KERNEL
);
1037 return VMCI_ERROR_NO_MEM
;
1039 alloc_msg
->hdr
.dst
= vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID
,
1040 VMCI_QUEUEPAIR_ALLOC
);
1041 alloc_msg
->hdr
.src
= VMCI_ANON_SRC_HANDLE
;
1042 alloc_msg
->hdr
.payload_size
= msg_size
- VMCI_DG_HEADERSIZE
;
1043 alloc_msg
->handle
= entry
->qp
.handle
;
1044 alloc_msg
->peer
= entry
->qp
.peer
;
1045 alloc_msg
->flags
= entry
->qp
.flags
;
1046 alloc_msg
->produce_size
= entry
->qp
.produce_size
;
1047 alloc_msg
->consume_size
= entry
->qp
.consume_size
;
1048 alloc_msg
->num_ppns
= entry
->num_ppns
;
1050 result
= qp_populate_ppn_set((u8
*)alloc_msg
+ sizeof(*alloc_msg
),
1052 if (result
== VMCI_SUCCESS
)
1053 result
= vmci_send_datagram(&alloc_msg
->hdr
);
1061 * Helper to make a queue_pairDetach hypercall when the driver is
1062 * supporting a guest device.
1064 static int qp_detatch_hypercall(struct vmci_handle handle
)
1066 struct vmci_qp_detach_msg detach_msg
;
1068 detach_msg
.hdr
.dst
= vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID
,
1069 VMCI_QUEUEPAIR_DETACH
);
1070 detach_msg
.hdr
.src
= VMCI_ANON_SRC_HANDLE
;
1071 detach_msg
.hdr
.payload_size
= sizeof(handle
);
1072 detach_msg
.handle
= handle
;
1074 return vmci_send_datagram(&detach_msg
.hdr
);
1078 * Adds the given entry to the list. Assumes that the list is locked.
1080 static void qp_list_add_entry(struct qp_list
*qp_list
, struct qp_entry
*entry
)
1083 list_add(&entry
->list_item
, &qp_list
->head
);
1087 * Removes the given entry from the list. Assumes that the list is locked.
1089 static void qp_list_remove_entry(struct qp_list
*qp_list
,
1090 struct qp_entry
*entry
)
1093 list_del(&entry
->list_item
);
1097 * Helper for VMCI queue_pair detach interface. Frees the physical
1098 * pages for the queue pair.
1100 static int qp_detatch_guest_work(struct vmci_handle handle
)
1103 struct qp_guest_endpoint
*entry
;
1104 u32 ref_count
= ~0; /* To avoid compiler warning below */
1106 mutex_lock(&qp_guest_endpoints
.mutex
);
1108 entry
= qp_guest_handle_to_entry(handle
);
1110 mutex_unlock(&qp_guest_endpoints
.mutex
);
1111 return VMCI_ERROR_NOT_FOUND
;
1114 if (entry
->qp
.flags
& VMCI_QPFLAG_LOCAL
) {
1115 result
= VMCI_SUCCESS
;
1117 if (entry
->qp
.ref_count
> 1) {
1118 result
= qp_notify_peer_local(false, handle
);
1120 * We can fail to notify a local queuepair
1121 * because we can't allocate. We still want
1122 * to release the entry if that happens, so
1123 * don't bail out yet.
1127 result
= qp_detatch_hypercall(handle
);
1128 if (result
< VMCI_SUCCESS
) {
1130 * We failed to notify a non-local queuepair.
1131 * That other queuepair might still be
1132 * accessing the shared memory, so don't
1133 * release the entry yet. It will get cleaned
1134 * up by VMCIqueue_pair_Exit() if necessary
1135 * (assuming we are going away, otherwise why
1139 mutex_unlock(&qp_guest_endpoints
.mutex
);
1145 * If we get here then we either failed to notify a local queuepair, or
1146 * we succeeded in all cases. Release the entry if required.
1149 entry
->qp
.ref_count
--;
1150 if (entry
->qp
.ref_count
== 0)
1151 qp_list_remove_entry(&qp_guest_endpoints
, &entry
->qp
);
1153 /* If we didn't remove the entry, this could change once we unlock. */
1155 ref_count
= entry
->qp
.ref_count
;
1157 mutex_unlock(&qp_guest_endpoints
.mutex
);
1160 qp_guest_endpoint_destroy(entry
);
1166 * This functions handles the actual allocation of a VMCI queue
1167 * pair guest endpoint. Allocates physical pages for the queue
1168 * pair. It makes OS dependent calls through generic wrappers.
1170 static int qp_alloc_guest_work(struct vmci_handle
*handle
,
1171 struct vmci_queue
**produce_q
,
1173 struct vmci_queue
**consume_q
,
1179 const u64 num_produce_pages
=
1180 DIV_ROUND_UP(produce_size
, PAGE_SIZE
) + 1;
1181 const u64 num_consume_pages
=
1182 DIV_ROUND_UP(consume_size
, PAGE_SIZE
) + 1;
1183 void *my_produce_q
= NULL
;
1184 void *my_consume_q
= NULL
;
1186 struct qp_guest_endpoint
*queue_pair_entry
= NULL
;
1188 if (priv_flags
!= VMCI_NO_PRIVILEGE_FLAGS
)
1189 return VMCI_ERROR_NO_ACCESS
;
1191 mutex_lock(&qp_guest_endpoints
.mutex
);
1193 queue_pair_entry
= qp_guest_handle_to_entry(*handle
);
1194 if (queue_pair_entry
) {
1195 if (queue_pair_entry
->qp
.flags
& VMCI_QPFLAG_LOCAL
) {
1196 /* Local attach case. */
1197 if (queue_pair_entry
->qp
.ref_count
> 1) {
1198 pr_devel("Error attempting to attach more than once\n");
1199 result
= VMCI_ERROR_UNAVAILABLE
;
1200 goto error_keep_entry
;
1203 if (queue_pair_entry
->qp
.produce_size
!= consume_size
||
1204 queue_pair_entry
->qp
.consume_size
!=
1206 queue_pair_entry
->qp
.flags
!=
1207 (flags
& ~VMCI_QPFLAG_ATTACH_ONLY
)) {
1208 pr_devel("Error mismatched queue pair in local attach\n");
1209 result
= VMCI_ERROR_QUEUEPAIR_MISMATCH
;
1210 goto error_keep_entry
;
1214 * Do a local attach. We swap the consume and
1215 * produce queues for the attacher and deliver
1218 result
= qp_notify_peer_local(true, *handle
);
1219 if (result
< VMCI_SUCCESS
)
1220 goto error_keep_entry
;
1222 my_produce_q
= queue_pair_entry
->consume_q
;
1223 my_consume_q
= queue_pair_entry
->produce_q
;
1227 result
= VMCI_ERROR_ALREADY_EXISTS
;
1228 goto error_keep_entry
;
1231 my_produce_q
= qp_alloc_queue(produce_size
, flags
);
1232 if (!my_produce_q
) {
1233 pr_warn("Error allocating pages for produce queue\n");
1234 result
= VMCI_ERROR_NO_MEM
;
1238 my_consume_q
= qp_alloc_queue(consume_size
, flags
);
1239 if (!my_consume_q
) {
1240 pr_warn("Error allocating pages for consume queue\n");
1241 result
= VMCI_ERROR_NO_MEM
;
1245 queue_pair_entry
= qp_guest_endpoint_create(*handle
, peer
, flags
,
1246 produce_size
, consume_size
,
1247 my_produce_q
, my_consume_q
);
1248 if (!queue_pair_entry
) {
1249 pr_warn("Error allocating memory in %s\n", __func__
);
1250 result
= VMCI_ERROR_NO_MEM
;
1254 result
= qp_alloc_ppn_set(my_produce_q
, num_produce_pages
, my_consume_q
,
1256 &queue_pair_entry
->ppn_set
);
1257 if (result
< VMCI_SUCCESS
) {
1258 pr_warn("qp_alloc_ppn_set failed\n");
1263 * It's only necessary to notify the host if this queue pair will be
1264 * attached to from another context.
1266 if (queue_pair_entry
->qp
.flags
& VMCI_QPFLAG_LOCAL
) {
1267 /* Local create case. */
1268 u32 context_id
= vmci_get_context_id();
1271 * Enforce similar checks on local queue pairs as we
1272 * do for regular ones. The handle's context must
1273 * match the creator or attacher context id (here they
1274 * are both the current context id) and the
1275 * attach-only flag cannot exist during create. We
1276 * also ensure specified peer is this context or an
1279 if (queue_pair_entry
->qp
.handle
.context
!= context_id
||
1280 (queue_pair_entry
->qp
.peer
!= VMCI_INVALID_ID
&&
1281 queue_pair_entry
->qp
.peer
!= context_id
)) {
1282 result
= VMCI_ERROR_NO_ACCESS
;
1286 if (queue_pair_entry
->qp
.flags
& VMCI_QPFLAG_ATTACH_ONLY
) {
1287 result
= VMCI_ERROR_NOT_FOUND
;
1291 result
= qp_alloc_hypercall(queue_pair_entry
);
1292 if (result
< VMCI_SUCCESS
) {
1293 pr_warn("qp_alloc_hypercall result = %d\n", result
);
1298 qp_init_queue_mutex((struct vmci_queue
*)my_produce_q
,
1299 (struct vmci_queue
*)my_consume_q
);
1301 qp_list_add_entry(&qp_guest_endpoints
, &queue_pair_entry
->qp
);
1304 queue_pair_entry
->qp
.ref_count
++;
1305 *handle
= queue_pair_entry
->qp
.handle
;
1306 *produce_q
= (struct vmci_queue
*)my_produce_q
;
1307 *consume_q
= (struct vmci_queue
*)my_consume_q
;
1310 * We should initialize the queue pair header pages on a local
1311 * queue pair create. For non-local queue pairs, the
1312 * hypervisor initializes the header pages in the create step.
1314 if ((queue_pair_entry
->qp
.flags
& VMCI_QPFLAG_LOCAL
) &&
1315 queue_pair_entry
->qp
.ref_count
== 1) {
1316 vmci_q_header_init((*produce_q
)->q_header
, *handle
);
1317 vmci_q_header_init((*consume_q
)->q_header
, *handle
);
1320 mutex_unlock(&qp_guest_endpoints
.mutex
);
1322 return VMCI_SUCCESS
;
1325 mutex_unlock(&qp_guest_endpoints
.mutex
);
1326 if (queue_pair_entry
) {
1327 /* The queues will be freed inside the destroy routine. */
1328 qp_guest_endpoint_destroy(queue_pair_entry
);
1330 qp_free_queue(my_produce_q
, produce_size
);
1331 qp_free_queue(my_consume_q
, consume_size
);
1336 /* This path should only be used when an existing entry was found. */
1337 mutex_unlock(&qp_guest_endpoints
.mutex
);
1342 * The first endpoint issuing a queue pair allocation will create the state
1343 * of the queue pair in the queue pair broker.
1345 * If the creator is a guest, it will associate a VMX virtual address range
1346 * with the queue pair as specified by the page_store. For compatibility with
1347 * older VMX'en, that would use a separate step to set the VMX virtual
1348 * address range, the virtual address range can be registered later using
1349 * vmci_qp_broker_set_page_store. In that case, a page_store of NULL should be
1352 * If the creator is the host, a page_store of NULL should be used as well,
1353 * since the host is not able to supply a page store for the queue pair.
1355 * For older VMX and host callers, the queue pair will be created in the
1356 * VMCIQPB_CREATED_NO_MEM state, and for current VMX callers, it will be
1357 * created in VMCOQPB_CREATED_MEM state.
1359 static int qp_broker_create(struct vmci_handle handle
,
1365 struct vmci_qp_page_store
*page_store
,
1366 struct vmci_ctx
*context
,
1367 vmci_event_release_cb wakeup_cb
,
1368 void *client_data
, struct qp_broker_entry
**ent
)
1370 struct qp_broker_entry
*entry
= NULL
;
1371 const u32 context_id
= vmci_ctx_get_id(context
);
1372 bool is_local
= flags
& VMCI_QPFLAG_LOCAL
;
1374 u64 guest_produce_size
;
1375 u64 guest_consume_size
;
1377 /* Do not create if the caller asked not to. */
1378 if (flags
& VMCI_QPFLAG_ATTACH_ONLY
)
1379 return VMCI_ERROR_NOT_FOUND
;
1382 * Creator's context ID should match handle's context ID or the creator
1383 * must allow the context in handle's context ID as the "peer".
1385 if (handle
.context
!= context_id
&& handle
.context
!= peer
)
1386 return VMCI_ERROR_NO_ACCESS
;
1388 if (VMCI_CONTEXT_IS_VM(context_id
) && VMCI_CONTEXT_IS_VM(peer
))
1389 return VMCI_ERROR_DST_UNREACHABLE
;
1392 * Creator's context ID for local queue pairs should match the
1393 * peer, if a peer is specified.
1395 if (is_local
&& peer
!= VMCI_INVALID_ID
&& context_id
!= peer
)
1396 return VMCI_ERROR_NO_ACCESS
;
1398 entry
= kzalloc(sizeof(*entry
), GFP_ATOMIC
);
1400 return VMCI_ERROR_NO_MEM
;
1402 if (vmci_ctx_get_id(context
) == VMCI_HOST_CONTEXT_ID
&& !is_local
) {
1404 * The queue pair broker entry stores values from the guest
1405 * point of view, so a creating host side endpoint should swap
1406 * produce and consume values -- unless it is a local queue
1407 * pair, in which case no swapping is necessary, since the local
1408 * attacher will swap queues.
1411 guest_produce_size
= consume_size
;
1412 guest_consume_size
= produce_size
;
1414 guest_produce_size
= produce_size
;
1415 guest_consume_size
= consume_size
;
1418 entry
->qp
.handle
= handle
;
1419 entry
->qp
.peer
= peer
;
1420 entry
->qp
.flags
= flags
;
1421 entry
->qp
.produce_size
= guest_produce_size
;
1422 entry
->qp
.consume_size
= guest_consume_size
;
1423 entry
->qp
.ref_count
= 1;
1424 entry
->create_id
= context_id
;
1425 entry
->attach_id
= VMCI_INVALID_ID
;
1426 entry
->state
= VMCIQPB_NEW
;
1427 entry
->require_trusted_attach
=
1428 !!(context
->priv_flags
& VMCI_PRIVILEGE_FLAG_RESTRICTED
);
1429 entry
->created_by_trusted
=
1430 !!(priv_flags
& VMCI_PRIVILEGE_FLAG_TRUSTED
);
1431 entry
->vmci_page_files
= false;
1432 entry
->wakeup_cb
= wakeup_cb
;
1433 entry
->client_data
= client_data
;
1434 entry
->produce_q
= qp_host_alloc_queue(guest_produce_size
);
1435 if (entry
->produce_q
== NULL
) {
1436 result
= VMCI_ERROR_NO_MEM
;
1439 entry
->consume_q
= qp_host_alloc_queue(guest_consume_size
);
1440 if (entry
->consume_q
== NULL
) {
1441 result
= VMCI_ERROR_NO_MEM
;
1445 qp_init_queue_mutex(entry
->produce_q
, entry
->consume_q
);
1447 INIT_LIST_HEAD(&entry
->qp
.list_item
);
1452 entry
->local_mem
= kcalloc(QPE_NUM_PAGES(entry
->qp
),
1453 PAGE_SIZE
, GFP_KERNEL
);
1454 if (entry
->local_mem
== NULL
) {
1455 result
= VMCI_ERROR_NO_MEM
;
1458 entry
->state
= VMCIQPB_CREATED_MEM
;
1459 entry
->produce_q
->q_header
= entry
->local_mem
;
1460 tmp
= (u8
*)entry
->local_mem
+ PAGE_SIZE
*
1461 (DIV_ROUND_UP(entry
->qp
.produce_size
, PAGE_SIZE
) + 1);
1462 entry
->consume_q
->q_header
= (struct vmci_queue_header
*)tmp
;
1463 } else if (page_store
) {
1465 * The VMX already initialized the queue pair headers, so no
1466 * need for the kernel side to do that.
1468 result
= qp_host_register_user_memory(page_store
,
1471 if (result
< VMCI_SUCCESS
)
1474 entry
->state
= VMCIQPB_CREATED_MEM
;
1477 * A create without a page_store may be either a host
1478 * side create (in which case we are waiting for the
1479 * guest side to supply the memory) or an old style
1480 * queue pair create (in which case we will expect a
1481 * set page store call as the next step).
1483 entry
->state
= VMCIQPB_CREATED_NO_MEM
;
1486 qp_list_add_entry(&qp_broker_list
, &entry
->qp
);
1490 /* Add to resource obj */
1491 result
= vmci_resource_add(&entry
->resource
,
1492 VMCI_RESOURCE_TYPE_QPAIR_HOST
,
1494 if (result
!= VMCI_SUCCESS
) {
1495 pr_warn("Failed to add new resource (handle=0x%x:0x%x), error: %d",
1496 handle
.context
, handle
.resource
, result
);
1500 entry
->qp
.handle
= vmci_resource_handle(&entry
->resource
);
1502 vmci_q_header_init(entry
->produce_q
->q_header
,
1504 vmci_q_header_init(entry
->consume_q
->q_header
,
1508 vmci_ctx_qp_create(context
, entry
->qp
.handle
);
1510 return VMCI_SUCCESS
;
1513 if (entry
!= NULL
) {
1514 qp_host_free_queue(entry
->produce_q
, guest_produce_size
);
1515 qp_host_free_queue(entry
->consume_q
, guest_consume_size
);
1523 * Enqueues an event datagram to notify the peer VM attached to
1524 * the given queue pair handle about attach/detach event by the
1525 * given VM. Returns Payload size of datagram enqueued on
1526 * success, error code otherwise.
1528 static int qp_notify_peer(bool attach
,
1529 struct vmci_handle handle
,
1534 struct vmci_event_qp ev
;
1536 if (vmci_handle_is_invalid(handle
) || my_id
== VMCI_INVALID_ID
||
1537 peer_id
== VMCI_INVALID_ID
)
1538 return VMCI_ERROR_INVALID_ARGS
;
1541 * In vmci_ctx_enqueue_datagram() we enforce the upper limit on
1542 * number of pending events from the hypervisor to a given VM
1543 * otherwise a rogue VM could do an arbitrary number of attach
1544 * and detach operations causing memory pressure in the host
1548 ev
.msg
.hdr
.dst
= vmci_make_handle(peer_id
, VMCI_EVENT_HANDLER
);
1549 ev
.msg
.hdr
.src
= vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID
,
1550 VMCI_CONTEXT_RESOURCE_ID
);
1551 ev
.msg
.hdr
.payload_size
= sizeof(ev
) - sizeof(ev
.msg
.hdr
);
1552 ev
.msg
.event_data
.event
= attach
?
1553 VMCI_EVENT_QP_PEER_ATTACH
: VMCI_EVENT_QP_PEER_DETACH
;
1554 ev
.payload
.handle
= handle
;
1555 ev
.payload
.peer_id
= my_id
;
1557 rv
= vmci_datagram_dispatch(VMCI_HYPERVISOR_CONTEXT_ID
,
1558 &ev
.msg
.hdr
, false);
1559 if (rv
< VMCI_SUCCESS
)
1560 pr_warn("Failed to enqueue queue_pair %s event datagram for context (ID=0x%x)\n",
1561 attach
? "ATTACH" : "DETACH", peer_id
);
1567 * The second endpoint issuing a queue pair allocation will attach to
1568 * the queue pair registered with the queue pair broker.
1570 * If the attacher is a guest, it will associate a VMX virtual address
1571 * range with the queue pair as specified by the page_store. At this
1572 * point, the already attach host endpoint may start using the queue
1573 * pair, and an attach event is sent to it. For compatibility with
1574 * older VMX'en, that used a separate step to set the VMX virtual
1575 * address range, the virtual address range can be registered later
1576 * using vmci_qp_broker_set_page_store. In that case, a page_store of
1577 * NULL should be used, and the attach event will be generated once
1578 * the actual page store has been set.
1580 * If the attacher is the host, a page_store of NULL should be used as
1581 * well, since the page store information is already set by the guest.
1583 * For new VMX and host callers, the queue pair will be moved to the
1584 * VMCIQPB_ATTACHED_MEM state, and for older VMX callers, it will be
1585 * moved to the VMCOQPB_ATTACHED_NO_MEM state.
1587 static int qp_broker_attach(struct qp_broker_entry
*entry
,
1593 struct vmci_qp_page_store
*page_store
,
1594 struct vmci_ctx
*context
,
1595 vmci_event_release_cb wakeup_cb
,
1597 struct qp_broker_entry
**ent
)
1599 const u32 context_id
= vmci_ctx_get_id(context
);
1600 bool is_local
= flags
& VMCI_QPFLAG_LOCAL
;
1603 if (entry
->state
!= VMCIQPB_CREATED_NO_MEM
&&
1604 entry
->state
!= VMCIQPB_CREATED_MEM
)
1605 return VMCI_ERROR_UNAVAILABLE
;
1608 if (!(entry
->qp
.flags
& VMCI_QPFLAG_LOCAL
) ||
1609 context_id
!= entry
->create_id
) {
1610 return VMCI_ERROR_INVALID_ARGS
;
1612 } else if (context_id
== entry
->create_id
||
1613 context_id
== entry
->attach_id
) {
1614 return VMCI_ERROR_ALREADY_EXISTS
;
1617 if (VMCI_CONTEXT_IS_VM(context_id
) &&
1618 VMCI_CONTEXT_IS_VM(entry
->create_id
))
1619 return VMCI_ERROR_DST_UNREACHABLE
;
1622 * If we are attaching from a restricted context then the queuepair
1623 * must have been created by a trusted endpoint.
1625 if ((context
->priv_flags
& VMCI_PRIVILEGE_FLAG_RESTRICTED
) &&
1626 !entry
->created_by_trusted
)
1627 return VMCI_ERROR_NO_ACCESS
;
1630 * If we are attaching to a queuepair that was created by a restricted
1631 * context then we must be trusted.
1633 if (entry
->require_trusted_attach
&&
1634 (!(priv_flags
& VMCI_PRIVILEGE_FLAG_TRUSTED
)))
1635 return VMCI_ERROR_NO_ACCESS
;
1638 * If the creator specifies VMCI_INVALID_ID in "peer" field, access
1639 * control check is not performed.
1641 if (entry
->qp
.peer
!= VMCI_INVALID_ID
&& entry
->qp
.peer
!= context_id
)
1642 return VMCI_ERROR_NO_ACCESS
;
1644 if (entry
->create_id
== VMCI_HOST_CONTEXT_ID
) {
1646 * Do not attach if the caller doesn't support Host Queue Pairs
1647 * and a host created this queue pair.
1650 if (!vmci_ctx_supports_host_qp(context
))
1651 return VMCI_ERROR_INVALID_RESOURCE
;
1653 } else if (context_id
== VMCI_HOST_CONTEXT_ID
) {
1654 struct vmci_ctx
*create_context
;
1655 bool supports_host_qp
;
1658 * Do not attach a host to a user created queue pair if that
1659 * user doesn't support host queue pair end points.
1662 create_context
= vmci_ctx_get(entry
->create_id
);
1663 supports_host_qp
= vmci_ctx_supports_host_qp(create_context
);
1664 vmci_ctx_put(create_context
);
1666 if (!supports_host_qp
)
1667 return VMCI_ERROR_INVALID_RESOURCE
;
1670 if ((entry
->qp
.flags
& ~VMCI_QP_ASYMM
) != (flags
& ~VMCI_QP_ASYMM_PEER
))
1671 return VMCI_ERROR_QUEUEPAIR_MISMATCH
;
1673 if (context_id
!= VMCI_HOST_CONTEXT_ID
) {
1675 * The queue pair broker entry stores values from the guest
1676 * point of view, so an attaching guest should match the values
1677 * stored in the entry.
1680 if (entry
->qp
.produce_size
!= produce_size
||
1681 entry
->qp
.consume_size
!= consume_size
) {
1682 return VMCI_ERROR_QUEUEPAIR_MISMATCH
;
1684 } else if (entry
->qp
.produce_size
!= consume_size
||
1685 entry
->qp
.consume_size
!= produce_size
) {
1686 return VMCI_ERROR_QUEUEPAIR_MISMATCH
;
1689 if (context_id
!= VMCI_HOST_CONTEXT_ID
) {
1691 * If a guest attached to a queue pair, it will supply
1692 * the backing memory. If this is a pre NOVMVM vmx,
1693 * the backing memory will be supplied by calling
1694 * vmci_qp_broker_set_page_store() following the
1695 * return of the vmci_qp_broker_alloc() call. If it is
1696 * a vmx of version NOVMVM or later, the page store
1697 * must be supplied as part of the
1698 * vmci_qp_broker_alloc call. Under all circumstances
1699 * must the initially created queue pair not have any
1700 * memory associated with it already.
1703 if (entry
->state
!= VMCIQPB_CREATED_NO_MEM
)
1704 return VMCI_ERROR_INVALID_ARGS
;
1706 if (page_store
!= NULL
) {
1708 * Patch up host state to point to guest
1709 * supplied memory. The VMX already
1710 * initialized the queue pair headers, so no
1711 * need for the kernel side to do that.
1714 result
= qp_host_register_user_memory(page_store
,
1717 if (result
< VMCI_SUCCESS
)
1720 entry
->state
= VMCIQPB_ATTACHED_MEM
;
1722 entry
->state
= VMCIQPB_ATTACHED_NO_MEM
;
1724 } else if (entry
->state
== VMCIQPB_CREATED_NO_MEM
) {
1726 * The host side is attempting to attach to a queue
1727 * pair that doesn't have any memory associated with
1728 * it. This must be a pre NOVMVM vmx that hasn't set
1729 * the page store information yet, or a quiesced VM.
1732 return VMCI_ERROR_UNAVAILABLE
;
1734 /* The host side has successfully attached to a queue pair. */
1735 entry
->state
= VMCIQPB_ATTACHED_MEM
;
1738 if (entry
->state
== VMCIQPB_ATTACHED_MEM
) {
1740 qp_notify_peer(true, entry
->qp
.handle
, context_id
,
1742 if (result
< VMCI_SUCCESS
)
1743 pr_warn("Failed to notify peer (ID=0x%x) of attach to queue pair (handle=0x%x:0x%x)\n",
1744 entry
->create_id
, entry
->qp
.handle
.context
,
1745 entry
->qp
.handle
.resource
);
1748 entry
->attach_id
= context_id
;
1749 entry
->qp
.ref_count
++;
1751 entry
->wakeup_cb
= wakeup_cb
;
1752 entry
->client_data
= client_data
;
1756 * When attaching to local queue pairs, the context already has
1757 * an entry tracking the queue pair, so don't add another one.
1760 vmci_ctx_qp_create(context
, entry
->qp
.handle
);
1765 return VMCI_SUCCESS
;
1769 * queue_pair_Alloc for use when setting up queue pair endpoints
1772 static int qp_broker_alloc(struct vmci_handle handle
,
1778 struct vmci_qp_page_store
*page_store
,
1779 struct vmci_ctx
*context
,
1780 vmci_event_release_cb wakeup_cb
,
1782 struct qp_broker_entry
**ent
,
1785 const u32 context_id
= vmci_ctx_get_id(context
);
1787 struct qp_broker_entry
*entry
= NULL
;
1788 bool is_local
= flags
& VMCI_QPFLAG_LOCAL
;
1791 if (vmci_handle_is_invalid(handle
) ||
1792 (flags
& ~VMCI_QP_ALL_FLAGS
) || is_local
||
1793 !(produce_size
|| consume_size
) ||
1794 !context
|| context_id
== VMCI_INVALID_ID
||
1795 handle
.context
== VMCI_INVALID_ID
) {
1796 return VMCI_ERROR_INVALID_ARGS
;
1799 if (page_store
&& !VMCI_QP_PAGESTORE_IS_WELLFORMED(page_store
))
1800 return VMCI_ERROR_INVALID_ARGS
;
1803 * In the initial argument check, we ensure that non-vmkernel hosts
1804 * are not allowed to create local queue pairs.
1807 mutex_lock(&qp_broker_list
.mutex
);
1809 if (!is_local
&& vmci_ctx_qp_exists(context
, handle
)) {
1810 pr_devel("Context (ID=0x%x) already attached to queue pair (handle=0x%x:0x%x)\n",
1811 context_id
, handle
.context
, handle
.resource
);
1812 mutex_unlock(&qp_broker_list
.mutex
);
1813 return VMCI_ERROR_ALREADY_EXISTS
;
1816 if (handle
.resource
!= VMCI_INVALID_ID
)
1817 entry
= qp_broker_handle_to_entry(handle
);
1822 qp_broker_create(handle
, peer
, flags
, priv_flags
,
1823 produce_size
, consume_size
, page_store
,
1824 context
, wakeup_cb
, client_data
, ent
);
1828 qp_broker_attach(entry
, peer
, flags
, priv_flags
,
1829 produce_size
, consume_size
, page_store
,
1830 context
, wakeup_cb
, client_data
, ent
);
1833 mutex_unlock(&qp_broker_list
.mutex
);
1836 *swap
= (context_id
== VMCI_HOST_CONTEXT_ID
) &&
1837 !(create
&& is_local
);
1843 * This function implements the kernel API for allocating a queue
1846 static int qp_alloc_host_work(struct vmci_handle
*handle
,
1847 struct vmci_queue
**produce_q
,
1849 struct vmci_queue
**consume_q
,
1854 vmci_event_release_cb wakeup_cb
,
1857 struct vmci_handle new_handle
;
1858 struct vmci_ctx
*context
;
1859 struct qp_broker_entry
*entry
;
1863 if (vmci_handle_is_invalid(*handle
)) {
1864 new_handle
= vmci_make_handle(
1865 VMCI_HOST_CONTEXT_ID
, VMCI_INVALID_ID
);
1867 new_handle
= *handle
;
1869 context
= vmci_ctx_get(VMCI_HOST_CONTEXT_ID
);
1872 qp_broker_alloc(new_handle
, peer
, flags
, priv_flags
,
1873 produce_size
, consume_size
, NULL
, context
,
1874 wakeup_cb
, client_data
, &entry
, &swap
);
1875 if (result
== VMCI_SUCCESS
) {
1878 * If this is a local queue pair, the attacher
1879 * will swap around produce and consume
1883 *produce_q
= entry
->consume_q
;
1884 *consume_q
= entry
->produce_q
;
1886 *produce_q
= entry
->produce_q
;
1887 *consume_q
= entry
->consume_q
;
1890 *handle
= vmci_resource_handle(&entry
->resource
);
1892 *handle
= VMCI_INVALID_HANDLE
;
1893 pr_devel("queue pair broker failed to alloc (result=%d)\n",
1896 vmci_ctx_put(context
);
1901 * Allocates a VMCI queue_pair. Only checks validity of input
1902 * arguments. The real work is done in the host or guest
1903 * specific function.
1905 int vmci_qp_alloc(struct vmci_handle
*handle
,
1906 struct vmci_queue
**produce_q
,
1908 struct vmci_queue
**consume_q
,
1913 bool guest_endpoint
,
1914 vmci_event_release_cb wakeup_cb
,
1917 if (!handle
|| !produce_q
|| !consume_q
||
1918 (!produce_size
&& !consume_size
) || (flags
& ~VMCI_QP_ALL_FLAGS
))
1919 return VMCI_ERROR_INVALID_ARGS
;
1921 if (guest_endpoint
) {
1922 return qp_alloc_guest_work(handle
, produce_q
,
1923 produce_size
, consume_q
,
1927 return qp_alloc_host_work(handle
, produce_q
,
1928 produce_size
, consume_q
,
1929 consume_size
, peer
, flags
,
1930 priv_flags
, wakeup_cb
, client_data
);
1935 * This function implements the host kernel API for detaching from
1938 static int qp_detatch_host_work(struct vmci_handle handle
)
1941 struct vmci_ctx
*context
;
1943 context
= vmci_ctx_get(VMCI_HOST_CONTEXT_ID
);
1945 result
= vmci_qp_broker_detach(handle
, context
);
1947 vmci_ctx_put(context
);
1952 * Detaches from a VMCI queue_pair. Only checks validity of input argument.
1953 * Real work is done in the host or guest specific function.
1955 static int qp_detatch(struct vmci_handle handle
, bool guest_endpoint
)
1957 if (vmci_handle_is_invalid(handle
))
1958 return VMCI_ERROR_INVALID_ARGS
;
1961 return qp_detatch_guest_work(handle
);
1963 return qp_detatch_host_work(handle
);
1967 * Returns the entry from the head of the list. Assumes that the list is
1970 static struct qp_entry
*qp_list_get_head(struct qp_list
*qp_list
)
1972 if (!list_empty(&qp_list
->head
)) {
1973 struct qp_entry
*entry
=
1974 list_first_entry(&qp_list
->head
, struct qp_entry
,
1982 void vmci_qp_broker_exit(void)
1984 struct qp_entry
*entry
;
1985 struct qp_broker_entry
*be
;
1987 mutex_lock(&qp_broker_list
.mutex
);
1989 while ((entry
= qp_list_get_head(&qp_broker_list
))) {
1990 be
= (struct qp_broker_entry
*)entry
;
1992 qp_list_remove_entry(&qp_broker_list
, entry
);
1996 mutex_unlock(&qp_broker_list
.mutex
);
2000 * Requests that a queue pair be allocated with the VMCI queue
2001 * pair broker. Allocates a queue pair entry if one does not
2002 * exist. Attaches to one if it exists, and retrieves the page
2003 * files backing that queue_pair. Assumes that the queue pair
2004 * broker lock is held.
2006 int vmci_qp_broker_alloc(struct vmci_handle handle
,
2012 struct vmci_qp_page_store
*page_store
,
2013 struct vmci_ctx
*context
)
2015 return qp_broker_alloc(handle
, peer
, flags
, priv_flags
,
2016 produce_size
, consume_size
,
2017 page_store
, context
, NULL
, NULL
, NULL
, NULL
);
2021 * VMX'en with versions lower than VMCI_VERSION_NOVMVM use a separate
2022 * step to add the UVAs of the VMX mapping of the queue pair. This function
2023 * provides backwards compatibility with such VMX'en, and takes care of
2024 * registering the page store for a queue pair previously allocated by the
2025 * VMX during create or attach. This function will move the queue pair state
2026 * to either from VMCIQBP_CREATED_NO_MEM to VMCIQBP_CREATED_MEM or
2027 * VMCIQBP_ATTACHED_NO_MEM to VMCIQBP_ATTACHED_MEM. If moving to the
2028 * attached state with memory, the queue pair is ready to be used by the
2029 * host peer, and an attached event will be generated.
2031 * Assumes that the queue pair broker lock is held.
2033 * This function is only used by the hosted platform, since there is no
2034 * issue with backwards compatibility for vmkernel.
2036 int vmci_qp_broker_set_page_store(struct vmci_handle handle
,
2039 struct vmci_ctx
*context
)
2041 struct qp_broker_entry
*entry
;
2043 const u32 context_id
= vmci_ctx_get_id(context
);
2045 if (vmci_handle_is_invalid(handle
) || !context
||
2046 context_id
== VMCI_INVALID_ID
)
2047 return VMCI_ERROR_INVALID_ARGS
;
2050 * We only support guest to host queue pairs, so the VMX must
2051 * supply UVAs for the mapped page files.
2054 if (produce_uva
== 0 || consume_uva
== 0)
2055 return VMCI_ERROR_INVALID_ARGS
;
2057 mutex_lock(&qp_broker_list
.mutex
);
2059 if (!vmci_ctx_qp_exists(context
, handle
)) {
2060 pr_warn("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
2061 context_id
, handle
.context
, handle
.resource
);
2062 result
= VMCI_ERROR_NOT_FOUND
;
2066 entry
= qp_broker_handle_to_entry(handle
);
2068 result
= VMCI_ERROR_NOT_FOUND
;
2073 * If I'm the owner then I can set the page store.
2075 * Or, if a host created the queue_pair and I'm the attached peer
2076 * then I can set the page store.
2078 if (entry
->create_id
!= context_id
&&
2079 (entry
->create_id
!= VMCI_HOST_CONTEXT_ID
||
2080 entry
->attach_id
!= context_id
)) {
2081 result
= VMCI_ERROR_QUEUEPAIR_NOTOWNER
;
2085 if (entry
->state
!= VMCIQPB_CREATED_NO_MEM
&&
2086 entry
->state
!= VMCIQPB_ATTACHED_NO_MEM
) {
2087 result
= VMCI_ERROR_UNAVAILABLE
;
2091 result
= qp_host_get_user_memory(produce_uva
, consume_uva
,
2092 entry
->produce_q
, entry
->consume_q
);
2093 if (result
< VMCI_SUCCESS
)
2096 result
= qp_host_map_queues(entry
->produce_q
, entry
->consume_q
);
2097 if (result
< VMCI_SUCCESS
) {
2098 qp_host_unregister_user_memory(entry
->produce_q
,
2103 if (entry
->state
== VMCIQPB_CREATED_NO_MEM
)
2104 entry
->state
= VMCIQPB_CREATED_MEM
;
2106 entry
->state
= VMCIQPB_ATTACHED_MEM
;
2108 entry
->vmci_page_files
= true;
2110 if (entry
->state
== VMCIQPB_ATTACHED_MEM
) {
2112 qp_notify_peer(true, handle
, context_id
, entry
->create_id
);
2113 if (result
< VMCI_SUCCESS
) {
2114 pr_warn("Failed to notify peer (ID=0x%x) of attach to queue pair (handle=0x%x:0x%x)\n",
2115 entry
->create_id
, entry
->qp
.handle
.context
,
2116 entry
->qp
.handle
.resource
);
2120 result
= VMCI_SUCCESS
;
2122 mutex_unlock(&qp_broker_list
.mutex
);
2127 * Resets saved queue headers for the given QP broker
2128 * entry. Should be used when guest memory becomes available
2129 * again, or the guest detaches.
2131 static void qp_reset_saved_headers(struct qp_broker_entry
*entry
)
2133 entry
->produce_q
->saved_header
= NULL
;
2134 entry
->consume_q
->saved_header
= NULL
;
2138 * The main entry point for detaching from a queue pair registered with the
2139 * queue pair broker. If more than one endpoint is attached to the queue
2140 * pair, the first endpoint will mainly decrement a reference count and
2141 * generate a notification to its peer. The last endpoint will clean up
2142 * the queue pair state registered with the broker.
2144 * When a guest endpoint detaches, it will unmap and unregister the guest
2145 * memory backing the queue pair. If the host is still attached, it will
2146 * no longer be able to access the queue pair content.
2148 * If the queue pair is already in a state where there is no memory
2149 * registered for the queue pair (any *_NO_MEM state), it will transition to
2150 * the VMCIQPB_SHUTDOWN_NO_MEM state. This will also happen, if a guest
2151 * endpoint is the first of two endpoints to detach. If the host endpoint is
2152 * the first out of two to detach, the queue pair will move to the
2153 * VMCIQPB_SHUTDOWN_MEM state.
2155 int vmci_qp_broker_detach(struct vmci_handle handle
, struct vmci_ctx
*context
)
2157 struct qp_broker_entry
*entry
;
2158 const u32 context_id
= vmci_ctx_get_id(context
);
2160 bool is_local
= false;
2163 if (vmci_handle_is_invalid(handle
) || !context
||
2164 context_id
== VMCI_INVALID_ID
) {
2165 return VMCI_ERROR_INVALID_ARGS
;
2168 mutex_lock(&qp_broker_list
.mutex
);
2170 if (!vmci_ctx_qp_exists(context
, handle
)) {
2171 pr_devel("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
2172 context_id
, handle
.context
, handle
.resource
);
2173 result
= VMCI_ERROR_NOT_FOUND
;
2177 entry
= qp_broker_handle_to_entry(handle
);
2179 pr_devel("Context (ID=0x%x) reports being attached to queue pair(handle=0x%x:0x%x) that isn't present in broker\n",
2180 context_id
, handle
.context
, handle
.resource
);
2181 result
= VMCI_ERROR_NOT_FOUND
;
2185 if (context_id
!= entry
->create_id
&& context_id
!= entry
->attach_id
) {
2186 result
= VMCI_ERROR_QUEUEPAIR_NOTATTACHED
;
2190 if (context_id
== entry
->create_id
) {
2191 peer_id
= entry
->attach_id
;
2192 entry
->create_id
= VMCI_INVALID_ID
;
2194 peer_id
= entry
->create_id
;
2195 entry
->attach_id
= VMCI_INVALID_ID
;
2197 entry
->qp
.ref_count
--;
2199 is_local
= entry
->qp
.flags
& VMCI_QPFLAG_LOCAL
;
2201 if (context_id
!= VMCI_HOST_CONTEXT_ID
) {
2202 bool headers_mapped
;
2205 * Pre NOVMVM vmx'en may detach from a queue pair
2206 * before setting the page store, and in that case
2207 * there is no user memory to detach from. Also, more
2208 * recent VMX'en may detach from a queue pair in the
2212 qp_acquire_queue_mutex(entry
->produce_q
);
2213 headers_mapped
= entry
->produce_q
->q_header
||
2214 entry
->consume_q
->q_header
;
2215 if (QPBROKERSTATE_HAS_MEM(entry
)) {
2217 qp_host_unmap_queues(INVALID_VMCI_GUEST_MEM_ID
,
2220 if (result
< VMCI_SUCCESS
)
2221 pr_warn("Failed to unmap queue headers for queue pair (handle=0x%x:0x%x,result=%d)\n",
2222 handle
.context
, handle
.resource
,
2225 if (entry
->vmci_page_files
)
2226 qp_host_unregister_user_memory(entry
->produce_q
,
2230 qp_host_unregister_user_memory(entry
->produce_q
,
2236 if (!headers_mapped
)
2237 qp_reset_saved_headers(entry
);
2239 qp_release_queue_mutex(entry
->produce_q
);
2241 if (!headers_mapped
&& entry
->wakeup_cb
)
2242 entry
->wakeup_cb(entry
->client_data
);
2245 if (entry
->wakeup_cb
) {
2246 entry
->wakeup_cb
= NULL
;
2247 entry
->client_data
= NULL
;
2251 if (entry
->qp
.ref_count
== 0) {
2252 qp_list_remove_entry(&qp_broker_list
, &entry
->qp
);
2255 kfree(entry
->local_mem
);
2257 qp_cleanup_queue_mutex(entry
->produce_q
, entry
->consume_q
);
2258 qp_host_free_queue(entry
->produce_q
, entry
->qp
.produce_size
);
2259 qp_host_free_queue(entry
->consume_q
, entry
->qp
.consume_size
);
2260 /* Unlink from resource hash table and free callback */
2261 vmci_resource_remove(&entry
->resource
);
2265 vmci_ctx_qp_destroy(context
, handle
);
2267 qp_notify_peer(false, handle
, context_id
, peer_id
);
2268 if (context_id
== VMCI_HOST_CONTEXT_ID
&&
2269 QPBROKERSTATE_HAS_MEM(entry
)) {
2270 entry
->state
= VMCIQPB_SHUTDOWN_MEM
;
2272 entry
->state
= VMCIQPB_SHUTDOWN_NO_MEM
;
2276 vmci_ctx_qp_destroy(context
, handle
);
2279 result
= VMCI_SUCCESS
;
2281 mutex_unlock(&qp_broker_list
.mutex
);
2286 * Establishes the necessary mappings for a queue pair given a
2287 * reference to the queue pair guest memory. This is usually
2288 * called when a guest is unquiesced and the VMX is allowed to
2289 * map guest memory once again.
2291 int vmci_qp_broker_map(struct vmci_handle handle
,
2292 struct vmci_ctx
*context
,
2295 struct qp_broker_entry
*entry
;
2296 const u32 context_id
= vmci_ctx_get_id(context
);
2297 bool is_local
= false;
2300 if (vmci_handle_is_invalid(handle
) || !context
||
2301 context_id
== VMCI_INVALID_ID
)
2302 return VMCI_ERROR_INVALID_ARGS
;
2304 mutex_lock(&qp_broker_list
.mutex
);
2306 if (!vmci_ctx_qp_exists(context
, handle
)) {
2307 pr_devel("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
2308 context_id
, handle
.context
, handle
.resource
);
2309 result
= VMCI_ERROR_NOT_FOUND
;
2313 entry
= qp_broker_handle_to_entry(handle
);
2315 pr_devel("Context (ID=0x%x) reports being attached to queue pair (handle=0x%x:0x%x) that isn't present in broker\n",
2316 context_id
, handle
.context
, handle
.resource
);
2317 result
= VMCI_ERROR_NOT_FOUND
;
2321 if (context_id
!= entry
->create_id
&& context_id
!= entry
->attach_id
) {
2322 result
= VMCI_ERROR_QUEUEPAIR_NOTATTACHED
;
2326 is_local
= entry
->qp
.flags
& VMCI_QPFLAG_LOCAL
;
2327 result
= VMCI_SUCCESS
;
2329 if (context_id
!= VMCI_HOST_CONTEXT_ID
) {
2330 struct vmci_qp_page_store page_store
;
2332 page_store
.pages
= guest_mem
;
2333 page_store
.len
= QPE_NUM_PAGES(entry
->qp
);
2335 qp_acquire_queue_mutex(entry
->produce_q
);
2336 qp_reset_saved_headers(entry
);
2338 qp_host_register_user_memory(&page_store
,
2341 qp_release_queue_mutex(entry
->produce_q
);
2342 if (result
== VMCI_SUCCESS
) {
2343 /* Move state from *_NO_MEM to *_MEM */
2347 if (entry
->wakeup_cb
)
2348 entry
->wakeup_cb(entry
->client_data
);
2353 mutex_unlock(&qp_broker_list
.mutex
);
2358 * Saves a snapshot of the queue headers for the given QP broker
2359 * entry. Should be used when guest memory is unmapped.
2361 * VMCI_SUCCESS on success, appropriate error code if guest memory
2362 * can't be accessed..
2364 static int qp_save_headers(struct qp_broker_entry
*entry
)
2368 if (entry
->produce_q
->saved_header
!= NULL
&&
2369 entry
->consume_q
->saved_header
!= NULL
) {
2371 * If the headers have already been saved, we don't need to do
2372 * it again, and we don't want to map in the headers
2376 return VMCI_SUCCESS
;
2379 if (NULL
== entry
->produce_q
->q_header
||
2380 NULL
== entry
->consume_q
->q_header
) {
2381 result
= qp_host_map_queues(entry
->produce_q
, entry
->consume_q
);
2382 if (result
< VMCI_SUCCESS
)
2386 memcpy(&entry
->saved_produce_q
, entry
->produce_q
->q_header
,
2387 sizeof(entry
->saved_produce_q
));
2388 entry
->produce_q
->saved_header
= &entry
->saved_produce_q
;
2389 memcpy(&entry
->saved_consume_q
, entry
->consume_q
->q_header
,
2390 sizeof(entry
->saved_consume_q
));
2391 entry
->consume_q
->saved_header
= &entry
->saved_consume_q
;
2393 return VMCI_SUCCESS
;
2397 * Removes all references to the guest memory of a given queue pair, and
2398 * will move the queue pair from state *_MEM to *_NO_MEM. It is usually
2399 * called when a VM is being quiesced where access to guest memory should
2402 int vmci_qp_broker_unmap(struct vmci_handle handle
,
2403 struct vmci_ctx
*context
,
2406 struct qp_broker_entry
*entry
;
2407 const u32 context_id
= vmci_ctx_get_id(context
);
2408 bool is_local
= false;
2411 if (vmci_handle_is_invalid(handle
) || !context
||
2412 context_id
== VMCI_INVALID_ID
)
2413 return VMCI_ERROR_INVALID_ARGS
;
2415 mutex_lock(&qp_broker_list
.mutex
);
2417 if (!vmci_ctx_qp_exists(context
, handle
)) {
2418 pr_devel("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
2419 context_id
, handle
.context
, handle
.resource
);
2420 result
= VMCI_ERROR_NOT_FOUND
;
2424 entry
= qp_broker_handle_to_entry(handle
);
2426 pr_devel("Context (ID=0x%x) reports being attached to queue pair (handle=0x%x:0x%x) that isn't present in broker\n",
2427 context_id
, handle
.context
, handle
.resource
);
2428 result
= VMCI_ERROR_NOT_FOUND
;
2432 if (context_id
!= entry
->create_id
&& context_id
!= entry
->attach_id
) {
2433 result
= VMCI_ERROR_QUEUEPAIR_NOTATTACHED
;
2437 is_local
= entry
->qp
.flags
& VMCI_QPFLAG_LOCAL
;
2439 if (context_id
!= VMCI_HOST_CONTEXT_ID
) {
2440 qp_acquire_queue_mutex(entry
->produce_q
);
2441 result
= qp_save_headers(entry
);
2442 if (result
< VMCI_SUCCESS
)
2443 pr_warn("Failed to save queue headers for queue pair (handle=0x%x:0x%x,result=%d)\n",
2444 handle
.context
, handle
.resource
, result
);
2446 qp_host_unmap_queues(gid
, entry
->produce_q
, entry
->consume_q
);
2449 * On hosted, when we unmap queue pairs, the VMX will also
2450 * unmap the guest memory, so we invalidate the previously
2451 * registered memory. If the queue pair is mapped again at a
2452 * later point in time, we will need to reregister the user
2453 * memory with a possibly new user VA.
2455 qp_host_unregister_user_memory(entry
->produce_q
,
2459 * Move state from *_MEM to *_NO_MEM.
2463 qp_release_queue_mutex(entry
->produce_q
);
2466 result
= VMCI_SUCCESS
;
2469 mutex_unlock(&qp_broker_list
.mutex
);
2474 * Destroys all guest queue pair endpoints. If active guest queue
2475 * pairs still exist, hypercalls to attempt detach from these
2476 * queue pairs will be made. Any failure to detach is silently
2479 void vmci_qp_guest_endpoints_exit(void)
2481 struct qp_entry
*entry
;
2482 struct qp_guest_endpoint
*ep
;
2484 mutex_lock(&qp_guest_endpoints
.mutex
);
2486 while ((entry
= qp_list_get_head(&qp_guest_endpoints
))) {
2487 ep
= (struct qp_guest_endpoint
*)entry
;
2489 /* Don't make a hypercall for local queue_pairs. */
2490 if (!(entry
->flags
& VMCI_QPFLAG_LOCAL
))
2491 qp_detatch_hypercall(entry
->handle
);
2493 /* We cannot fail the exit, so let's reset ref_count. */
2494 entry
->ref_count
= 0;
2495 qp_list_remove_entry(&qp_guest_endpoints
, entry
);
2497 qp_guest_endpoint_destroy(ep
);
2500 mutex_unlock(&qp_guest_endpoints
.mutex
);
2504 * Helper routine that will lock the queue pair before subsequent
2506 * Note: Non-blocking on the host side is currently only implemented in ESX.
2507 * Since non-blocking isn't yet implemented on the host personality we
2508 * have no reason to acquire a spin lock. So to avoid the use of an
2509 * unnecessary lock only acquire the mutex if we can block.
2511 static void qp_lock(const struct vmci_qp
*qpair
)
2513 qp_acquire_queue_mutex(qpair
->produce_q
);
2517 * Helper routine that unlocks the queue pair after calling
2520 static void qp_unlock(const struct vmci_qp
*qpair
)
2522 qp_release_queue_mutex(qpair
->produce_q
);
2526 * The queue headers may not be mapped at all times. If a queue is
2527 * currently not mapped, it will be attempted to do so.
2529 static int qp_map_queue_headers(struct vmci_queue
*produce_q
,
2530 struct vmci_queue
*consume_q
)
2534 if (NULL
== produce_q
->q_header
|| NULL
== consume_q
->q_header
) {
2535 result
= qp_host_map_queues(produce_q
, consume_q
);
2536 if (result
< VMCI_SUCCESS
)
2537 return (produce_q
->saved_header
&&
2538 consume_q
->saved_header
) ?
2539 VMCI_ERROR_QUEUEPAIR_NOT_READY
:
2540 VMCI_ERROR_QUEUEPAIR_NOTATTACHED
;
2543 return VMCI_SUCCESS
;
2547 * Helper routine that will retrieve the produce and consume
2548 * headers of a given queue pair. If the guest memory of the
2549 * queue pair is currently not available, the saved queue headers
2550 * will be returned, if these are available.
2552 static int qp_get_queue_headers(const struct vmci_qp
*qpair
,
2553 struct vmci_queue_header
**produce_q_header
,
2554 struct vmci_queue_header
**consume_q_header
)
2558 result
= qp_map_queue_headers(qpair
->produce_q
, qpair
->consume_q
);
2559 if (result
== VMCI_SUCCESS
) {
2560 *produce_q_header
= qpair
->produce_q
->q_header
;
2561 *consume_q_header
= qpair
->consume_q
->q_header
;
2562 } else if (qpair
->produce_q
->saved_header
&&
2563 qpair
->consume_q
->saved_header
) {
2564 *produce_q_header
= qpair
->produce_q
->saved_header
;
2565 *consume_q_header
= qpair
->consume_q
->saved_header
;
2566 result
= VMCI_SUCCESS
;
2573 * Callback from VMCI queue pair broker indicating that a queue
2574 * pair that was previously not ready, now either is ready or
2577 static int qp_wakeup_cb(void *client_data
)
2579 struct vmci_qp
*qpair
= (struct vmci_qp
*)client_data
;
2582 while (qpair
->blocked
> 0) {
2584 qpair
->generation
++;
2585 wake_up(&qpair
->event
);
2589 return VMCI_SUCCESS
;
2593 * Makes the calling thread wait for the queue pair to become
2594 * ready for host side access. Returns true when thread is
2595 * woken up after queue pair state change, false otherwise.
2597 static bool qp_wait_for_ready_queue(struct vmci_qp
*qpair
)
2599 unsigned int generation
;
2602 generation
= qpair
->generation
;
2604 wait_event(qpair
->event
, generation
!= qpair
->generation
);
2611 * Enqueues a given buffer to the produce queue using the provided
2612 * function. As many bytes as possible (space available in the queue)
2613 * are enqueued. Assumes the queue->mutex has been acquired. Returns
2614 * VMCI_ERROR_QUEUEPAIR_NOSPACE if no space was available to enqueue
2615 * data, VMCI_ERROR_INVALID_SIZE, if any queue pointer is outside the
2616 * queue (as defined by the queue size), VMCI_ERROR_INVALID_ARGS, if
2617 * an error occured when accessing the buffer,
2618 * VMCI_ERROR_QUEUEPAIR_NOTATTACHED, if the queue pair pages aren't
2619 * available. Otherwise, the number of bytes written to the queue is
2620 * returned. Updates the tail pointer of the produce queue.
2622 static ssize_t
qp_enqueue_locked(struct vmci_queue
*produce_q
,
2623 struct vmci_queue
*consume_q
,
2624 const u64 produce_q_size
,
2627 vmci_memcpy_to_queue_func memcpy_to_queue
)
2634 result
= qp_map_queue_headers(produce_q
, consume_q
);
2635 if (unlikely(result
!= VMCI_SUCCESS
))
2638 free_space
= vmci_q_header_free_space(produce_q
->q_header
,
2639 consume_q
->q_header
,
2641 if (free_space
== 0)
2642 return VMCI_ERROR_QUEUEPAIR_NOSPACE
;
2644 if (free_space
< VMCI_SUCCESS
)
2645 return (ssize_t
) free_space
;
2647 written
= (size_t) (free_space
> buf_size
? buf_size
: free_space
);
2648 tail
= vmci_q_header_producer_tail(produce_q
->q_header
);
2649 if (likely(tail
+ written
< produce_q_size
)) {
2650 result
= memcpy_to_queue(produce_q
, tail
, buf
, 0, written
);
2652 /* Tail pointer wraps around. */
2654 const size_t tmp
= (size_t) (produce_q_size
- tail
);
2656 result
= memcpy_to_queue(produce_q
, tail
, buf
, 0, tmp
);
2657 if (result
>= VMCI_SUCCESS
)
2658 result
= memcpy_to_queue(produce_q
, 0, buf
, tmp
,
2662 if (result
< VMCI_SUCCESS
)
2665 vmci_q_header_add_producer_tail(produce_q
->q_header
, written
,
2671 * Dequeues data (if available) from the given consume queue. Writes data
2672 * to the user provided buffer using the provided function.
2673 * Assumes the queue->mutex has been acquired.
2675 * VMCI_ERROR_QUEUEPAIR_NODATA if no data was available to dequeue.
2676 * VMCI_ERROR_INVALID_SIZE, if any queue pointer is outside the queue
2677 * (as defined by the queue size).
2678 * VMCI_ERROR_INVALID_ARGS, if an error occured when accessing the buffer.
2679 * Otherwise the number of bytes dequeued is returned.
2681 * Updates the head pointer of the consume queue.
2683 static ssize_t
qp_dequeue_locked(struct vmci_queue
*produce_q
,
2684 struct vmci_queue
*consume_q
,
2685 const u64 consume_q_size
,
2688 vmci_memcpy_from_queue_func memcpy_from_queue
,
2689 bool update_consumer
)
2696 result
= qp_map_queue_headers(produce_q
, consume_q
);
2697 if (unlikely(result
!= VMCI_SUCCESS
))
2700 buf_ready
= vmci_q_header_buf_ready(consume_q
->q_header
,
2701 produce_q
->q_header
,
2704 return VMCI_ERROR_QUEUEPAIR_NODATA
;
2706 if (buf_ready
< VMCI_SUCCESS
)
2707 return (ssize_t
) buf_ready
;
2709 read
= (size_t) (buf_ready
> buf_size
? buf_size
: buf_ready
);
2710 head
= vmci_q_header_consumer_head(produce_q
->q_header
);
2711 if (likely(head
+ read
< consume_q_size
)) {
2712 result
= memcpy_from_queue(buf
, 0, consume_q
, head
, read
);
2714 /* Head pointer wraps around. */
2716 const size_t tmp
= (size_t) (consume_q_size
- head
);
2718 result
= memcpy_from_queue(buf
, 0, consume_q
, head
, tmp
);
2719 if (result
>= VMCI_SUCCESS
)
2720 result
= memcpy_from_queue(buf
, tmp
, consume_q
, 0,
2725 if (result
< VMCI_SUCCESS
)
2728 if (update_consumer
)
2729 vmci_q_header_add_consumer_head(produce_q
->q_header
,
2730 read
, consume_q_size
);
2736 * vmci_qpair_alloc() - Allocates a queue pair.
2737 * @qpair: Pointer for the new vmci_qp struct.
2738 * @handle: Handle to track the resource.
2739 * @produce_qsize: Desired size of the producer queue.
2740 * @consume_qsize: Desired size of the consumer queue.
2741 * @peer: ContextID of the peer.
2742 * @flags: VMCI flags.
2743 * @priv_flags: VMCI priviledge flags.
2745 * This is the client interface for allocating the memory for a
2746 * vmci_qp structure and then attaching to the underlying
2747 * queue. If an error occurs allocating the memory for the
2748 * vmci_qp structure no attempt is made to attach. If an
2749 * error occurs attaching, then the structure is freed.
2751 int vmci_qpair_alloc(struct vmci_qp
**qpair
,
2752 struct vmci_handle
*handle
,
2759 struct vmci_qp
*my_qpair
;
2761 struct vmci_handle src
= VMCI_INVALID_HANDLE
;
2762 struct vmci_handle dst
= vmci_make_handle(peer
, VMCI_INVALID_ID
);
2763 enum vmci_route route
;
2764 vmci_event_release_cb wakeup_cb
;
2768 * Restrict the size of a queuepair. The device already
2769 * enforces a limit on the total amount of memory that can be
2770 * allocated to queuepairs for a guest. However, we try to
2771 * allocate this memory before we make the queuepair
2772 * allocation hypercall. On Linux, we allocate each page
2773 * separately, which means rather than fail, the guest will
2774 * thrash while it tries to allocate, and will become
2775 * increasingly unresponsive to the point where it appears to
2776 * be hung. So we place a limit on the size of an individual
2777 * queuepair here, and leave the device to enforce the
2778 * restriction on total queuepair memory. (Note that this
2779 * doesn't prevent all cases; a user with only this much
2780 * physical memory could still get into trouble.) The error
2781 * used by the device is NO_RESOURCES, so use that here too.
2784 if (produce_qsize
+ consume_qsize
< max(produce_qsize
, consume_qsize
) ||
2785 produce_qsize
+ consume_qsize
> VMCI_MAX_GUEST_QP_MEMORY
)
2786 return VMCI_ERROR_NO_RESOURCES
;
2788 retval
= vmci_route(&src
, &dst
, false, &route
);
2789 if (retval
< VMCI_SUCCESS
)
2790 route
= vmci_guest_code_active() ?
2791 VMCI_ROUTE_AS_GUEST
: VMCI_ROUTE_AS_HOST
;
2793 if (flags
& (VMCI_QPFLAG_NONBLOCK
| VMCI_QPFLAG_PINNED
)) {
2794 pr_devel("NONBLOCK OR PINNED set");
2795 return VMCI_ERROR_INVALID_ARGS
;
2798 my_qpair
= kzalloc(sizeof(*my_qpair
), GFP_KERNEL
);
2800 return VMCI_ERROR_NO_MEM
;
2802 my_qpair
->produce_q_size
= produce_qsize
;
2803 my_qpair
->consume_q_size
= consume_qsize
;
2804 my_qpair
->peer
= peer
;
2805 my_qpair
->flags
= flags
;
2806 my_qpair
->priv_flags
= priv_flags
;
2811 if (VMCI_ROUTE_AS_HOST
== route
) {
2812 my_qpair
->guest_endpoint
= false;
2813 if (!(flags
& VMCI_QPFLAG_LOCAL
)) {
2814 my_qpair
->blocked
= 0;
2815 my_qpair
->generation
= 0;
2816 init_waitqueue_head(&my_qpair
->event
);
2817 wakeup_cb
= qp_wakeup_cb
;
2818 client_data
= (void *)my_qpair
;
2821 my_qpair
->guest_endpoint
= true;
2824 retval
= vmci_qp_alloc(handle
,
2825 &my_qpair
->produce_q
,
2826 my_qpair
->produce_q_size
,
2827 &my_qpair
->consume_q
,
2828 my_qpair
->consume_q_size
,
2831 my_qpair
->priv_flags
,
2832 my_qpair
->guest_endpoint
,
2833 wakeup_cb
, client_data
);
2835 if (retval
< VMCI_SUCCESS
) {
2841 my_qpair
->handle
= *handle
;
2845 EXPORT_SYMBOL_GPL(vmci_qpair_alloc
);
2848 * vmci_qpair_detach() - Detatches the client from a queue pair.
2849 * @qpair: Reference of a pointer to the qpair struct.
2851 * This is the client interface for detaching from a VMCIQPair.
2852 * Note that this routine will free the memory allocated for the
2853 * vmci_qp structure too.
2855 int vmci_qpair_detach(struct vmci_qp
**qpair
)
2858 struct vmci_qp
*old_qpair
;
2860 if (!qpair
|| !(*qpair
))
2861 return VMCI_ERROR_INVALID_ARGS
;
2864 result
= qp_detatch(old_qpair
->handle
, old_qpair
->guest_endpoint
);
2867 * The guest can fail to detach for a number of reasons, and
2868 * if it does so, it will cleanup the entry (if there is one).
2869 * The host can fail too, but it won't cleanup the entry
2870 * immediately, it will do that later when the context is
2871 * freed. Either way, we need to release the qpair struct
2872 * here; there isn't much the caller can do, and we don't want
2876 memset(old_qpair
, 0, sizeof(*old_qpair
));
2877 old_qpair
->handle
= VMCI_INVALID_HANDLE
;
2878 old_qpair
->peer
= VMCI_INVALID_ID
;
2884 EXPORT_SYMBOL_GPL(vmci_qpair_detach
);
2887 * vmci_qpair_get_produce_indexes() - Retrieves the indexes of the producer.
2888 * @qpair: Pointer to the queue pair struct.
2889 * @producer_tail: Reference used for storing producer tail index.
2890 * @consumer_head: Reference used for storing the consumer head index.
2892 * This is the client interface for getting the current indexes of the
2893 * QPair from the point of the view of the caller as the producer.
2895 int vmci_qpair_get_produce_indexes(const struct vmci_qp
*qpair
,
2899 struct vmci_queue_header
*produce_q_header
;
2900 struct vmci_queue_header
*consume_q_header
;
2904 return VMCI_ERROR_INVALID_ARGS
;
2908 qp_get_queue_headers(qpair
, &produce_q_header
, &consume_q_header
);
2909 if (result
== VMCI_SUCCESS
)
2910 vmci_q_header_get_pointers(produce_q_header
, consume_q_header
,
2911 producer_tail
, consumer_head
);
2914 if (result
== VMCI_SUCCESS
&&
2915 ((producer_tail
&& *producer_tail
>= qpair
->produce_q_size
) ||
2916 (consumer_head
&& *consumer_head
>= qpair
->produce_q_size
)))
2917 return VMCI_ERROR_INVALID_SIZE
;
2921 EXPORT_SYMBOL_GPL(vmci_qpair_get_produce_indexes
);
2924 * vmci_qpair_get_consume_indexes() - Retrieves the indexes of the comsumer.
2925 * @qpair: Pointer to the queue pair struct.
2926 * @consumer_tail: Reference used for storing consumer tail index.
2927 * @producer_head: Reference used for storing the producer head index.
2929 * This is the client interface for getting the current indexes of the
2930 * QPair from the point of the view of the caller as the consumer.
2932 int vmci_qpair_get_consume_indexes(const struct vmci_qp
*qpair
,
2936 struct vmci_queue_header
*produce_q_header
;
2937 struct vmci_queue_header
*consume_q_header
;
2941 return VMCI_ERROR_INVALID_ARGS
;
2945 qp_get_queue_headers(qpair
, &produce_q_header
, &consume_q_header
);
2946 if (result
== VMCI_SUCCESS
)
2947 vmci_q_header_get_pointers(consume_q_header
, produce_q_header
,
2948 consumer_tail
, producer_head
);
2951 if (result
== VMCI_SUCCESS
&&
2952 ((consumer_tail
&& *consumer_tail
>= qpair
->consume_q_size
) ||
2953 (producer_head
&& *producer_head
>= qpair
->consume_q_size
)))
2954 return VMCI_ERROR_INVALID_SIZE
;
2958 EXPORT_SYMBOL_GPL(vmci_qpair_get_consume_indexes
);
2961 * vmci_qpair_produce_free_space() - Retrieves free space in producer queue.
2962 * @qpair: Pointer to the queue pair struct.
2964 * This is the client interface for getting the amount of free
2965 * space in the QPair from the point of the view of the caller as
2966 * the producer which is the common case. Returns < 0 if err, else
2967 * available bytes into which data can be enqueued if > 0.
2969 s64
vmci_qpair_produce_free_space(const struct vmci_qp
*qpair
)
2971 struct vmci_queue_header
*produce_q_header
;
2972 struct vmci_queue_header
*consume_q_header
;
2976 return VMCI_ERROR_INVALID_ARGS
;
2980 qp_get_queue_headers(qpair
, &produce_q_header
, &consume_q_header
);
2981 if (result
== VMCI_SUCCESS
)
2982 result
= vmci_q_header_free_space(produce_q_header
,
2984 qpair
->produce_q_size
);
2992 EXPORT_SYMBOL_GPL(vmci_qpair_produce_free_space
);
2995 * vmci_qpair_consume_free_space() - Retrieves free space in consumer queue.
2996 * @qpair: Pointer to the queue pair struct.
2998 * This is the client interface for getting the amount of free
2999 * space in the QPair from the point of the view of the caller as
3000 * the consumer which is not the common case. Returns < 0 if err, else
3001 * available bytes into which data can be enqueued if > 0.
3003 s64
vmci_qpair_consume_free_space(const struct vmci_qp
*qpair
)
3005 struct vmci_queue_header
*produce_q_header
;
3006 struct vmci_queue_header
*consume_q_header
;
3010 return VMCI_ERROR_INVALID_ARGS
;
3014 qp_get_queue_headers(qpair
, &produce_q_header
, &consume_q_header
);
3015 if (result
== VMCI_SUCCESS
)
3016 result
= vmci_q_header_free_space(consume_q_header
,
3018 qpair
->consume_q_size
);
3026 EXPORT_SYMBOL_GPL(vmci_qpair_consume_free_space
);
3029 * vmci_qpair_produce_buf_ready() - Gets bytes ready to read from
3031 * @qpair: Pointer to the queue pair struct.
3033 * This is the client interface for getting the amount of
3034 * enqueued data in the QPair from the point of the view of the
3035 * caller as the producer which is not the common case. Returns < 0 if err,
3036 * else available bytes that may be read.
3038 s64
vmci_qpair_produce_buf_ready(const struct vmci_qp
*qpair
)
3040 struct vmci_queue_header
*produce_q_header
;
3041 struct vmci_queue_header
*consume_q_header
;
3045 return VMCI_ERROR_INVALID_ARGS
;
3049 qp_get_queue_headers(qpair
, &produce_q_header
, &consume_q_header
);
3050 if (result
== VMCI_SUCCESS
)
3051 result
= vmci_q_header_buf_ready(produce_q_header
,
3053 qpair
->produce_q_size
);
3061 EXPORT_SYMBOL_GPL(vmci_qpair_produce_buf_ready
);
3064 * vmci_qpair_consume_buf_ready() - Gets bytes ready to read from
3066 * @qpair: Pointer to the queue pair struct.
3068 * This is the client interface for getting the amount of
3069 * enqueued data in the QPair from the point of the view of the
3070 * caller as the consumer which is the normal case. Returns < 0 if err,
3071 * else available bytes that may be read.
3073 s64
vmci_qpair_consume_buf_ready(const struct vmci_qp
*qpair
)
3075 struct vmci_queue_header
*produce_q_header
;
3076 struct vmci_queue_header
*consume_q_header
;
3080 return VMCI_ERROR_INVALID_ARGS
;
3084 qp_get_queue_headers(qpair
, &produce_q_header
, &consume_q_header
);
3085 if (result
== VMCI_SUCCESS
)
3086 result
= vmci_q_header_buf_ready(consume_q_header
,
3088 qpair
->consume_q_size
);
3096 EXPORT_SYMBOL_GPL(vmci_qpair_consume_buf_ready
);
3099 * vmci_qpair_enqueue() - Throw data on the queue.
3100 * @qpair: Pointer to the queue pair struct.
3101 * @buf: Pointer to buffer containing data
3102 * @buf_size: Length of buffer.
3103 * @buf_type: Buffer type (Unused).
3105 * This is the client interface for enqueueing data into the queue.
3106 * Returns number of bytes enqueued or < 0 on error.
3108 ssize_t
vmci_qpair_enqueue(struct vmci_qp
*qpair
,
3116 return VMCI_ERROR_INVALID_ARGS
;
3121 result
= qp_enqueue_locked(qpair
->produce_q
,
3123 qpair
->produce_q_size
,
3125 qp_memcpy_to_queue
);
3127 if (result
== VMCI_ERROR_QUEUEPAIR_NOT_READY
&&
3128 !qp_wait_for_ready_queue(qpair
))
3129 result
= VMCI_ERROR_WOULD_BLOCK
;
3131 } while (result
== VMCI_ERROR_QUEUEPAIR_NOT_READY
);
3137 EXPORT_SYMBOL_GPL(vmci_qpair_enqueue
);
3140 * vmci_qpair_dequeue() - Get data from the queue.
3141 * @qpair: Pointer to the queue pair struct.
3142 * @buf: Pointer to buffer for the data
3143 * @buf_size: Length of buffer.
3144 * @buf_type: Buffer type (Unused).
3146 * This is the client interface for dequeueing data from the queue.
3147 * Returns number of bytes dequeued or < 0 on error.
3149 ssize_t
vmci_qpair_dequeue(struct vmci_qp
*qpair
,
3157 return VMCI_ERROR_INVALID_ARGS
;
3162 result
= qp_dequeue_locked(qpair
->produce_q
,
3164 qpair
->consume_q_size
,
3166 qp_memcpy_from_queue
, true);
3168 if (result
== VMCI_ERROR_QUEUEPAIR_NOT_READY
&&
3169 !qp_wait_for_ready_queue(qpair
))
3170 result
= VMCI_ERROR_WOULD_BLOCK
;
3172 } while (result
== VMCI_ERROR_QUEUEPAIR_NOT_READY
);
3178 EXPORT_SYMBOL_GPL(vmci_qpair_dequeue
);
3181 * vmci_qpair_peek() - Peek at the data in the queue.
3182 * @qpair: Pointer to the queue pair struct.
3183 * @buf: Pointer to buffer for the data
3184 * @buf_size: Length of buffer.
3185 * @buf_type: Buffer type (Unused on Linux).
3187 * This is the client interface for peeking into a queue. (I.e.,
3188 * copy data from the queue without updating the head pointer.)
3189 * Returns number of bytes dequeued or < 0 on error.
3191 ssize_t
vmci_qpair_peek(struct vmci_qp
*qpair
,
3199 return VMCI_ERROR_INVALID_ARGS
;
3204 result
= qp_dequeue_locked(qpair
->produce_q
,
3206 qpair
->consume_q_size
,
3208 qp_memcpy_from_queue
, false);
3210 if (result
== VMCI_ERROR_QUEUEPAIR_NOT_READY
&&
3211 !qp_wait_for_ready_queue(qpair
))
3212 result
= VMCI_ERROR_WOULD_BLOCK
;
3214 } while (result
== VMCI_ERROR_QUEUEPAIR_NOT_READY
);
3220 EXPORT_SYMBOL_GPL(vmci_qpair_peek
);
3223 * vmci_qpair_enquev() - Throw data on the queue using iov.
3224 * @qpair: Pointer to the queue pair struct.
3225 * @iov: Pointer to buffer containing data
3226 * @iov_size: Length of buffer.
3227 * @buf_type: Buffer type (Unused).
3229 * This is the client interface for enqueueing data into the queue.
3230 * This function uses IO vectors to handle the work. Returns number
3231 * of bytes enqueued or < 0 on error.
3233 ssize_t
vmci_qpair_enquev(struct vmci_qp
*qpair
,
3241 return VMCI_ERROR_INVALID_ARGS
;
3246 result
= qp_enqueue_locked(qpair
->produce_q
,
3248 qpair
->produce_q_size
,
3250 qp_memcpy_to_queue_iov
);
3252 if (result
== VMCI_ERROR_QUEUEPAIR_NOT_READY
&&
3253 !qp_wait_for_ready_queue(qpair
))
3254 result
= VMCI_ERROR_WOULD_BLOCK
;
3256 } while (result
== VMCI_ERROR_QUEUEPAIR_NOT_READY
);
3262 EXPORT_SYMBOL_GPL(vmci_qpair_enquev
);
3265 * vmci_qpair_dequev() - Get data from the queue using iov.
3266 * @qpair: Pointer to the queue pair struct.
3267 * @iov: Pointer to buffer for the data
3268 * @iov_size: Length of buffer.
3269 * @buf_type: Buffer type (Unused).
3271 * This is the client interface for dequeueing data from the queue.
3272 * This function uses IO vectors to handle the work. Returns number
3273 * of bytes dequeued or < 0 on error.
3275 ssize_t
vmci_qpair_dequev(struct vmci_qp
*qpair
,
3283 return VMCI_ERROR_INVALID_ARGS
;
3288 result
= qp_dequeue_locked(qpair
->produce_q
,
3290 qpair
->consume_q_size
,
3292 qp_memcpy_from_queue_iov
,
3295 if (result
== VMCI_ERROR_QUEUEPAIR_NOT_READY
&&
3296 !qp_wait_for_ready_queue(qpair
))
3297 result
= VMCI_ERROR_WOULD_BLOCK
;
3299 } while (result
== VMCI_ERROR_QUEUEPAIR_NOT_READY
);
3305 EXPORT_SYMBOL_GPL(vmci_qpair_dequev
);
3308 * vmci_qpair_peekv() - Peek at the data in the queue using iov.
3309 * @qpair: Pointer to the queue pair struct.
3310 * @iov: Pointer to buffer for the data
3311 * @iov_size: Length of buffer.
3312 * @buf_type: Buffer type (Unused on Linux).
3314 * This is the client interface for peeking into a queue. (I.e.,
3315 * copy data from the queue without updating the head pointer.)
3316 * This function uses IO vectors to handle the work. Returns number
3317 * of bytes peeked or < 0 on error.
3319 ssize_t
vmci_qpair_peekv(struct vmci_qp
*qpair
,
3327 return VMCI_ERROR_INVALID_ARGS
;
3332 result
= qp_dequeue_locked(qpair
->produce_q
,
3334 qpair
->consume_q_size
,
3336 qp_memcpy_from_queue_iov
,
3339 if (result
== VMCI_ERROR_QUEUEPAIR_NOT_READY
&&
3340 !qp_wait_for_ready_queue(qpair
))
3341 result
= VMCI_ERROR_WOULD_BLOCK
;
3343 } while (result
== VMCI_ERROR_QUEUEPAIR_NOT_READY
);
3348 EXPORT_SYMBOL_GPL(vmci_qpair_peekv
);