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>
20 #include <linux/module.h>
21 #include <linux/sched.h>
22 #include <linux/slab.h>
24 #include "vmci_queue_pair.h"
25 #include "vmci_datagram.h"
26 #include "vmci_doorbell.h"
27 #include "vmci_context.h"
28 #include "vmci_driver.h"
29 #include "vmci_event.h"
31 /* Use a wide upper bound for the maximum contexts. */
32 #define VMCI_MAX_CONTEXTS 2000
35 * List of current VMCI contexts. Contexts can be added by
36 * vmci_ctx_create() and removed via vmci_ctx_destroy().
37 * These, along with context lookup, are protected by the
38 * list structure's lock.
41 struct list_head head;
42 spinlock_t lock; /* Spinlock for context list operations */
44 .head = LIST_HEAD_INIT(ctx_list.head),
45 .lock = __SPIN_LOCK_UNLOCKED(ctx_list.lock),
48 /* Used by contexts that did not set up notify flag pointers */
49 static bool ctx_dummy_notify;
51 static void ctx_signal_notify(struct vmci_ctx *context)
53 *context->notify = true;
56 static void ctx_clear_notify(struct vmci_ctx *context)
58 *context->notify = false;
62 * If nothing requires the attention of the guest, clears both
63 * notify flag and call.
65 static void ctx_clear_notify_call(struct vmci_ctx *context)
67 if (context->pending_datagrams == 0 &&
68 vmci_handle_arr_get_size(context->pending_doorbell_array) == 0)
69 ctx_clear_notify(context);
73 * Sets the context's notify flag iff datagrams are pending for this
74 * context. Called from vmci_setup_notify().
76 void vmci_ctx_check_signal_notify(struct vmci_ctx *context)
78 spin_lock(&context->lock);
79 if (context->pending_datagrams)
80 ctx_signal_notify(context);
81 spin_unlock(&context->lock);
85 * Allocates and initializes a VMCI context.
87 struct vmci_ctx *vmci_ctx_create(u32 cid, u32 priv_flags,
90 const struct cred *cred)
92 struct vmci_ctx *context;
95 if (cid == VMCI_INVALID_ID) {
96 pr_devel("Invalid context ID for VMCI context\n");
101 if (priv_flags & ~VMCI_PRIVILEGE_ALL_FLAGS) {
102 pr_devel("Invalid flag (flags=0x%x) for VMCI context\n",
108 if (user_version == 0) {
109 pr_devel("Invalid suer_version %d\n", user_version);
114 context = kzalloc(sizeof(*context), GFP_KERNEL);
116 pr_warn("Failed to allocate memory for VMCI context\n");
121 kref_init(&context->kref);
122 spin_lock_init(&context->lock);
123 INIT_LIST_HEAD(&context->list_item);
124 INIT_LIST_HEAD(&context->datagram_queue);
125 INIT_LIST_HEAD(&context->notifier_list);
127 /* Initialize host-specific VMCI context. */
128 init_waitqueue_head(&context->host_context.wait_queue);
130 context->queue_pair_array =
131 vmci_handle_arr_create(0, VMCI_MAX_GUEST_QP_COUNT);
132 if (!context->queue_pair_array) {
137 context->doorbell_array =
138 vmci_handle_arr_create(0, VMCI_MAX_GUEST_DOORBELL_COUNT);
139 if (!context->doorbell_array) {
141 goto err_free_qp_array;
144 context->pending_doorbell_array =
145 vmci_handle_arr_create(0, VMCI_MAX_GUEST_DOORBELL_COUNT);
146 if (!context->pending_doorbell_array) {
148 goto err_free_db_array;
151 context->user_version = user_version;
153 context->priv_flags = priv_flags;
156 context->cred = get_cred(cred);
158 context->notify = &ctx_dummy_notify;
159 context->notify_page = NULL;
162 * If we collide with an existing context we generate a new
163 * and use it instead. The VMX will determine if regeneration
164 * is okay. Since there isn't 4B - 16 VMs running on a given
165 * host, the below loop will terminate.
167 spin_lock(&ctx_list.lock);
169 while (vmci_ctx_exists(cid)) {
170 /* We reserve the lowest 16 ids for fixed contexts. */
171 cid = max(cid, VMCI_RESERVED_CID_LIMIT - 1) + 1;
172 if (cid == VMCI_INVALID_ID)
173 cid = VMCI_RESERVED_CID_LIMIT;
177 list_add_tail_rcu(&context->list_item, &ctx_list.head);
178 spin_unlock(&ctx_list.lock);
183 vmci_handle_arr_destroy(context->doorbell_array);
185 vmci_handle_arr_destroy(context->queue_pair_array);
189 return ERR_PTR(error);
193 * Destroy VMCI context.
195 void vmci_ctx_destroy(struct vmci_ctx *context)
197 spin_lock(&ctx_list.lock);
198 list_del_rcu(&context->list_item);
199 spin_unlock(&ctx_list.lock);
202 vmci_ctx_put(context);
206 * Fire notification for all contexts interested in given cid.
208 static int ctx_fire_notification(u32 context_id, u32 priv_flags)
211 struct vmci_ctx *sub_ctx;
212 struct vmci_handle_arr *subscriber_array;
213 struct vmci_handle context_handle =
214 vmci_make_handle(context_id, VMCI_EVENT_HANDLER);
217 * We create an array to hold the subscribers we find when
218 * scanning through all contexts.
220 subscriber_array = vmci_handle_arr_create(0, VMCI_MAX_CONTEXTS);
221 if (subscriber_array == NULL)
222 return VMCI_ERROR_NO_MEM;
225 * Scan all contexts to find who is interested in being
226 * notified about given contextID.
229 list_for_each_entry_rcu(sub_ctx, &ctx_list.head, list_item) {
230 struct vmci_handle_list *node;
233 * We only deliver notifications of the removal of
234 * contexts, if the two contexts are allowed to
237 if (vmci_deny_interaction(priv_flags, sub_ctx->priv_flags))
240 list_for_each_entry_rcu(node, &sub_ctx->notifier_list, node) {
241 if (!vmci_handle_is_equal(node->handle, context_handle))
244 vmci_handle_arr_append_entry(&subscriber_array,
245 vmci_make_handle(sub_ctx->cid,
246 VMCI_EVENT_HANDLER));
251 /* Fire event to all subscribers. */
252 array_size = vmci_handle_arr_get_size(subscriber_array);
253 for (i = 0; i < array_size; i++) {
255 struct vmci_event_ctx ev;
257 ev.msg.hdr.dst = vmci_handle_arr_get_entry(subscriber_array, i);
258 ev.msg.hdr.src = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
259 VMCI_CONTEXT_RESOURCE_ID);
260 ev.msg.hdr.payload_size = sizeof(ev) - sizeof(ev.msg.hdr);
261 ev.msg.event_data.event = VMCI_EVENT_CTX_REMOVED;
262 ev.payload.context_id = context_id;
264 result = vmci_datagram_dispatch(VMCI_HYPERVISOR_CONTEXT_ID,
266 if (result < VMCI_SUCCESS) {
267 pr_devel("Failed to enqueue event datagram (type=%d) for context (ID=0x%x)\n",
268 ev.msg.event_data.event,
269 ev.msg.hdr.dst.context);
270 /* We continue to enqueue on next subscriber. */
273 vmci_handle_arr_destroy(subscriber_array);
279 * Returns the current number of pending datagrams. The call may
280 * also serve as a synchronization point for the datagram queue,
281 * as no enqueue operations can occur concurrently.
283 int vmci_ctx_pending_datagrams(u32 cid, u32 *pending)
285 struct vmci_ctx *context;
287 context = vmci_ctx_get(cid);
289 return VMCI_ERROR_INVALID_ARGS;
291 spin_lock(&context->lock);
293 *pending = context->pending_datagrams;
294 spin_unlock(&context->lock);
295 vmci_ctx_put(context);
301 * Queues a VMCI datagram for the appropriate target VM context.
303 int vmci_ctx_enqueue_datagram(u32 cid, struct vmci_datagram *dg)
305 struct vmci_datagram_queue_entry *dq_entry;
306 struct vmci_ctx *context;
307 struct vmci_handle dg_src;
310 vmci_dg_size = VMCI_DG_SIZE(dg);
311 if (vmci_dg_size > VMCI_MAX_DG_SIZE) {
312 pr_devel("Datagram too large (bytes=%Zu)\n", vmci_dg_size);
313 return VMCI_ERROR_INVALID_ARGS;
316 /* Get the target VM's VMCI context. */
317 context = vmci_ctx_get(cid);
319 pr_devel("Invalid context (ID=0x%x)\n", cid);
320 return VMCI_ERROR_INVALID_ARGS;
323 /* Allocate guest call entry and add it to the target VM's queue. */
324 dq_entry = kmalloc(sizeof(*dq_entry), GFP_KERNEL);
325 if (dq_entry == NULL) {
326 pr_warn("Failed to allocate memory for datagram\n");
327 vmci_ctx_put(context);
328 return VMCI_ERROR_NO_MEM;
331 dq_entry->dg_size = vmci_dg_size;
333 INIT_LIST_HEAD(&dq_entry->list_item);
335 spin_lock(&context->lock);
338 * We put a higher limit on datagrams from the hypervisor. If
339 * the pending datagram is not from hypervisor, then we check
340 * if enqueueing it would exceed the
341 * VMCI_MAX_DATAGRAM_QUEUE_SIZE limit on the destination. If
342 * the pending datagram is from hypervisor, we allow it to be
343 * queued at the destination side provided we don't reach the
344 * VMCI_MAX_DATAGRAM_AND_EVENT_QUEUE_SIZE limit.
346 if (context->datagram_queue_size + vmci_dg_size >=
347 VMCI_MAX_DATAGRAM_QUEUE_SIZE &&
348 (!vmci_handle_is_equal(dg_src,
350 (VMCI_HYPERVISOR_CONTEXT_ID,
351 VMCI_CONTEXT_RESOURCE_ID)) ||
352 context->datagram_queue_size + vmci_dg_size >=
353 VMCI_MAX_DATAGRAM_AND_EVENT_QUEUE_SIZE)) {
354 spin_unlock(&context->lock);
355 vmci_ctx_put(context);
357 pr_devel("Context (ID=0x%x) receive queue is full\n", cid);
358 return VMCI_ERROR_NO_RESOURCES;
361 list_add(&dq_entry->list_item, &context->datagram_queue);
362 context->pending_datagrams++;
363 context->datagram_queue_size += vmci_dg_size;
364 ctx_signal_notify(context);
365 wake_up(&context->host_context.wait_queue);
366 spin_unlock(&context->lock);
367 vmci_ctx_put(context);
373 * Verifies whether a context with the specified context ID exists.
374 * FIXME: utility is dubious as no decisions can be reliably made
375 * using this data as context can appear and disappear at any time.
377 bool vmci_ctx_exists(u32 cid)
379 struct vmci_ctx *context;
384 list_for_each_entry_rcu(context, &ctx_list.head, list_item) {
385 if (context->cid == cid) {
396 * Retrieves VMCI context corresponding to the given cid.
398 struct vmci_ctx *vmci_ctx_get(u32 cid)
400 struct vmci_ctx *c, *context = NULL;
402 if (cid == VMCI_INVALID_ID)
406 list_for_each_entry_rcu(c, &ctx_list.head, list_item) {
409 * The context owner drops its own reference to the
410 * context only after removing it from the list and
411 * waiting for RCU grace period to expire. This
412 * means that we are not about to increase the
413 * reference count of something that is in the
414 * process of being destroyed.
417 kref_get(&context->kref);
427 * Deallocates all parts of a context data structure. This
428 * function doesn't lock the context, because it assumes that
429 * the caller was holding the last reference to context.
431 static void ctx_free_ctx(struct kref *kref)
433 struct vmci_ctx *context = container_of(kref, struct vmci_ctx, kref);
434 struct vmci_datagram_queue_entry *dq_entry, *dq_entry_tmp;
435 struct vmci_handle temp_handle;
436 struct vmci_handle_list *notifier, *tmp;
439 * Fire event to all contexts interested in knowing this
442 ctx_fire_notification(context->cid, context->priv_flags);
445 * Cleanup all queue pair resources attached to context. If
446 * the VM dies without cleaning up, this code will make sure
447 * that no resources are leaked.
449 temp_handle = vmci_handle_arr_get_entry(context->queue_pair_array, 0);
450 while (!vmci_handle_is_equal(temp_handle, VMCI_INVALID_HANDLE)) {
451 if (vmci_qp_broker_detach(temp_handle,
452 context) < VMCI_SUCCESS) {
454 * When vmci_qp_broker_detach() succeeds it
455 * removes the handle from the array. If
456 * detach fails, we must remove the handle
459 vmci_handle_arr_remove_entry(context->queue_pair_array,
463 vmci_handle_arr_get_entry(context->queue_pair_array, 0);
467 * It is fine to destroy this without locking the callQueue, as
468 * this is the only thread having a reference to the context.
470 list_for_each_entry_safe(dq_entry, dq_entry_tmp,
471 &context->datagram_queue, list_item) {
472 WARN_ON(dq_entry->dg_size != VMCI_DG_SIZE(dq_entry->dg));
473 list_del(&dq_entry->list_item);
478 list_for_each_entry_safe(notifier, tmp,
479 &context->notifier_list, node) {
480 list_del(¬ifier->node);
484 vmci_handle_arr_destroy(context->queue_pair_array);
485 vmci_handle_arr_destroy(context->doorbell_array);
486 vmci_handle_arr_destroy(context->pending_doorbell_array);
487 vmci_ctx_unset_notify(context);
489 put_cred(context->cred);
494 * Drops reference to VMCI context. If this is the last reference to
495 * the context it will be deallocated. A context is created with
496 * a reference count of one, and on destroy, it is removed from
497 * the context list before its reference count is decremented. Thus,
498 * if we reach zero, we are sure that nobody else are about to increment
499 * it (they need the entry in the context list for that), and so there
500 * is no need for locking.
502 void vmci_ctx_put(struct vmci_ctx *context)
504 kref_put(&context->kref, ctx_free_ctx);
508 * Dequeues the next datagram and returns it to caller.
509 * The caller passes in a pointer to the max size datagram
510 * it can handle and the datagram is only unqueued if the
511 * size is less than max_size. If larger max_size is set to
512 * the size of the datagram to give the caller a chance to
513 * set up a larger buffer for the guestcall.
515 int vmci_ctx_dequeue_datagram(struct vmci_ctx *context,
517 struct vmci_datagram **dg)
519 struct vmci_datagram_queue_entry *dq_entry;
520 struct list_head *list_item;
523 /* Dequeue the next datagram entry. */
524 spin_lock(&context->lock);
525 if (context->pending_datagrams == 0) {
526 ctx_clear_notify_call(context);
527 spin_unlock(&context->lock);
528 pr_devel("No datagrams pending\n");
529 return VMCI_ERROR_NO_MORE_DATAGRAMS;
532 list_item = context->datagram_queue.next;
535 list_entry(list_item, struct vmci_datagram_queue_entry, list_item);
537 /* Check size of caller's buffer. */
538 if (*max_size < dq_entry->dg_size) {
539 *max_size = dq_entry->dg_size;
540 spin_unlock(&context->lock);
541 pr_devel("Caller's buffer should be at least (size=%u bytes)\n",
543 return VMCI_ERROR_NO_MEM;
547 context->pending_datagrams--;
548 context->datagram_queue_size -= dq_entry->dg_size;
549 if (context->pending_datagrams == 0) {
550 ctx_clear_notify_call(context);
554 * Return the size of the next datagram.
556 struct vmci_datagram_queue_entry *next_entry;
558 list_item = context->datagram_queue.next;
560 list_entry(list_item, struct vmci_datagram_queue_entry,
564 * The following size_t -> int truncation is fine as
565 * the maximum size of a (routable) datagram is 68KB.
567 rv = (int)next_entry->dg_size;
569 spin_unlock(&context->lock);
571 /* Caller must free datagram. */
580 * Reverts actions set up by vmci_setup_notify(). Unmaps and unlocks the
581 * page mapped/locked by vmci_setup_notify().
583 void vmci_ctx_unset_notify(struct vmci_ctx *context)
585 struct page *notify_page;
587 spin_lock(&context->lock);
589 notify_page = context->notify_page;
590 context->notify = &ctx_dummy_notify;
591 context->notify_page = NULL;
593 spin_unlock(&context->lock);
597 put_page(notify_page);
602 * Add remote_cid to list of contexts current contexts wants
603 * notifications from/about.
605 int vmci_ctx_add_notification(u32 context_id, u32 remote_cid)
607 struct vmci_ctx *context;
608 struct vmci_handle_list *notifier, *n;
612 context = vmci_ctx_get(context_id);
614 return VMCI_ERROR_NOT_FOUND;
616 if (VMCI_CONTEXT_IS_VM(context_id) && VMCI_CONTEXT_IS_VM(remote_cid)) {
617 pr_devel("Context removed notifications for other VMs not supported (src=0x%x, remote=0x%x)\n",
618 context_id, remote_cid);
619 result = VMCI_ERROR_DST_UNREACHABLE;
623 if (context->priv_flags & VMCI_PRIVILEGE_FLAG_RESTRICTED) {
624 result = VMCI_ERROR_NO_ACCESS;
628 notifier = kmalloc(sizeof(struct vmci_handle_list), GFP_KERNEL);
630 result = VMCI_ERROR_NO_MEM;
634 INIT_LIST_HEAD(¬ifier->node);
635 notifier->handle = vmci_make_handle(remote_cid, VMCI_EVENT_HANDLER);
637 spin_lock(&context->lock);
639 if (context->n_notifiers < VMCI_MAX_CONTEXTS) {
640 list_for_each_entry(n, &context->notifier_list, node) {
641 if (vmci_handle_is_equal(n->handle, notifier->handle)) {
649 result = VMCI_ERROR_ALREADY_EXISTS;
651 list_add_tail_rcu(¬ifier->node,
652 &context->notifier_list);
653 context->n_notifiers++;
654 result = VMCI_SUCCESS;
658 result = VMCI_ERROR_NO_MEM;
661 spin_unlock(&context->lock);
664 vmci_ctx_put(context);
669 * Remove remote_cid from current context's list of contexts it is
670 * interested in getting notifications from/about.
672 int vmci_ctx_remove_notification(u32 context_id, u32 remote_cid)
674 struct vmci_ctx *context;
675 struct vmci_handle_list *notifier, *tmp;
676 struct vmci_handle handle;
679 context = vmci_ctx_get(context_id);
681 return VMCI_ERROR_NOT_FOUND;
683 handle = vmci_make_handle(remote_cid, VMCI_EVENT_HANDLER);
685 spin_lock(&context->lock);
686 list_for_each_entry_safe(notifier, tmp,
687 &context->notifier_list, node) {
688 if (vmci_handle_is_equal(notifier->handle, handle)) {
689 list_del_rcu(¬ifier->node);
690 context->n_notifiers--;
695 spin_unlock(&context->lock);
702 vmci_ctx_put(context);
704 return found ? VMCI_SUCCESS : VMCI_ERROR_NOT_FOUND;
707 static int vmci_ctx_get_chkpt_notifiers(struct vmci_ctx *context,
708 u32 *buf_size, void **pbuf)
712 struct vmci_handle_list *entry;
715 if (context->n_notifiers == 0) {
721 data_size = context->n_notifiers * sizeof(*notifiers);
722 if (*buf_size < data_size) {
723 *buf_size = data_size;
724 return VMCI_ERROR_MORE_DATA;
727 notifiers = kmalloc(data_size, GFP_ATOMIC); /* FIXME: want GFP_KERNEL */
729 return VMCI_ERROR_NO_MEM;
731 list_for_each_entry(entry, &context->notifier_list, node)
732 notifiers[i++] = entry->handle.context;
734 *buf_size = data_size;
739 static int vmci_ctx_get_chkpt_doorbells(struct vmci_ctx *context,
740 u32 *buf_size, void **pbuf)
742 struct dbell_cpt_state *dbells;
745 n_doorbells = vmci_handle_arr_get_size(context->doorbell_array);
746 if (n_doorbells > 0) {
747 size_t data_size = n_doorbells * sizeof(*dbells);
748 if (*buf_size < data_size) {
749 *buf_size = data_size;
750 return VMCI_ERROR_MORE_DATA;
753 dbells = kzalloc(data_size, GFP_ATOMIC);
755 return VMCI_ERROR_NO_MEM;
757 for (i = 0; i < n_doorbells; i++)
758 dbells[i].handle = vmci_handle_arr_get_entry(
759 context->doorbell_array, i);
761 *buf_size = data_size;
772 * Get current context's checkpoint state of given type.
774 int vmci_ctx_get_chkpt_state(u32 context_id,
779 struct vmci_ctx *context;
782 context = vmci_ctx_get(context_id);
784 return VMCI_ERROR_NOT_FOUND;
786 spin_lock(&context->lock);
789 case VMCI_NOTIFICATION_CPT_STATE:
790 result = vmci_ctx_get_chkpt_notifiers(context, buf_size, pbuf);
793 case VMCI_WELLKNOWN_CPT_STATE:
795 * For compatibility with VMX'en with VM to VM communication, we
796 * always return zero wellknown handles.
801 result = VMCI_SUCCESS;
804 case VMCI_DOORBELL_CPT_STATE:
805 result = vmci_ctx_get_chkpt_doorbells(context, buf_size, pbuf);
809 pr_devel("Invalid cpt state (type=%d)\n", cpt_type);
810 result = VMCI_ERROR_INVALID_ARGS;
814 spin_unlock(&context->lock);
815 vmci_ctx_put(context);
821 * Set current context's checkpoint state of given type.
823 int vmci_ctx_set_chkpt_state(u32 context_id,
830 int result = VMCI_SUCCESS;
831 u32 num_ids = buf_size / sizeof(u32);
833 if (cpt_type == VMCI_WELLKNOWN_CPT_STATE && num_ids > 0) {
835 * We would end up here if VMX with VM to VM communication
836 * attempts to restore a checkpoint with wellknown handles.
838 pr_warn("Attempt to restore checkpoint with obsolete wellknown handles\n");
839 return VMCI_ERROR_OBSOLETE;
842 if (cpt_type != VMCI_NOTIFICATION_CPT_STATE) {
843 pr_devel("Invalid cpt state (type=%d)\n", cpt_type);
844 return VMCI_ERROR_INVALID_ARGS;
847 for (i = 0; i < num_ids && result == VMCI_SUCCESS; i++) {
848 current_id = ((u32 *)cpt_buf)[i];
849 result = vmci_ctx_add_notification(context_id, current_id);
850 if (result != VMCI_SUCCESS)
853 if (result != VMCI_SUCCESS)
854 pr_devel("Failed to set cpt state (type=%d) (error=%d)\n",
861 * Retrieves the specified context's pending notifications in the
862 * form of a handle array. The handle arrays returned are the
863 * actual data - not a copy and should not be modified by the
864 * caller. They must be released using
865 * vmci_ctx_rcv_notifications_release.
867 int vmci_ctx_rcv_notifications_get(u32 context_id,
868 struct vmci_handle_arr **db_handle_array,
869 struct vmci_handle_arr **qp_handle_array)
871 struct vmci_ctx *context;
872 int result = VMCI_SUCCESS;
874 context = vmci_ctx_get(context_id);
876 return VMCI_ERROR_NOT_FOUND;
878 spin_lock(&context->lock);
880 *db_handle_array = context->pending_doorbell_array;
881 context->pending_doorbell_array =
882 vmci_handle_arr_create(0, VMCI_MAX_GUEST_DOORBELL_COUNT);
883 if (!context->pending_doorbell_array) {
884 context->pending_doorbell_array = *db_handle_array;
885 *db_handle_array = NULL;
886 result = VMCI_ERROR_NO_MEM;
888 *qp_handle_array = NULL;
890 spin_unlock(&context->lock);
891 vmci_ctx_put(context);
897 * Releases handle arrays with pending notifications previously
898 * retrieved using vmci_ctx_rcv_notifications_get. If the
899 * notifications were not successfully handed over to the guest,
900 * success must be false.
902 void vmci_ctx_rcv_notifications_release(u32 context_id,
903 struct vmci_handle_arr *db_handle_array,
904 struct vmci_handle_arr *qp_handle_array,
907 struct vmci_ctx *context = vmci_ctx_get(context_id);
909 spin_lock(&context->lock);
911 struct vmci_handle handle;
914 * New notifications may have been added while we were not
915 * holding the context lock, so we transfer any new pending
916 * doorbell notifications to the old array, and reinstate the
920 handle = vmci_handle_arr_remove_tail(
921 context->pending_doorbell_array);
922 while (!vmci_handle_is_invalid(handle)) {
923 if (!vmci_handle_arr_has_entry(db_handle_array,
925 vmci_handle_arr_append_entry(
926 &db_handle_array, handle);
928 handle = vmci_handle_arr_remove_tail(
929 context->pending_doorbell_array);
931 vmci_handle_arr_destroy(context->pending_doorbell_array);
932 context->pending_doorbell_array = db_handle_array;
933 db_handle_array = NULL;
935 ctx_clear_notify_call(context);
937 spin_unlock(&context->lock);
938 vmci_ctx_put(context);
941 vmci_handle_arr_destroy(db_handle_array);
944 vmci_handle_arr_destroy(qp_handle_array);
948 * Registers that a new doorbell handle has been allocated by the
949 * context. Only doorbell handles registered can be notified.
951 int vmci_ctx_dbell_create(u32 context_id, struct vmci_handle handle)
953 struct vmci_ctx *context;
956 if (context_id == VMCI_INVALID_ID || vmci_handle_is_invalid(handle))
957 return VMCI_ERROR_INVALID_ARGS;
959 context = vmci_ctx_get(context_id);
961 return VMCI_ERROR_NOT_FOUND;
963 spin_lock(&context->lock);
964 if (!vmci_handle_arr_has_entry(context->doorbell_array, handle))
965 result = vmci_handle_arr_append_entry(&context->doorbell_array,
968 result = VMCI_ERROR_DUPLICATE_ENTRY;
970 spin_unlock(&context->lock);
971 vmci_ctx_put(context);
977 * Unregisters a doorbell handle that was previously registered
978 * with vmci_ctx_dbell_create.
980 int vmci_ctx_dbell_destroy(u32 context_id, struct vmci_handle handle)
982 struct vmci_ctx *context;
983 struct vmci_handle removed_handle;
985 if (context_id == VMCI_INVALID_ID || vmci_handle_is_invalid(handle))
986 return VMCI_ERROR_INVALID_ARGS;
988 context = vmci_ctx_get(context_id);
990 return VMCI_ERROR_NOT_FOUND;
992 spin_lock(&context->lock);
994 vmci_handle_arr_remove_entry(context->doorbell_array, handle);
995 vmci_handle_arr_remove_entry(context->pending_doorbell_array, handle);
996 spin_unlock(&context->lock);
998 vmci_ctx_put(context);
1000 return vmci_handle_is_invalid(removed_handle) ?
1001 VMCI_ERROR_NOT_FOUND : VMCI_SUCCESS;
1005 * Unregisters all doorbell handles that were previously
1006 * registered with vmci_ctx_dbell_create.
1008 int vmci_ctx_dbell_destroy_all(u32 context_id)
1010 struct vmci_ctx *context;
1011 struct vmci_handle handle;
1013 if (context_id == VMCI_INVALID_ID)
1014 return VMCI_ERROR_INVALID_ARGS;
1016 context = vmci_ctx_get(context_id);
1017 if (context == NULL)
1018 return VMCI_ERROR_NOT_FOUND;
1020 spin_lock(&context->lock);
1022 struct vmci_handle_arr *arr = context->doorbell_array;
1023 handle = vmci_handle_arr_remove_tail(arr);
1024 } while (!vmci_handle_is_invalid(handle));
1026 struct vmci_handle_arr *arr = context->pending_doorbell_array;
1027 handle = vmci_handle_arr_remove_tail(arr);
1028 } while (!vmci_handle_is_invalid(handle));
1029 spin_unlock(&context->lock);
1031 vmci_ctx_put(context);
1033 return VMCI_SUCCESS;
1037 * Registers a notification of a doorbell handle initiated by the
1038 * specified source context. The notification of doorbells are
1039 * subject to the same isolation rules as datagram delivery. To
1040 * allow host side senders of notifications a finer granularity
1041 * of sender rights than those assigned to the sending context
1042 * itself, the host context is required to specify a different
1043 * set of privilege flags that will override the privileges of
1044 * the source context.
1046 int vmci_ctx_notify_dbell(u32 src_cid,
1047 struct vmci_handle handle,
1050 struct vmci_ctx *dst_context;
1053 if (vmci_handle_is_invalid(handle))
1054 return VMCI_ERROR_INVALID_ARGS;
1056 /* Get the target VM's VMCI context. */
1057 dst_context = vmci_ctx_get(handle.context);
1059 pr_devel("Invalid context (ID=0x%x)\n", handle.context);
1060 return VMCI_ERROR_NOT_FOUND;
1063 if (src_cid != handle.context) {
1066 if (VMCI_CONTEXT_IS_VM(src_cid) &&
1067 VMCI_CONTEXT_IS_VM(handle.context)) {
1068 pr_devel("Doorbell notification from VM to VM not supported (src=0x%x, dst=0x%x)\n",
1069 src_cid, handle.context);
1070 result = VMCI_ERROR_DST_UNREACHABLE;
1074 result = vmci_dbell_get_priv_flags(handle, &dst_priv_flags);
1075 if (result < VMCI_SUCCESS) {
1076 pr_warn("Failed to get privilege flags for destination (handle=0x%x:0x%x)\n",
1077 handle.context, handle.resource);
1081 if (src_cid != VMCI_HOST_CONTEXT_ID ||
1082 src_priv_flags == VMCI_NO_PRIVILEGE_FLAGS) {
1083 src_priv_flags = vmci_context_get_priv_flags(src_cid);
1086 if (vmci_deny_interaction(src_priv_flags, dst_priv_flags)) {
1087 result = VMCI_ERROR_NO_ACCESS;
1092 if (handle.context == VMCI_HOST_CONTEXT_ID) {
1093 result = vmci_dbell_host_context_notify(src_cid, handle);
1095 spin_lock(&dst_context->lock);
1097 if (!vmci_handle_arr_has_entry(dst_context->doorbell_array,
1099 result = VMCI_ERROR_NOT_FOUND;
1101 if (!vmci_handle_arr_has_entry(
1102 dst_context->pending_doorbell_array,
1104 result = vmci_handle_arr_append_entry(
1105 &dst_context->pending_doorbell_array,
1107 if (result == VMCI_SUCCESS) {
1108 ctx_signal_notify(dst_context);
1109 wake_up(&dst_context->host_context.wait_queue);
1112 result = VMCI_SUCCESS;
1115 spin_unlock(&dst_context->lock);
1119 vmci_ctx_put(dst_context);
1124 bool vmci_ctx_supports_host_qp(struct vmci_ctx *context)
1126 return context && context->user_version >= VMCI_VERSION_HOSTQP;
1130 * Registers that a new queue pair handle has been allocated by
1133 int vmci_ctx_qp_create(struct vmci_ctx *context, struct vmci_handle handle)
1137 if (context == NULL || vmci_handle_is_invalid(handle))
1138 return VMCI_ERROR_INVALID_ARGS;
1140 if (!vmci_handle_arr_has_entry(context->queue_pair_array, handle))
1141 result = vmci_handle_arr_append_entry(
1142 &context->queue_pair_array, handle);
1144 result = VMCI_ERROR_DUPLICATE_ENTRY;
1150 * Unregisters a queue pair handle that was previously registered
1151 * with vmci_ctx_qp_create.
1153 int vmci_ctx_qp_destroy(struct vmci_ctx *context, struct vmci_handle handle)
1155 struct vmci_handle hndl;
1157 if (context == NULL || vmci_handle_is_invalid(handle))
1158 return VMCI_ERROR_INVALID_ARGS;
1160 hndl = vmci_handle_arr_remove_entry(context->queue_pair_array, handle);
1162 return vmci_handle_is_invalid(hndl) ?
1163 VMCI_ERROR_NOT_FOUND : VMCI_SUCCESS;
1167 * Determines whether a given queue pair handle is registered
1168 * with the given context.
1170 bool vmci_ctx_qp_exists(struct vmci_ctx *context, struct vmci_handle handle)
1172 if (context == NULL || vmci_handle_is_invalid(handle))
1175 return vmci_handle_arr_has_entry(context->queue_pair_array, handle);
1179 * vmci_context_get_priv_flags() - Retrieve privilege flags.
1180 * @context_id: The context ID of the VMCI context.
1182 * Retrieves privilege flags of the given VMCI context ID.
1184 u32 vmci_context_get_priv_flags(u32 context_id)
1186 if (vmci_host_code_active()) {
1188 struct vmci_ctx *context;
1190 context = vmci_ctx_get(context_id);
1192 return VMCI_LEAST_PRIVILEGE_FLAGS;
1194 flags = context->priv_flags;
1195 vmci_ctx_put(context);
1198 return VMCI_NO_PRIVILEGE_FLAGS;
1200 EXPORT_SYMBOL_GPL(vmci_context_get_priv_flags);
1203 * vmci_is_context_owner() - Determimnes if user is the context owner
1204 * @context_id: The context ID of the VMCI context.
1205 * @uid: The host user id (real kernel value).
1207 * Determines whether a given UID is the owner of given VMCI context.
1209 bool vmci_is_context_owner(u32 context_id, kuid_t uid)
1211 bool is_owner = false;
1213 if (vmci_host_code_active()) {
1214 struct vmci_ctx *context = vmci_ctx_get(context_id);
1217 is_owner = uid_eq(context->cred->uid, uid);
1218 vmci_ctx_put(context);
1224 EXPORT_SYMBOL_GPL(vmci_is_context_owner);