/* * In the following, we will distinguish between two kinds of VMX processes - * the ones with versions lower than VMCI_VERSION_NOVMVM that use specialized * VMCI page files in the VMX and supporting VM to VM communication and the * newer ones that use the guest memory directly. We will in the following * refer to the older VMX versions as old-style VMX'en, and the newer ones as * new-style VMX'en. * * The state transition datagram is as follows (the VMCIQPB_ prefix has been * removed for readability) - see below for more details on the transtions: * * -------------- NEW ------------- * | | * \_/ \_/ * CREATED_NO_MEM <-----------------> CREATED_MEM * | | | * | o-----------------------o | * | | | * \_/ \_/ \_/ * ATTACHED_NO_MEM <----------------> ATTACHED_MEM * | | | * | o----------------------o | * | | | * \_/ \_/ \_/ * SHUTDOWN_NO_MEM <----------------> SHUTDOWN_MEM * | | * | | * -------------> gone <------------- * * In more detail. When a VMCI queue pair is first created, it will be in the * VMCIQPB_NEW state. It will then move into one of the following states: * * - VMCIQPB_CREATED_NO_MEM: this state indicates that either: * * - the created was performed by a host endpoint, in which case there is * no backing memory yet. * * - the create was initiated by an old-style VMX, that uses * vmci_qp_broker_set_page_store to specify the UVAs of the queue pair at * a later point in time. This state can be distinguished from the one * above by the context ID of the creator. A host side is not allowed to * attach until the page store has been set. * * - VMCIQPB_CREATED_MEM: this state is the result when the queue pair * is created by a VMX using the queue pair device backend that * sets the UVAs of the queue pair immediately and stores the * information for later attachers. At this point, it is ready for * the host side to attach to it. * * Once the queue pair is in one of the created states (with the exception of * the case mentioned for older VMX'en above), it is possible to attach to the * queue pair. Again we have two new states possible: * * - VMCIQPB_ATTACHED_MEM: this state can be reached through the following * paths: * * - from VMCIQPB_CREATED_NO_MEM when a new-style VMX allocates a queue * pair, and attaches to a queue pair previously created by the host side. * * - from VMCIQPB_CREATED_MEM when the host side attaches to a queue pair * already created by a guest. * * - from VMCIQPB_ATTACHED_NO_MEM, when an old-style VMX calls * vmci_qp_broker_set_page_store (see below). * * - VMCIQPB_ATTACHED_NO_MEM: If the queue pair already was in the * VMCIQPB_CREATED_NO_MEM due to a host side create, an old-style VMX will * bring the queue pair into this state. Once vmci_qp_broker_set_page_store * is called to register the user memory, the VMCIQPB_ATTACH_MEM state * will be entered. * * From the attached queue pair, the queue pair can enter the shutdown states * when either side of the queue pair detaches. If the guest side detaches * first, the queue pair will enter the VMCIQPB_SHUTDOWN_NO_MEM state, where * the content of the queue pair will no longer be available. If the host * side detaches first, the queue pair will either enter the * VMCIQPB_SHUTDOWN_MEM, if the guest memory is currently mapped, or * VMCIQPB_SHUTDOWN_NO_MEM, if the guest memory is not mapped * (e.g., the host detaches while a guest is stunned). * * New-style VMX'en will also unmap guest memory, if the guest is * quiesced, e.g., during a snapshot operation. In that case, the guest * memory will no longer be available, and the queue pair will transition from * *_MEM state to a *_NO_MEM state. The VMX may later map the memory once more, * in which case the queue pair will transition from the *_NO_MEM state at that * point back to the *_MEM state. Note that the *_NO_MEM state may have changed, * since the peer may have either attached or detached in the meantime. The * values are laid out such that ++ on a state will move from a *_NO_MEM to a * *_MEM state, and vice versa.
*/
/* The Kernel specific component of the struct vmci_queue structure. */ struct vmci_queue_kern_if { struct mutex __mutex; /* Protects the queue. */ struct mutex *mutex; /* Shared by producer and consumer queues. */
size_t num_pages; /* Number of pages incl. header. */ bool host; /* Host or guest? */ union { struct {
dma_addr_t *pas; void **vas;
} g; /* Used by the guest. */ struct { struct page **page; struct page **header_page;
} h; /* Used by the host. */
} u;
};
/* * In the queue pair broker, we always use the guest point of view for * the produce and consume queue values and references, e.g., the * produce queue size stored is the guests produce queue size. The * host endpoint will need to swap these around. The only exception is * the local queue pairs on the host, in which case the host endpoint * that creates the queue pair will have the right orientation, and * the attaching host endpoint will need to swap.
*/ struct qp_entry { struct list_head list_item; struct vmci_handle handle;
u32 peer;
u32 flags;
u64 produce_size;
u64 consume_size;
u32 ref_count;
};
/* * Frees kernel VA space for a given queue and its queue header, and * frees physical data pages.
*/ staticvoid qp_free_queue(void *q, u64 size)
{ struct vmci_queue *queue = q;
if (queue) {
u64 i;
/* Given size does not include header, so add in a page here. */ for (i = 0; i < DIV_ROUND_UP(size, PAGE_SIZE) + 1; i++) {
dma_free_coherent(&vmci_pdev->dev, PAGE_SIZE,
queue->kernel_if->u.g.vas[i],
queue->kernel_if->u.g.pas[i]);
}
vfree(queue);
}
}
/* * Allocates kernel queue pages of specified size with IOMMU mappings, * plus space for the queue structure/kernel interface and the queue * header.
*/ staticvoid *qp_alloc_queue(u64 size, u32 flags)
{
u64 i; struct vmci_queue *queue;
size_t pas_size;
size_t vas_size;
size_t queue_size = sizeof(*queue) + sizeof(*queue->kernel_if);
u64 num_pages;
for (i = 0; i < num_pages; i++) {
queue->kernel_if->u.g.vas[i] =
dma_alloc_coherent(&vmci_pdev->dev, PAGE_SIZE,
&queue->kernel_if->u.g.pas[i],
GFP_KERNEL); if (!queue->kernel_if->u.g.vas[i]) { /* Size excl. the header. */
qp_free_queue(queue, i * PAGE_SIZE); return NULL;
}
}
/* Queue header is the first page. */
queue->q_header = queue->kernel_if->u.g.vas[0];
return queue;
}
/* * Copies from a given buffer or iovector to a VMCI Queue. Uses * kmap_local_page() to dynamically map required portions of the queue * by traversing the offset -> page translation structure for the queue. * Assumes that offset + size does not wrap around in the queue.
*/ staticint qp_memcpy_to_queue_iter(struct vmci_queue *queue,
u64 queue_offset, struct iov_iter *from,
size_t size)
{ struct vmci_queue_kern_if *kernel_if = queue->kernel_if;
size_t bytes_copied = 0;
if (kernel_if->host)
va = kmap_local_page(kernel_if->u.h.page[page_index]); else
va = kernel_if->u.g.vas[page_index + 1]; /* Skip header. */
if (size - bytes_copied > PAGE_SIZE - page_offset) /* Enough payload to fill up from this page. */
to_copy = PAGE_SIZE - page_offset; else
to_copy = size - bytes_copied;
if (!copy_from_iter_full((u8 *)va + page_offset, to_copy,
from)) { if (kernel_if->host)
kunmap_local(va); return VMCI_ERROR_INVALID_ARGS;
}
bytes_copied += to_copy; if (kernel_if->host)
kunmap_local(va);
}
return VMCI_SUCCESS;
}
/* * Copies to a given buffer or iovector from a VMCI Queue. Uses * kmap_local_page() to dynamically map required portions of the queue * by traversing the offset -> page translation structure for the queue. * Assumes that offset + size does not wrap around in the queue.
*/ staticint qp_memcpy_from_queue_iter(struct iov_iter *to, conststruct vmci_queue *queue,
u64 queue_offset, size_t size)
{ struct vmci_queue_kern_if *kernel_if = queue->kernel_if;
size_t bytes_copied = 0;
if (kernel_if->host)
va = kmap_local_page(kernel_if->u.h.page[page_index]); else
va = kernel_if->u.g.vas[page_index + 1]; /* Skip header. */
if (size - bytes_copied > PAGE_SIZE - page_offset) /* Enough payload to fill up this page. */
to_copy = PAGE_SIZE - page_offset; else
to_copy = size - bytes_copied;
err = copy_to_iter((u8 *)va + page_offset, to_copy, to); if (err != to_copy) { if (kernel_if->host)
kunmap_local(va); return VMCI_ERROR_INVALID_ARGS;
}
bytes_copied += to_copy; if (kernel_if->host)
kunmap_local(va);
}
return VMCI_SUCCESS;
}
/* * Allocates two list of PPNs --- one for the pages in the produce queue, * and the other for the pages in the consume queue. Intializes the list * of PPNs with the page frame numbers of the KVA for the two queues (and * the queue headers).
*/ staticint qp_alloc_ppn_set(void *prod_q,
u64 num_produce_pages, void *cons_q,
u64 num_consume_pages, struct ppn_set *ppn_set)
{
u64 *produce_ppns;
u64 *consume_ppns; struct vmci_queue *produce_q = prod_q; struct vmci_queue *consume_q = cons_q;
u64 i;
/* * Frees the two list of PPNs for a queue pair.
*/ staticvoid qp_free_ppn_set(struct ppn_set *ppn_set)
{ if (ppn_set->initialized) { /* Do not call these functions on NULL inputs. */
kfree(ppn_set->produce_ppns);
kfree(ppn_set->consume_ppns);
}
memset(ppn_set, 0, sizeof(*ppn_set));
}
/* * Populates the list of PPNs in the hypercall structure with the PPNS * of the produce queue and the consume queue.
*/ staticint qp_populate_ppn_set(u8 *call_buf, conststruct ppn_set *ppn_set)
{ if (vmci_use_ppn64()) {
memcpy(call_buf, ppn_set->produce_ppns,
ppn_set->num_produce_pages * sizeof(*ppn_set->produce_ppns));
memcpy(call_buf +
ppn_set->num_produce_pages * sizeof(*ppn_set->produce_ppns),
ppn_set->consume_ppns,
ppn_set->num_consume_pages * sizeof(*ppn_set->consume_ppns));
} else { int i;
u32 *ppns = (u32 *) call_buf;
for (i = 0; i < ppn_set->num_produce_pages; i++)
ppns[i] = (u32) ppn_set->produce_ppns[i];
ppns = &ppns[ppn_set->num_produce_pages];
for (i = 0; i < ppn_set->num_consume_pages; i++)
ppns[i] = (u32) ppn_set->consume_ppns[i];
}
return VMCI_SUCCESS;
}
/* * Allocates kernel VA space of specified size plus space for the queue * and kernel interface. This is different from the guest queue allocator, * because we do not allocate our own queue header/data pages here but * share those of the guest.
*/ staticstruct vmci_queue *qp_host_alloc_queue(u64 size)
{ struct vmci_queue *queue;
size_t queue_page_size;
u64 num_pages; const size_t queue_size = sizeof(*queue) + sizeof(*(queue->kernel_if));
/* * Frees kernel memory for a given queue (header plus translation * structure).
*/ staticvoid qp_host_free_queue(struct vmci_queue *queue, u64 queue_size)
{
kfree(queue);
}
/* * Initialize the mutex for the pair of queues. This mutex is used to * protect the q_header and the buffer from changing out from under any * users of either queue. Of course, it's only any good if the mutexes * are actually acquired. Queue structure must lie on non-paged memory * or we cannot guarantee access to the mutex.
*/ staticvoid qp_init_queue_mutex(struct vmci_queue *produce_q, struct vmci_queue *consume_q)
{ /* * Only the host queue has shared state - the guest queues do not * need to synchronize access using a queue mutex.
*/
/* * Cleans up the mutex for the pair of queues.
*/ staticvoid qp_cleanup_queue_mutex(struct vmci_queue *produce_q, struct vmci_queue *consume_q)
{ if (produce_q->kernel_if->host) {
produce_q->kernel_if->mutex = NULL;
consume_q->kernel_if->mutex = NULL;
}
}
/* * Acquire the mutex for the queue. Note that the produce_q and * the consume_q share a mutex. So, only one of the two need to * be passed in to this routine. Either will work just fine.
*/ staticvoid qp_acquire_queue_mutex(struct vmci_queue *queue)
{ if (queue->kernel_if->host)
mutex_lock(queue->kernel_if->mutex);
}
/* * Release the mutex for the queue. Note that the produce_q and * the consume_q share a mutex. So, only one of the two need to * be passed in to this routine. Either will work just fine.
*/ staticvoid qp_release_queue_mutex(struct vmci_queue *queue)
{ if (queue->kernel_if->host)
mutex_unlock(queue->kernel_if->mutex);
}
/* * Helper function to release pages in the PageStoreAttachInfo * previously obtained using get_user_pages.
*/ staticvoid qp_release_pages(struct page **pages,
u64 num_pages, bool dirty)
{ int i;
for (i = 0; i < num_pages; i++) { if (dirty)
set_page_dirty_lock(pages[i]);
put_page(pages[i]);
pages[i] = NULL;
}
}
/* * Lock the user pages referenced by the {produce,consume}Buffer * struct into memory and populate the {produce,consume}Pages * arrays in the attach structure with them.
*/ staticint qp_host_get_user_memory(u64 produce_uva,
u64 consume_uva, struct vmci_queue *produce_q, struct vmci_queue *consume_q)
{ int retval; int err = VMCI_SUCCESS;
/* * Registers the specification of the user pages used for backing a queue * pair. Enough information to map in pages is stored in the OS specific * part of the struct vmci_queue structure.
*/ staticint qp_host_register_user_memory(struct vmci_qp_page_store *page_store, struct vmci_queue *produce_q, struct vmci_queue *consume_q)
{
u64 produce_uva;
u64 consume_uva;
/* * The new style and the old style mapping only differs in * that we either get a single or two UVAs, so we split the * single UVA range at the appropriate spot.
*/
produce_uva = page_store->pages;
consume_uva = page_store->pages +
produce_q->kernel_if->num_pages * PAGE_SIZE; return qp_host_get_user_memory(produce_uva, consume_uva, produce_q,
consume_q);
}
/* * Releases and removes the references to user pages stored in the attach * struct. Pages are released from the page cache and may become * swappable again.
*/ staticvoid qp_host_unregister_user_memory(struct vmci_queue *produce_q, struct vmci_queue *consume_q)
{
qp_release_pages(produce_q->kernel_if->u.h.header_page,
produce_q->kernel_if->num_pages, true);
memset(produce_q->kernel_if->u.h.header_page, 0, sizeof(*produce_q->kernel_if->u.h.header_page) *
produce_q->kernel_if->num_pages);
qp_release_pages(consume_q->kernel_if->u.h.header_page,
consume_q->kernel_if->num_pages, true);
memset(consume_q->kernel_if->u.h.header_page, 0, sizeof(*consume_q->kernel_if->u.h.header_page) *
consume_q->kernel_if->num_pages);
}
/* * Once qp_host_register_user_memory has been performed on a * queue, the queue pair headers can be mapped into the * kernel. Once mapped, they must be unmapped with * qp_host_unmap_queues prior to calling * qp_host_unregister_user_memory. * Pages are pinned.
*/ staticint qp_host_map_queues(struct vmci_queue *produce_q, struct vmci_queue *consume_q)
{ int result;
if (!produce_q->q_header || !consume_q->q_header) { struct page *headers[2];
if (produce_q->q_header != consume_q->q_header) return VMCI_ERROR_QUEUEPAIR_MISMATCH;
if (produce_q->kernel_if->u.h.header_page == NULL ||
*produce_q->kernel_if->u.h.header_page == NULL) return VMCI_ERROR_UNAVAILABLE;
/* * Finds the entry in the list corresponding to a given handle. Assumes * that the list is locked.
*/ staticstruct qp_entry *qp_list_find(struct qp_list *qp_list, struct vmci_handle handle)
{ struct qp_entry *entry;
if (vmci_handle_is_invalid(handle)) return NULL;
list_for_each_entry(entry, &qp_list->head, list_item) { if (vmci_handle_is_equal(entry->handle, handle)) return entry;
}
return NULL;
}
/* * Finds the entry in the list corresponding to a given handle.
*/ staticstruct qp_guest_endpoint *
qp_guest_handle_to_entry(struct vmci_handle handle)
{ struct qp_guest_endpoint *entry; struct qp_entry *qp = qp_list_find(&qp_guest_endpoints, handle);
/* * Finds the entry in the list corresponding to a given handle.
*/ staticstruct qp_broker_entry *
qp_broker_handle_to_entry(struct vmci_handle handle)
{ struct qp_broker_entry *entry; struct qp_entry *qp = qp_list_find(&qp_broker_list, handle);
/* * Allocates and initializes a qp_guest_endpoint structure. * Allocates a queue_pair rid (and handle) iff the given entry has * an invalid handle. 0 through VMCI_RESERVED_RESOURCE_ID_MAX * are reserved handles. Assumes that the QP list mutex is held * by the caller.
*/ staticstruct qp_guest_endpoint *
qp_guest_endpoint_create(struct vmci_handle handle,
u32 peer,
u32 flags,
u64 produce_size,
u64 consume_size, void *produce_q, void *consume_q)
{ int result; struct qp_guest_endpoint *entry; /* One page each for the queue headers. */ const u64 num_ppns = DIV_ROUND_UP(produce_size, PAGE_SIZE) +
DIV_ROUND_UP(consume_size, PAGE_SIZE) + 2;
if (vmci_handle_is_invalid(handle)) {
u32 context_id = vmci_get_context_id();
/* * Frees a qp_guest_endpoint structure.
*/ staticvoid qp_guest_endpoint_destroy(struct qp_guest_endpoint *entry)
{
qp_free_ppn_set(&entry->ppn_set);
qp_cleanup_queue_mutex(entry->produce_q, entry->consume_q);
qp_free_queue(entry->produce_q, entry->qp.produce_size);
qp_free_queue(entry->consume_q, entry->qp.consume_size); /* Unlink from resource hash table and free callback */
vmci_resource_remove(&entry->resource);
kfree(entry);
}
/* * Helper to make a queue_pairAlloc hypercall when the driver is * supporting a guest device.
*/ staticint qp_alloc_hypercall(conststruct qp_guest_endpoint *entry)
{ struct vmci_qp_alloc_msg *alloc_msg;
size_t msg_size;
size_t ppn_size; int result;
if (!entry || entry->num_ppns <= 2) return VMCI_ERROR_INVALID_ARGS;
result = qp_populate_ppn_set((u8 *)alloc_msg + sizeof(*alloc_msg),
&entry->ppn_set); if (result == VMCI_SUCCESS)
result = vmci_send_datagram(&alloc_msg->hdr);
kfree(alloc_msg);
return result;
}
/* * Helper to make a queue_pairDetach hypercall when the driver is * supporting a guest device.
*/ staticint qp_detatch_hypercall(struct vmci_handle handle)
{ struct vmci_qp_detach_msg detach_msg;
/* * Adds the given entry to the list. Assumes that the list is locked.
*/ staticvoid qp_list_add_entry(struct qp_list *qp_list, struct qp_entry *entry)
{ if (entry)
list_add(&entry->list_item, &qp_list->head);
}
/* * Removes the given entry from the list. Assumes that the list is locked.
*/ staticvoid qp_list_remove_entry(struct qp_list *qp_list, struct qp_entry *entry)
{ if (entry)
list_del(&entry->list_item);
}
/* * Helper for VMCI queue_pair detach interface. Frees the physical * pages for the queue pair.
*/ staticint qp_detatch_guest_work(struct vmci_handle handle)
{ int result; struct qp_guest_endpoint *entry;
u32 ref_count = ~0; /* To avoid compiler warning below */
mutex_lock(&qp_guest_endpoints.mutex);
entry = qp_guest_handle_to_entry(handle); if (!entry) {
mutex_unlock(&qp_guest_endpoints.mutex); return VMCI_ERROR_NOT_FOUND;
}
if (entry->qp.flags & VMCI_QPFLAG_LOCAL) {
result = VMCI_SUCCESS;
if (entry->qp.ref_count > 1) {
result = qp_notify_peer_local(false, handle); /* * We can fail to notify a local queuepair * because we can't allocate. We still want * to release the entry if that happens, so * don't bail out yet.
*/
}
} else {
result = qp_detatch_hypercall(handle); if (result < VMCI_SUCCESS) { /* * We failed to notify a non-local queuepair. * That other queuepair might still be * accessing the shared memory, so don't * release the entry yet. It will get cleaned * up by VMCIqueue_pair_Exit() if necessary * (assuming we are going away, otherwise why * did this fail?).
*/
/* * If we get here then we either failed to notify a local queuepair, or * we succeeded in all cases. Release the entry if required.
*/
entry->qp.ref_count--; if (entry->qp.ref_count == 0)
qp_list_remove_entry(&qp_guest_endpoints, &entry->qp);
/* If we didn't remove the entry, this could change once we unlock. */ if (entry)
ref_count = entry->qp.ref_count;
mutex_unlock(&qp_guest_endpoints.mutex);
if (ref_count == 0)
qp_guest_endpoint_destroy(entry);
return result;
}
/* * This functions handles the actual allocation of a VMCI queue * pair guest endpoint. Allocates physical pages for the queue * pair. It makes OS dependent calls through generic wrappers.
*/ staticint qp_alloc_guest_work(struct vmci_handle *handle, struct vmci_queue **produce_q,
u64 produce_size, struct vmci_queue **consume_q,
u64 consume_size,
u32 peer,
u32 flags,
u32 priv_flags)
{ const u64 num_produce_pages =
DIV_ROUND_UP(produce_size, PAGE_SIZE) + 1; const u64 num_consume_pages =
DIV_ROUND_UP(consume_size, PAGE_SIZE) + 1; void *my_produce_q = NULL; void *my_consume_q = NULL; int result; struct qp_guest_endpoint *queue_pair_entry = NULL;
if (priv_flags != VMCI_NO_PRIVILEGE_FLAGS) return VMCI_ERROR_NO_ACCESS;
mutex_lock(&qp_guest_endpoints.mutex);
queue_pair_entry = qp_guest_handle_to_entry(*handle); if (queue_pair_entry) { if (queue_pair_entry->qp.flags & VMCI_QPFLAG_LOCAL) { /* Local attach case. */ if (queue_pair_entry->qp.ref_count > 1) {
pr_devel("Error attempting to attach more than once\n");
result = VMCI_ERROR_UNAVAILABLE; goto error_keep_entry;
}
if (queue_pair_entry->qp.produce_size != consume_size ||
queue_pair_entry->qp.consume_size !=
produce_size ||
queue_pair_entry->qp.flags !=
(flags & ~VMCI_QPFLAG_ATTACH_ONLY)) {
pr_devel("Error mismatched queue pair in local attach\n");
result = VMCI_ERROR_QUEUEPAIR_MISMATCH; goto error_keep_entry;
}
/* * Do a local attach. We swap the consume and * produce queues for the attacher and deliver * an attach event.
*/
result = qp_notify_peer_local(true, *handle); if (result < VMCI_SUCCESS) goto error_keep_entry;
result = VMCI_ERROR_ALREADY_EXISTS; goto error_keep_entry;
}
my_produce_q = qp_alloc_queue(produce_size, flags); if (!my_produce_q) {
pr_warn("Error allocating pages for produce queue\n");
result = VMCI_ERROR_NO_MEM; goto error;
}
my_consume_q = qp_alloc_queue(consume_size, flags); if (!my_consume_q) {
pr_warn("Error allocating pages for consume queue\n");
result = VMCI_ERROR_NO_MEM; goto error;
}
queue_pair_entry = qp_guest_endpoint_create(*handle, peer, flags,
produce_size, consume_size,
my_produce_q, my_consume_q); if (!queue_pair_entry) {
pr_warn("Error allocating memory in %s\n", __func__);
result = VMCI_ERROR_NO_MEM; goto error;
}
result = qp_alloc_ppn_set(my_produce_q, num_produce_pages, my_consume_q,
num_consume_pages,
&queue_pair_entry->ppn_set); if (result < VMCI_SUCCESS) {
pr_warn("qp_alloc_ppn_set failed\n"); goto error;
}
/* * It's only necessary to notify the host if this queue pair will be * attached to from another context.
*/ if (queue_pair_entry->qp.flags & VMCI_QPFLAG_LOCAL) { /* Local create case. */
u32 context_id = vmci_get_context_id();
/* * Enforce similar checks on local queue pairs as we * do for regular ones. The handle's context must * match the creator or attacher context id (here they * are both the current context id) and the * attach-only flag cannot exist during create. We * also ensure specified peer is this context or an * invalid one.
*/ if (queue_pair_entry->qp.handle.context != context_id ||
(queue_pair_entry->qp.peer != VMCI_INVALID_ID &&
queue_pair_entry->qp.peer != context_id)) {
result = VMCI_ERROR_NO_ACCESS; goto error;
}
if (queue_pair_entry->qp.flags & VMCI_QPFLAG_ATTACH_ONLY) {
result = VMCI_ERROR_NOT_FOUND; goto error;
}
} else {
result = qp_alloc_hypercall(queue_pair_entry); if (result < VMCI_SUCCESS) {
pr_devel("qp_alloc_hypercall result = %d\n", result); goto error;
}
}
/* * We should initialize the queue pair header pages on a local * queue pair create. For non-local queue pairs, the * hypervisor initializes the header pages in the create step.
*/ if ((queue_pair_entry->qp.flags & VMCI_QPFLAG_LOCAL) &&
queue_pair_entry->qp.ref_count == 1) {
vmci_q_header_init((*produce_q)->q_header, *handle);
vmci_q_header_init((*consume_q)->q_header, *handle);
}
mutex_unlock(&qp_guest_endpoints.mutex);
return VMCI_SUCCESS;
error:
mutex_unlock(&qp_guest_endpoints.mutex); if (queue_pair_entry) { /* The queues will be freed inside the destroy routine. */
qp_guest_endpoint_destroy(queue_pair_entry);
} else {
qp_free_queue(my_produce_q, produce_size);
qp_free_queue(my_consume_q, consume_size);
} return result;
error_keep_entry: /* This path should only be used when an existing entry was found. */
mutex_unlock(&qp_guest_endpoints.mutex); return result;
}
/* * The first endpoint issuing a queue pair allocation will create the state * of the queue pair in the queue pair broker. * * If the creator is a guest, it will associate a VMX virtual address range * with the queue pair as specified by the page_store. For compatibility with * older VMX'en, that would use a separate step to set the VMX virtual * address range, the virtual address range can be registered later using * vmci_qp_broker_set_page_store. In that case, a page_store of NULL should be * used. * * If the creator is the host, a page_store of NULL should be used as well, * since the host is not able to supply a page store for the queue pair. * * For older VMX and host callers, the queue pair will be created in the * VMCIQPB_CREATED_NO_MEM state, and for current VMX callers, it will be * created in VMCOQPB_CREATED_MEM state.
*/ staticint qp_broker_create(struct vmci_handle handle,
u32 peer,
u32 flags,
u32 priv_flags,
u64 produce_size,
u64 consume_size, struct vmci_qp_page_store *page_store, struct vmci_ctx *context,
vmci_event_release_cb wakeup_cb, void *client_data, struct qp_broker_entry **ent)
{ struct qp_broker_entry *entry = NULL; const u32 context_id = vmci_ctx_get_id(context); bool is_local = flags & VMCI_QPFLAG_LOCAL; int result;
u64 guest_produce_size;
u64 guest_consume_size;
/* Do not create if the caller asked not to. */ if (flags & VMCI_QPFLAG_ATTACH_ONLY) return VMCI_ERROR_NOT_FOUND;
/* * Creator's context ID should match handle's context ID or the creator * must allow the context in handle's context ID as the "peer".
*/ if (handle.context != context_id && handle.context != peer) return VMCI_ERROR_NO_ACCESS;
if (VMCI_CONTEXT_IS_VM(context_id) && VMCI_CONTEXT_IS_VM(peer)) return VMCI_ERROR_DST_UNREACHABLE;
/* * Creator's context ID for local queue pairs should match the * peer, if a peer is specified.
*/ if (is_local && peer != VMCI_INVALID_ID && context_id != peer) return VMCI_ERROR_NO_ACCESS;
entry = kzalloc(sizeof(*entry), GFP_ATOMIC); if (!entry) return VMCI_ERROR_NO_MEM;
if (vmci_ctx_get_id(context) == VMCI_HOST_CONTEXT_ID && !is_local) { /* * The queue pair broker entry stores values from the guest * point of view, so a creating host side endpoint should swap * produce and consume values -- unless it is a local queue * pair, in which case no swapping is necessary, since the local * attacher will swap queues.
*/
entry->local_mem = kcalloc(QPE_NUM_PAGES(entry->qp),
PAGE_SIZE, GFP_KERNEL); if (entry->local_mem == NULL) {
result = VMCI_ERROR_NO_MEM; goto error;
}
entry->state = VMCIQPB_CREATED_MEM;
entry->produce_q->q_header = entry->local_mem;
tmp = (u8 *)entry->local_mem + PAGE_SIZE *
(DIV_ROUND_UP(entry->qp.produce_size, PAGE_SIZE) + 1);
entry->consume_q->q_header = (struct vmci_queue_header *)tmp;
} elseif (page_store) { /* * The VMX already initialized the queue pair headers, so no * need for the kernel side to do that.
*/
result = qp_host_register_user_memory(page_store,
entry->produce_q,
entry->consume_q); if (result < VMCI_SUCCESS) goto error;
entry->state = VMCIQPB_CREATED_MEM;
} else { /* * A create without a page_store may be either a host * side create (in which case we are waiting for the * guest side to supply the memory) or an old style * queue pair create (in which case we will expect a * set page store call as the next step).
*/
entry->state = VMCIQPB_CREATED_NO_MEM;
}
qp_list_add_entry(&qp_broker_list, &entry->qp); if (ent != NULL)
*ent = entry;
/* Add to resource obj */
result = vmci_resource_add(&entry->resource,
VMCI_RESOURCE_TYPE_QPAIR_HOST,
handle); if (result != VMCI_SUCCESS) {
pr_warn("Failed to add new resource (handle=0x%x:0x%x), error: %d",
handle.context, handle.resource, result); goto error;
}
/* * Enqueues an event datagram to notify the peer VM attached to * the given queue pair handle about attach/detach event by the * given VM. Returns Payload size of datagram enqueued on * success, error code otherwise.
*/ staticint qp_notify_peer(bool attach, struct vmci_handle handle,
u32 my_id,
u32 peer_id)
{ int rv; struct vmci_event_qp ev;
/* * In vmci_ctx_enqueue_datagram() we enforce the upper limit on * number of pending events from the hypervisor to a given VM * otherwise a rogue VM could do an arbitrary number of attach * and detach operations causing memory pressure in the host * kernel.
*/
rv = vmci_datagram_dispatch(VMCI_HYPERVISOR_CONTEXT_ID,
&ev.msg.hdr, false); if (rv < VMCI_SUCCESS)
pr_warn("Failed to enqueue queue_pair %s event datagram for context (ID=0x%x)\n",
attach ? "ATTACH" : "DETACH", peer_id);
return rv;
}
/* * The second endpoint issuing a queue pair allocation will attach to * the queue pair registered with the queue pair broker. * * If the attacher is a guest, it will associate a VMX virtual address * range with the queue pair as specified by the page_store. At this * point, the already attach host endpoint may start using the queue * pair, and an attach event is sent to it. For compatibility with * older VMX'en, that used a separate step to set the VMX virtual * address range, the virtual address range can be registered later * using vmci_qp_broker_set_page_store. In that case, a page_store of * NULL should be used, and the attach event will be generated once * the actual page store has been set. * * If the attacher is the host, a page_store of NULL should be used as * well, since the page store information is already set by the guest. * * For new VMX and host callers, the queue pair will be moved to the * VMCIQPB_ATTACHED_MEM state, and for older VMX callers, it will be * moved to the VMCOQPB_ATTACHED_NO_MEM state.
*/ staticint qp_broker_attach(struct qp_broker_entry *entry,
u32 peer,
u32 flags,
u32 priv_flags,
u64 produce_size,
u64 consume_size, struct vmci_qp_page_store *page_store, struct vmci_ctx *context,
vmci_event_release_cb wakeup_cb, void *client_data, struct qp_broker_entry **ent)
{ const u32 context_id = vmci_ctx_get_id(context); bool is_local = flags & VMCI_QPFLAG_LOCAL; int result;
if (entry->state != VMCIQPB_CREATED_NO_MEM &&
entry->state != VMCIQPB_CREATED_MEM) return VMCI_ERROR_UNAVAILABLE;
if (VMCI_CONTEXT_IS_VM(context_id) &&
VMCI_CONTEXT_IS_VM(entry->create_id)) return VMCI_ERROR_DST_UNREACHABLE;
/* * If we are attaching from a restricted context then the queuepair * must have been created by a trusted endpoint.
*/ if ((context->priv_flags & VMCI_PRIVILEGE_FLAG_RESTRICTED) &&
!entry->created_by_trusted) return VMCI_ERROR_NO_ACCESS;
/* * If we are attaching to a queuepair that was created by a restricted * context then we must be trusted.
*/ if (entry->require_trusted_attach &&
(!(priv_flags & VMCI_PRIVILEGE_FLAG_TRUSTED))) return VMCI_ERROR_NO_ACCESS;
/* * If the creator specifies VMCI_INVALID_ID in "peer" field, access * control check is not performed.
*/ if (entry->qp.peer != VMCI_INVALID_ID && entry->qp.peer != context_id) return VMCI_ERROR_NO_ACCESS;
if (entry->create_id == VMCI_HOST_CONTEXT_ID) { /* * Do not attach if the caller doesn't support Host Queue Pairs * and a host created this queue pair.
*/
if (!vmci_ctx_supports_host_qp(context)) return VMCI_ERROR_INVALID_RESOURCE;
if (!supports_host_qp) return VMCI_ERROR_INVALID_RESOURCE;
}
if ((entry->qp.flags & ~VMCI_QP_ASYMM) != (flags & ~VMCI_QP_ASYMM_PEER)) return VMCI_ERROR_QUEUEPAIR_MISMATCH;
if (context_id != VMCI_HOST_CONTEXT_ID) { /* * The queue pair broker entry stores values from the guest * point of view, so an attaching guest should match the values * stored in the entry.
*/
if (context_id != VMCI_HOST_CONTEXT_ID) { /* * If a guest attached to a queue pair, it will supply * the backing memory. If this is a pre NOVMVM vmx, * the backing memory will be supplied by calling * vmci_qp_broker_set_page_store() following the * return of the vmci_qp_broker_alloc() call. If it is * a vmx of version NOVMVM or later, the page store * must be supplied as part of the * vmci_qp_broker_alloc call. Under all circumstances * must the initially created queue pair not have any * memory associated with it already.
*/
if (entry->state != VMCIQPB_CREATED_NO_MEM) return VMCI_ERROR_INVALID_ARGS;
if (page_store != NULL) { /* * Patch up host state to point to guest * supplied memory. The VMX already * initialized the queue pair headers, so no * need for the kernel side to do that.
*/
result = qp_host_register_user_memory(page_store,
entry->produce_q,
entry->consume_q); if (result < VMCI_SUCCESS) return result;
entry->state = VMCIQPB_ATTACHED_MEM;
} else {
entry->state = VMCIQPB_ATTACHED_NO_MEM;
}
} elseif (entry->state == VMCIQPB_CREATED_NO_MEM) { /* * The host side is attempting to attach to a queue * pair that doesn't have any memory associated with * it. This must be a pre NOVMVM vmx that hasn't set * the page store information yet, or a quiesced VM.
*/
return VMCI_ERROR_UNAVAILABLE;
} else { /* The host side has successfully attached to a queue pair. */
entry->state = VMCIQPB_ATTACHED_MEM;
}
if (entry->state == VMCIQPB_ATTACHED_MEM) {
result =
qp_notify_peer(true, entry->qp.handle, context_id,
entry->create_id); if (result < VMCI_SUCCESS)
pr_warn("Failed to notify peer (ID=0x%x) of attach to queue pair (handle=0x%x:0x%x)\n",
entry->create_id, entry->qp.handle.context,
entry->qp.handle.resource);
}
/* * When attaching to local queue pairs, the context already has * an entry tracking the queue pair, so don't add another one.
*/ if (!is_local)
vmci_ctx_qp_create(context, entry->qp.handle);
if (ent != NULL)
*ent = entry;
return VMCI_SUCCESS;
}
/* * queue_pair_Alloc for use when setting up queue pair endpoints * on the host.
*/ staticint qp_broker_alloc(struct vmci_handle handle,
u32 peer,
u32 flags,
u32 priv_flags,
u64 produce_size,
u64 consume_size, struct vmci_qp_page_store *page_store, struct vmci_ctx *context,
vmci_event_release_cb wakeup_cb, void *client_data, struct qp_broker_entry **ent, bool *swap)
{ const u32 context_id = vmci_ctx_get_id(context); bool create; struct qp_broker_entry *entry = NULL; bool is_local = flags & VMCI_QPFLAG_LOCAL; int result;
context = vmci_ctx_get(VMCI_HOST_CONTEXT_ID);
entry = NULL;
result =
qp_broker_alloc(new_handle, peer, flags, priv_flags,
produce_size, consume_size, NULL, context,
wakeup_cb, client_data, &entry, &swap); if (result == VMCI_SUCCESS) { if (swap) { /* * If this is a local queue pair, the attacher * will swap around produce and consume * queues.
*/
/* * This function implements the host kernel API for detaching from * a queue pair.
*/ staticint qp_detatch_host_work(struct vmci_handle handle)
{ int result; struct vmci_ctx *context;
context = vmci_ctx_get(VMCI_HOST_CONTEXT_ID);
result = vmci_qp_broker_detach(handle, context);
vmci_ctx_put(context); return result;
}
/* * Detaches from a VMCI queue_pair. Only checks validity of input argument. * Real work is done in the host or guest specific function.
*/ staticint qp_detatch(struct vmci_handle handle, bool guest_endpoint)
{ if (vmci_handle_is_invalid(handle)) return VMCI_ERROR_INVALID_ARGS;
if (guest_endpoint) return qp_detatch_guest_work(handle); else return qp_detatch_host_work(handle);
}
/* * Returns the entry from the head of the list. Assumes that the list is * locked.
*/ staticstruct qp_entry *qp_list_get_head(struct qp_list *qp_list)
{ if (!list_empty(&qp_list->head)) { struct qp_entry *entry =
list_first_entry(&qp_list->head, struct qp_entry,
list_item); return entry;
}
/* * Requests that a queue pair be allocated with the VMCI queue * pair broker. Allocates a queue pair entry if one does not * exist. Attaches to one if it exists, and retrieves the page * files backing that queue_pair. Assumes that the queue pair * broker lock is held.
*/ int vmci_qp_broker_alloc(struct vmci_handle handle,
u32 peer,
u32 flags,
u32 priv_flags,
u64 produce_size,
u64 consume_size, struct vmci_qp_page_store *page_store, struct vmci_ctx *context)
{ if (!QP_SIZES_ARE_VALID(produce_size, consume_size)) return VMCI_ERROR_NO_RESOURCES;
/* * VMX'en with versions lower than VMCI_VERSION_NOVMVM use a separate * step to add the UVAs of the VMX mapping of the queue pair. This function * provides backwards compatibility with such VMX'en, and takes care of * registering the page store for a queue pair previously allocated by the * VMX during create or attach. This function will move the queue pair state * to either from VMCIQBP_CREATED_NO_MEM to VMCIQBP_CREATED_MEM or * VMCIQBP_ATTACHED_NO_MEM to VMCIQBP_ATTACHED_MEM. If moving to the * attached state with memory, the queue pair is ready to be used by the * host peer, and an attached event will be generated. * * Assumes that the queue pair broker lock is held. * * This function is only used by the hosted platform, since there is no * issue with backwards compatibility for vmkernel.
*/ int vmci_qp_broker_set_page_store(struct vmci_handle handle,
u64 produce_uva,
u64 consume_uva, struct vmci_ctx *context)
{ struct qp_broker_entry *entry; int result; const u32 context_id = vmci_ctx_get_id(context);
if (vmci_handle_is_invalid(handle) || !context ||
context_id == VMCI_INVALID_ID) return VMCI_ERROR_INVALID_ARGS;
/* * We only support guest to host queue pairs, so the VMX must * supply UVAs for the mapped page files.
*/
if (produce_uva == 0 || consume_uva == 0) return VMCI_ERROR_INVALID_ARGS;
mutex_lock(&qp_broker_list.mutex);
if (!vmci_ctx_qp_exists(context, handle)) {
pr_warn("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
context_id, handle.context, handle.resource);
result = VMCI_ERROR_NOT_FOUND; goto out;
}
entry = qp_broker_handle_to_entry(handle); if (!entry) {
result = VMCI_ERROR_NOT_FOUND; goto out;
}
/* * If I'm the owner then I can set the page store. * * Or, if a host created the queue_pair and I'm the attached peer * then I can set the page store.
*/ if (entry->create_id != context_id &&
(entry->create_id != VMCI_HOST_CONTEXT_ID ||
entry->attach_id != context_id)) {
result = VMCI_ERROR_QUEUEPAIR_NOTOWNER; goto out;
}
if (entry->state != VMCIQPB_CREATED_NO_MEM &&
entry->state != VMCIQPB_ATTACHED_NO_MEM) {
result = VMCI_ERROR_UNAVAILABLE; goto out;
}
result = qp_host_get_user_memory(produce_uva, consume_uva,
entry->produce_q, entry->consume_q); if (result < VMCI_SUCCESS) goto out;
result = qp_host_map_queues(entry->produce_q, entry->consume_q); if (result < VMCI_SUCCESS) {
qp_host_unregister_user_memory(entry->produce_q,
entry->consume_q); goto out;
}
if (entry->state == VMCIQPB_ATTACHED_MEM) {
result =
qp_notify_peer(true, handle, context_id, entry->create_id); if (result < VMCI_SUCCESS) {
pr_warn("Failed to notify peer (ID=0x%x) of attach to queue pair (handle=0x%x:0x%x)\n",
entry->create_id, entry->qp.handle.context,
entry->qp.handle.resource);
}
}
result = VMCI_SUCCESS;
out:
mutex_unlock(&qp_broker_list.mutex); return result;
}
/* * Resets saved queue headers for the given QP broker * entry. Should be used when guest memory becomes available * again, or the guest detaches.
*/ staticvoid qp_reset_saved_headers(struct qp_broker_entry *entry)
{
entry->produce_q->saved_header = NULL;
entry->consume_q->saved_header = NULL;
}
/* * The main entry point for detaching from a queue pair registered with the * queue pair broker. If more than one endpoint is attached to the queue * pair, the first endpoint will mainly decrement a reference count and * generate a notification to its peer. The last endpoint will clean up * the queue pair state registered with the broker. * * When a guest endpoint detaches, it will unmap and unregister the guest * memory backing the queue pair. If the host is still attached, it will * no longer be able to access the queue pair content. * * If the queue pair is already in a state where there is no memory * registered for the queue pair (any *_NO_MEM state), it will transition to * the VMCIQPB_SHUTDOWN_NO_MEM state. This will also happen, if a guest * endpoint is the first of two endpoints to detach. If the host endpoint is * the first out of two to detach, the queue pair will move to the * VMCIQPB_SHUTDOWN_MEM state.
*/ int vmci_qp_broker_detach(struct vmci_handle handle, struct vmci_ctx *context)
{ struct qp_broker_entry *entry; const u32 context_id = vmci_ctx_get_id(context);
u32 peer_id; bool is_local = false; int result;
if (!vmci_ctx_qp_exists(context, handle)) {
pr_devel("Context (ID=0x%x) not attached to queue pair (handle=0x%x:0x%x)\n",
context_id, handle.context, handle.resource);
result = VMCI_ERROR_NOT_FOUND; goto out;
}
entry = qp_broker_handle_to_entry(handle); if (!entry) {
pr_devel("Context (ID=0x%x) reports being attached to queue pair(handle=0x%x:0x%x) that isn't present in broker\n",
context_id, handle.context, handle.resource);
result = VMCI_ERROR_NOT_FOUND; goto out;
}
if (context_id != entry->create_id && context_id != entry->attach_id) {
result = VMCI_ERROR_QUEUEPAIR_NOTATTACHED; goto out;
}
if (context_id != VMCI_HOST_CONTEXT_ID) { bool headers_mapped;
/* * Pre NOVMVM vmx'en may detach from a queue pair * before setting the page store, and in that case * there is no user memory to detach from. Also, more * recent VMX'en may detach from a queue pair in the * quiesced state.
*/
qp_acquire_queue_mutex(entry->produce_q);
headers_mapped = entry->produce_q->q_header ||
entry->consume_q->q_header; if (QPBROKERSTATE_HAS_MEM(entry)) {
result =
qp_host_unmap_queues(INVALID_VMCI_GUEST_MEM_ID,
entry->produce_q,
entry->consume_q); if (result < VMCI_SUCCESS)
pr_warn("Failed to unmap queue headers for queue pair (handle=0x%x:0x%x,result=%d)\n",
handle.context, handle.resource,
result);
if (!is_local)
vmci_ctx_qp_destroy(context, handle);
}
result = VMCI_SUCCESS;
out:
mutex_unlock(&qp_broker_list.mutex); return result;
}
/* * Establishes the necessary mappings for a queue pair given a * reference to the queue pair guest memory. This is usually * called when a guest is unquiesced and the VMX is allowed to * map guest memory once again.
*/ int vmci_qp_broker_map(struct vmci_handle handle, struct vmci_ctx *context,
u64 guest_mem)
{ struct qp_broker_entry *entry; const u32 context_id = vmci_ctx_get_id(context); int result;
if (vmci_handle_is_invalid(handle) || !context ||
context_id == VMCI_INVALID_ID) return VMCI_ERROR_INVALID_ARGS;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.