1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2012 VMware, Inc. All rights reserved.
8 #include <linux/vmw_vmci_defs.h>
9 #include <linux/vmw_vmci_api.h>
10 #include <linux/highmem.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/cred.h>
15 #include <linux/slab.h>
17 #include "vmci_queue_pair.h"
18 #include "vmci_datagram.h"
19 #include "vmci_doorbell.h"
20 #include "vmci_context.h"
21 #include "vmci_driver.h"
22 #include "vmci_event.h"
24 /* Use a wide upper bound for the maximum contexts. */
25 #define VMCI_MAX_CONTEXTS 2000
28 * List of current VMCI contexts. Contexts can be added by
29 * vmci_ctx_create() and removed via vmci_ctx_destroy().
30 * These, along with context lookup, are protected by the
31 * list structure's lock.
34 struct list_head head
;
35 spinlock_t lock
; /* Spinlock for context list operations */
37 .head
= LIST_HEAD_INIT(ctx_list
.head
),
38 .lock
= __SPIN_LOCK_UNLOCKED(ctx_list
.lock
),
41 /* Used by contexts that did not set up notify flag pointers */
42 static bool ctx_dummy_notify
;
44 static void ctx_signal_notify(struct vmci_ctx
*context
)
46 *context
->notify
= true;
49 static void ctx_clear_notify(struct vmci_ctx
*context
)
51 *context
->notify
= false;
55 * If nothing requires the attention of the guest, clears both
56 * notify flag and call.
58 static void ctx_clear_notify_call(struct vmci_ctx
*context
)
60 if (context
->pending_datagrams
== 0 &&
61 vmci_handle_arr_get_size(context
->pending_doorbell_array
) == 0)
62 ctx_clear_notify(context
);
66 * Sets the context's notify flag iff datagrams are pending for this
67 * context. Called from vmci_setup_notify().
69 void vmci_ctx_check_signal_notify(struct vmci_ctx
*context
)
71 spin_lock(&context
->lock
);
72 if (context
->pending_datagrams
)
73 ctx_signal_notify(context
);
74 spin_unlock(&context
->lock
);
78 * Allocates and initializes a VMCI context.
80 struct vmci_ctx
*vmci_ctx_create(u32 cid
, u32 priv_flags
,
83 const struct cred
*cred
)
85 struct vmci_ctx
*context
;
88 if (cid
== VMCI_INVALID_ID
) {
89 pr_devel("Invalid context ID for VMCI context\n");
94 if (priv_flags
& ~VMCI_PRIVILEGE_ALL_FLAGS
) {
95 pr_devel("Invalid flag (flags=0x%x) for VMCI context\n",
101 if (user_version
== 0) {
102 pr_devel("Invalid suer_version %d\n", user_version
);
107 context
= kzalloc(sizeof(*context
), GFP_KERNEL
);
109 pr_warn("Failed to allocate memory for VMCI context\n");
114 kref_init(&context
->kref
);
115 spin_lock_init(&context
->lock
);
116 INIT_LIST_HEAD(&context
->list_item
);
117 INIT_LIST_HEAD(&context
->datagram_queue
);
118 INIT_LIST_HEAD(&context
->notifier_list
);
120 /* Initialize host-specific VMCI context. */
121 init_waitqueue_head(&context
->host_context
.wait_queue
);
123 context
->queue_pair_array
=
124 vmci_handle_arr_create(0, VMCI_MAX_GUEST_QP_COUNT
);
125 if (!context
->queue_pair_array
) {
130 context
->doorbell_array
=
131 vmci_handle_arr_create(0, VMCI_MAX_GUEST_DOORBELL_COUNT
);
132 if (!context
->doorbell_array
) {
134 goto err_free_qp_array
;
137 context
->pending_doorbell_array
=
138 vmci_handle_arr_create(0, VMCI_MAX_GUEST_DOORBELL_COUNT
);
139 if (!context
->pending_doorbell_array
) {
141 goto err_free_db_array
;
144 context
->user_version
= user_version
;
146 context
->priv_flags
= priv_flags
;
149 context
->cred
= get_cred(cred
);
151 context
->notify
= &ctx_dummy_notify
;
152 context
->notify_page
= NULL
;
155 * If we collide with an existing context we generate a new
156 * and use it instead. The VMX will determine if regeneration
157 * is okay. Since there isn't 4B - 16 VMs running on a given
158 * host, the below loop will terminate.
160 spin_lock(&ctx_list
.lock
);
162 while (vmci_ctx_exists(cid
)) {
163 /* We reserve the lowest 16 ids for fixed contexts. */
164 cid
= max(cid
, VMCI_RESERVED_CID_LIMIT
- 1) + 1;
165 if (cid
== VMCI_INVALID_ID
)
166 cid
= VMCI_RESERVED_CID_LIMIT
;
170 list_add_tail_rcu(&context
->list_item
, &ctx_list
.head
);
171 spin_unlock(&ctx_list
.lock
);
176 vmci_handle_arr_destroy(context
->doorbell_array
);
178 vmci_handle_arr_destroy(context
->queue_pair_array
);
182 return ERR_PTR(error
);
186 * Destroy VMCI context.
188 void vmci_ctx_destroy(struct vmci_ctx
*context
)
190 spin_lock(&ctx_list
.lock
);
191 list_del_rcu(&context
->list_item
);
192 spin_unlock(&ctx_list
.lock
);
195 vmci_ctx_put(context
);
199 * Fire notification for all contexts interested in given cid.
201 static int ctx_fire_notification(u32 context_id
, u32 priv_flags
)
204 struct vmci_ctx
*sub_ctx
;
205 struct vmci_handle_arr
*subscriber_array
;
206 struct vmci_handle context_handle
=
207 vmci_make_handle(context_id
, VMCI_EVENT_HANDLER
);
210 * We create an array to hold the subscribers we find when
211 * scanning through all contexts.
213 subscriber_array
= vmci_handle_arr_create(0, VMCI_MAX_CONTEXTS
);
214 if (subscriber_array
== NULL
)
215 return VMCI_ERROR_NO_MEM
;
218 * Scan all contexts to find who is interested in being
219 * notified about given contextID.
222 list_for_each_entry_rcu(sub_ctx
, &ctx_list
.head
, list_item
) {
223 struct vmci_handle_list
*node
;
226 * We only deliver notifications of the removal of
227 * contexts, if the two contexts are allowed to
230 if (vmci_deny_interaction(priv_flags
, sub_ctx
->priv_flags
))
233 list_for_each_entry_rcu(node
, &sub_ctx
->notifier_list
, node
) {
234 if (!vmci_handle_is_equal(node
->handle
, context_handle
))
237 vmci_handle_arr_append_entry(&subscriber_array
,
238 vmci_make_handle(sub_ctx
->cid
,
239 VMCI_EVENT_HANDLER
));
244 /* Fire event to all subscribers. */
245 array_size
= vmci_handle_arr_get_size(subscriber_array
);
246 for (i
= 0; i
< array_size
; i
++) {
248 struct vmci_event_ctx ev
;
250 ev
.msg
.hdr
.dst
= vmci_handle_arr_get_entry(subscriber_array
, i
);
251 ev
.msg
.hdr
.src
= vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID
,
252 VMCI_CONTEXT_RESOURCE_ID
);
253 ev
.msg
.hdr
.payload_size
= sizeof(ev
) - sizeof(ev
.msg
.hdr
);
254 ev
.msg
.event_data
.event
= VMCI_EVENT_CTX_REMOVED
;
255 ev
.payload
.context_id
= context_id
;
257 result
= vmci_datagram_dispatch(VMCI_HYPERVISOR_CONTEXT_ID
,
259 if (result
< VMCI_SUCCESS
) {
260 pr_devel("Failed to enqueue event datagram (type=%d) for context (ID=0x%x)\n",
261 ev
.msg
.event_data
.event
,
262 ev
.msg
.hdr
.dst
.context
);
263 /* We continue to enqueue on next subscriber. */
266 vmci_handle_arr_destroy(subscriber_array
);
272 * Returns the current number of pending datagrams. The call may
273 * also serve as a synchronization point for the datagram queue,
274 * as no enqueue operations can occur concurrently.
276 int vmci_ctx_pending_datagrams(u32 cid
, u32
*pending
)
278 struct vmci_ctx
*context
;
280 context
= vmci_ctx_get(cid
);
282 return VMCI_ERROR_INVALID_ARGS
;
284 spin_lock(&context
->lock
);
286 *pending
= context
->pending_datagrams
;
287 spin_unlock(&context
->lock
);
288 vmci_ctx_put(context
);
294 * Queues a VMCI datagram for the appropriate target VM context.
296 int vmci_ctx_enqueue_datagram(u32 cid
, struct vmci_datagram
*dg
)
298 struct vmci_datagram_queue_entry
*dq_entry
;
299 struct vmci_ctx
*context
;
300 struct vmci_handle dg_src
;
303 vmci_dg_size
= VMCI_DG_SIZE(dg
);
304 if (vmci_dg_size
> VMCI_MAX_DG_SIZE
) {
305 pr_devel("Datagram too large (bytes=%zu)\n", vmci_dg_size
);
306 return VMCI_ERROR_INVALID_ARGS
;
309 /* Get the target VM's VMCI context. */
310 context
= vmci_ctx_get(cid
);
312 pr_devel("Invalid context (ID=0x%x)\n", cid
);
313 return VMCI_ERROR_INVALID_ARGS
;
316 /* Allocate guest call entry and add it to the target VM's queue. */
317 dq_entry
= kmalloc(sizeof(*dq_entry
), GFP_KERNEL
);
318 if (dq_entry
== NULL
) {
319 pr_warn("Failed to allocate memory for datagram\n");
320 vmci_ctx_put(context
);
321 return VMCI_ERROR_NO_MEM
;
324 dq_entry
->dg_size
= vmci_dg_size
;
326 INIT_LIST_HEAD(&dq_entry
->list_item
);
328 spin_lock(&context
->lock
);
331 * We put a higher limit on datagrams from the hypervisor. If
332 * the pending datagram is not from hypervisor, then we check
333 * if enqueueing it would exceed the
334 * VMCI_MAX_DATAGRAM_QUEUE_SIZE limit on the destination. If
335 * the pending datagram is from hypervisor, we allow it to be
336 * queued at the destination side provided we don't reach the
337 * VMCI_MAX_DATAGRAM_AND_EVENT_QUEUE_SIZE limit.
339 if (context
->datagram_queue_size
+ vmci_dg_size
>=
340 VMCI_MAX_DATAGRAM_QUEUE_SIZE
&&
341 (!vmci_handle_is_equal(dg_src
,
343 (VMCI_HYPERVISOR_CONTEXT_ID
,
344 VMCI_CONTEXT_RESOURCE_ID
)) ||
345 context
->datagram_queue_size
+ vmci_dg_size
>=
346 VMCI_MAX_DATAGRAM_AND_EVENT_QUEUE_SIZE
)) {
347 spin_unlock(&context
->lock
);
348 vmci_ctx_put(context
);
350 pr_devel("Context (ID=0x%x) receive queue is full\n", cid
);
351 return VMCI_ERROR_NO_RESOURCES
;
354 list_add(&dq_entry
->list_item
, &context
->datagram_queue
);
355 context
->pending_datagrams
++;
356 context
->datagram_queue_size
+= vmci_dg_size
;
357 ctx_signal_notify(context
);
358 wake_up(&context
->host_context
.wait_queue
);
359 spin_unlock(&context
->lock
);
360 vmci_ctx_put(context
);
366 * Verifies whether a context with the specified context ID exists.
367 * FIXME: utility is dubious as no decisions can be reliably made
368 * using this data as context can appear and disappear at any time.
370 bool vmci_ctx_exists(u32 cid
)
372 struct vmci_ctx
*context
;
377 list_for_each_entry_rcu(context
, &ctx_list
.head
, list_item
) {
378 if (context
->cid
== cid
) {
389 * Retrieves VMCI context corresponding to the given cid.
391 struct vmci_ctx
*vmci_ctx_get(u32 cid
)
393 struct vmci_ctx
*c
, *context
= NULL
;
395 if (cid
== VMCI_INVALID_ID
)
399 list_for_each_entry_rcu(c
, &ctx_list
.head
, list_item
) {
402 * The context owner drops its own reference to the
403 * context only after removing it from the list and
404 * waiting for RCU grace period to expire. This
405 * means that we are not about to increase the
406 * reference count of something that is in the
407 * process of being destroyed.
410 kref_get(&context
->kref
);
420 * Deallocates all parts of a context data structure. This
421 * function doesn't lock the context, because it assumes that
422 * the caller was holding the last reference to context.
424 static void ctx_free_ctx(struct kref
*kref
)
426 struct vmci_ctx
*context
= container_of(kref
, struct vmci_ctx
, kref
);
427 struct vmci_datagram_queue_entry
*dq_entry
, *dq_entry_tmp
;
428 struct vmci_handle temp_handle
;
429 struct vmci_handle_list
*notifier
, *tmp
;
432 * Fire event to all contexts interested in knowing this
435 ctx_fire_notification(context
->cid
, context
->priv_flags
);
438 * Cleanup all queue pair resources attached to context. If
439 * the VM dies without cleaning up, this code will make sure
440 * that no resources are leaked.
442 temp_handle
= vmci_handle_arr_get_entry(context
->queue_pair_array
, 0);
443 while (!vmci_handle_is_equal(temp_handle
, VMCI_INVALID_HANDLE
)) {
444 if (vmci_qp_broker_detach(temp_handle
,
445 context
) < VMCI_SUCCESS
) {
447 * When vmci_qp_broker_detach() succeeds it
448 * removes the handle from the array. If
449 * detach fails, we must remove the handle
452 vmci_handle_arr_remove_entry(context
->queue_pair_array
,
456 vmci_handle_arr_get_entry(context
->queue_pair_array
, 0);
460 * It is fine to destroy this without locking the callQueue, as
461 * this is the only thread having a reference to the context.
463 list_for_each_entry_safe(dq_entry
, dq_entry_tmp
,
464 &context
->datagram_queue
, list_item
) {
465 WARN_ON(dq_entry
->dg_size
!= VMCI_DG_SIZE(dq_entry
->dg
));
466 list_del(&dq_entry
->list_item
);
471 list_for_each_entry_safe(notifier
, tmp
,
472 &context
->notifier_list
, node
) {
473 list_del(¬ifier
->node
);
477 vmci_handle_arr_destroy(context
->queue_pair_array
);
478 vmci_handle_arr_destroy(context
->doorbell_array
);
479 vmci_handle_arr_destroy(context
->pending_doorbell_array
);
480 vmci_ctx_unset_notify(context
);
482 put_cred(context
->cred
);
487 * Drops reference to VMCI context. If this is the last reference to
488 * the context it will be deallocated. A context is created with
489 * a reference count of one, and on destroy, it is removed from
490 * the context list before its reference count is decremented. Thus,
491 * if we reach zero, we are sure that nobody else are about to increment
492 * it (they need the entry in the context list for that), and so there
493 * is no need for locking.
495 void vmci_ctx_put(struct vmci_ctx
*context
)
497 kref_put(&context
->kref
, ctx_free_ctx
);
501 * Dequeues the next datagram and returns it to caller.
502 * The caller passes in a pointer to the max size datagram
503 * it can handle and the datagram is only unqueued if the
504 * size is less than max_size. If larger max_size is set to
505 * the size of the datagram to give the caller a chance to
506 * set up a larger buffer for the guestcall.
508 int vmci_ctx_dequeue_datagram(struct vmci_ctx
*context
,
510 struct vmci_datagram
**dg
)
512 struct vmci_datagram_queue_entry
*dq_entry
;
513 struct list_head
*list_item
;
516 /* Dequeue the next datagram entry. */
517 spin_lock(&context
->lock
);
518 if (context
->pending_datagrams
== 0) {
519 ctx_clear_notify_call(context
);
520 spin_unlock(&context
->lock
);
521 pr_devel("No datagrams pending\n");
522 return VMCI_ERROR_NO_MORE_DATAGRAMS
;
525 list_item
= context
->datagram_queue
.next
;
528 list_entry(list_item
, struct vmci_datagram_queue_entry
, list_item
);
530 /* Check size of caller's buffer. */
531 if (*max_size
< dq_entry
->dg_size
) {
532 *max_size
= dq_entry
->dg_size
;
533 spin_unlock(&context
->lock
);
534 pr_devel("Caller's buffer should be at least (size=%u bytes)\n",
536 return VMCI_ERROR_NO_MEM
;
540 context
->pending_datagrams
--;
541 context
->datagram_queue_size
-= dq_entry
->dg_size
;
542 if (context
->pending_datagrams
== 0) {
543 ctx_clear_notify_call(context
);
547 * Return the size of the next datagram.
549 struct vmci_datagram_queue_entry
*next_entry
;
551 list_item
= context
->datagram_queue
.next
;
553 list_entry(list_item
, struct vmci_datagram_queue_entry
,
557 * The following size_t -> int truncation is fine as
558 * the maximum size of a (routable) datagram is 68KB.
560 rv
= (int)next_entry
->dg_size
;
562 spin_unlock(&context
->lock
);
564 /* Caller must free datagram. */
573 * Reverts actions set up by vmci_setup_notify(). Unmaps and unlocks the
574 * page mapped/locked by vmci_setup_notify().
576 void vmci_ctx_unset_notify(struct vmci_ctx
*context
)
578 struct page
*notify_page
;
580 spin_lock(&context
->lock
);
582 notify_page
= context
->notify_page
;
583 context
->notify
= &ctx_dummy_notify
;
584 context
->notify_page
= NULL
;
586 spin_unlock(&context
->lock
);
590 put_page(notify_page
);
595 * Add remote_cid to list of contexts current contexts wants
596 * notifications from/about.
598 int vmci_ctx_add_notification(u32 context_id
, u32 remote_cid
)
600 struct vmci_ctx
*context
;
601 struct vmci_handle_list
*notifier
, *n
;
605 context
= vmci_ctx_get(context_id
);
607 return VMCI_ERROR_NOT_FOUND
;
609 if (VMCI_CONTEXT_IS_VM(context_id
) && VMCI_CONTEXT_IS_VM(remote_cid
)) {
610 pr_devel("Context removed notifications for other VMs not supported (src=0x%x, remote=0x%x)\n",
611 context_id
, remote_cid
);
612 result
= VMCI_ERROR_DST_UNREACHABLE
;
616 if (context
->priv_flags
& VMCI_PRIVILEGE_FLAG_RESTRICTED
) {
617 result
= VMCI_ERROR_NO_ACCESS
;
621 notifier
= kmalloc(sizeof(struct vmci_handle_list
), GFP_KERNEL
);
623 result
= VMCI_ERROR_NO_MEM
;
627 INIT_LIST_HEAD(¬ifier
->node
);
628 notifier
->handle
= vmci_make_handle(remote_cid
, VMCI_EVENT_HANDLER
);
630 spin_lock(&context
->lock
);
632 if (context
->n_notifiers
< VMCI_MAX_CONTEXTS
) {
633 list_for_each_entry(n
, &context
->notifier_list
, node
) {
634 if (vmci_handle_is_equal(n
->handle
, notifier
->handle
)) {
642 result
= VMCI_ERROR_ALREADY_EXISTS
;
644 list_add_tail_rcu(¬ifier
->node
,
645 &context
->notifier_list
);
646 context
->n_notifiers
++;
647 result
= VMCI_SUCCESS
;
651 result
= VMCI_ERROR_NO_MEM
;
654 spin_unlock(&context
->lock
);
657 vmci_ctx_put(context
);
662 * Remove remote_cid from current context's list of contexts it is
663 * interested in getting notifications from/about.
665 int vmci_ctx_remove_notification(u32 context_id
, u32 remote_cid
)
667 struct vmci_ctx
*context
;
668 struct vmci_handle_list
*notifier
= NULL
, *iter
, *tmp
;
669 struct vmci_handle handle
;
671 context
= vmci_ctx_get(context_id
);
673 return VMCI_ERROR_NOT_FOUND
;
675 handle
= vmci_make_handle(remote_cid
, VMCI_EVENT_HANDLER
);
677 spin_lock(&context
->lock
);
678 list_for_each_entry_safe(iter
, tmp
,
679 &context
->notifier_list
, node
) {
680 if (vmci_handle_is_equal(iter
->handle
, handle
)) {
681 list_del_rcu(&iter
->node
);
682 context
->n_notifiers
--;
687 spin_unlock(&context
->lock
);
690 kvfree_rcu_mightsleep(notifier
);
692 vmci_ctx_put(context
);
694 return notifier
? VMCI_SUCCESS
: VMCI_ERROR_NOT_FOUND
;
697 static int vmci_ctx_get_chkpt_notifiers(struct vmci_ctx
*context
,
698 u32
*buf_size
, void **pbuf
)
702 struct vmci_handle_list
*entry
;
705 if (context
->n_notifiers
== 0) {
711 data_size
= context
->n_notifiers
* sizeof(*notifiers
);
712 if (*buf_size
< data_size
) {
713 *buf_size
= data_size
;
714 return VMCI_ERROR_MORE_DATA
;
717 notifiers
= kmalloc(data_size
, GFP_ATOMIC
); /* FIXME: want GFP_KERNEL */
719 return VMCI_ERROR_NO_MEM
;
721 list_for_each_entry(entry
, &context
->notifier_list
, node
)
722 notifiers
[i
++] = entry
->handle
.context
;
724 *buf_size
= data_size
;
729 static int vmci_ctx_get_chkpt_doorbells(struct vmci_ctx
*context
,
730 u32
*buf_size
, void **pbuf
)
732 struct dbell_cpt_state
*dbells
;
735 n_doorbells
= vmci_handle_arr_get_size(context
->doorbell_array
);
736 if (n_doorbells
> 0) {
737 size_t data_size
= n_doorbells
* sizeof(*dbells
);
738 if (*buf_size
< data_size
) {
739 *buf_size
= data_size
;
740 return VMCI_ERROR_MORE_DATA
;
743 dbells
= kzalloc(data_size
, GFP_ATOMIC
);
745 return VMCI_ERROR_NO_MEM
;
747 for (i
= 0; i
< n_doorbells
; i
++)
748 dbells
[i
].handle
= vmci_handle_arr_get_entry(
749 context
->doorbell_array
, i
);
751 *buf_size
= data_size
;
762 * Get current context's checkpoint state of given type.
764 int vmci_ctx_get_chkpt_state(u32 context_id
,
769 struct vmci_ctx
*context
;
772 context
= vmci_ctx_get(context_id
);
774 return VMCI_ERROR_NOT_FOUND
;
776 spin_lock(&context
->lock
);
779 case VMCI_NOTIFICATION_CPT_STATE
:
780 result
= vmci_ctx_get_chkpt_notifiers(context
, buf_size
, pbuf
);
783 case VMCI_WELLKNOWN_CPT_STATE
:
785 * For compatibility with VMX'en with VM to VM communication, we
786 * always return zero wellknown handles.
791 result
= VMCI_SUCCESS
;
794 case VMCI_DOORBELL_CPT_STATE
:
795 result
= vmci_ctx_get_chkpt_doorbells(context
, buf_size
, pbuf
);
799 pr_devel("Invalid cpt state (type=%d)\n", cpt_type
);
800 result
= VMCI_ERROR_INVALID_ARGS
;
804 spin_unlock(&context
->lock
);
805 vmci_ctx_put(context
);
811 * Set current context's checkpoint state of given type.
813 int vmci_ctx_set_chkpt_state(u32 context_id
,
820 int result
= VMCI_SUCCESS
;
821 u32 num_ids
= buf_size
/ sizeof(u32
);
823 if (cpt_type
== VMCI_WELLKNOWN_CPT_STATE
&& num_ids
> 0) {
825 * We would end up here if VMX with VM to VM communication
826 * attempts to restore a checkpoint with wellknown handles.
828 pr_warn("Attempt to restore checkpoint with obsolete wellknown handles\n");
829 return VMCI_ERROR_OBSOLETE
;
832 if (cpt_type
!= VMCI_NOTIFICATION_CPT_STATE
) {
833 pr_devel("Invalid cpt state (type=%d)\n", cpt_type
);
834 return VMCI_ERROR_INVALID_ARGS
;
837 for (i
= 0; i
< num_ids
&& result
== VMCI_SUCCESS
; i
++) {
838 current_id
= ((u32
*)cpt_buf
)[i
];
839 result
= vmci_ctx_add_notification(context_id
, current_id
);
840 if (result
!= VMCI_SUCCESS
)
843 if (result
!= VMCI_SUCCESS
)
844 pr_devel("Failed to set cpt state (type=%d) (error=%d)\n",
851 * Retrieves the specified context's pending notifications in the
852 * form of a handle array. The handle arrays returned are the
853 * actual data - not a copy and should not be modified by the
854 * caller. They must be released using
855 * vmci_ctx_rcv_notifications_release.
857 int vmci_ctx_rcv_notifications_get(u32 context_id
,
858 struct vmci_handle_arr
**db_handle_array
,
859 struct vmci_handle_arr
**qp_handle_array
)
861 struct vmci_ctx
*context
;
862 int result
= VMCI_SUCCESS
;
864 context
= vmci_ctx_get(context_id
);
866 return VMCI_ERROR_NOT_FOUND
;
868 spin_lock(&context
->lock
);
870 *db_handle_array
= context
->pending_doorbell_array
;
871 context
->pending_doorbell_array
=
872 vmci_handle_arr_create(0, VMCI_MAX_GUEST_DOORBELL_COUNT
);
873 if (!context
->pending_doorbell_array
) {
874 context
->pending_doorbell_array
= *db_handle_array
;
875 *db_handle_array
= NULL
;
876 result
= VMCI_ERROR_NO_MEM
;
878 *qp_handle_array
= NULL
;
880 spin_unlock(&context
->lock
);
881 vmci_ctx_put(context
);
887 * Releases handle arrays with pending notifications previously
888 * retrieved using vmci_ctx_rcv_notifications_get. If the
889 * notifications were not successfully handed over to the guest,
890 * success must be false.
892 void vmci_ctx_rcv_notifications_release(u32 context_id
,
893 struct vmci_handle_arr
*db_handle_array
,
894 struct vmci_handle_arr
*qp_handle_array
,
897 struct vmci_ctx
*context
= vmci_ctx_get(context_id
);
899 spin_lock(&context
->lock
);
901 struct vmci_handle handle
;
904 * New notifications may have been added while we were not
905 * holding the context lock, so we transfer any new pending
906 * doorbell notifications to the old array, and reinstate the
910 handle
= vmci_handle_arr_remove_tail(
911 context
->pending_doorbell_array
);
912 while (!vmci_handle_is_invalid(handle
)) {
913 if (!vmci_handle_arr_has_entry(db_handle_array
,
915 vmci_handle_arr_append_entry(
916 &db_handle_array
, handle
);
918 handle
= vmci_handle_arr_remove_tail(
919 context
->pending_doorbell_array
);
921 vmci_handle_arr_destroy(context
->pending_doorbell_array
);
922 context
->pending_doorbell_array
= db_handle_array
;
923 db_handle_array
= NULL
;
925 ctx_clear_notify_call(context
);
927 spin_unlock(&context
->lock
);
928 vmci_ctx_put(context
);
931 vmci_handle_arr_destroy(db_handle_array
);
934 vmci_handle_arr_destroy(qp_handle_array
);
938 * Registers that a new doorbell handle has been allocated by the
939 * context. Only doorbell handles registered can be notified.
941 int vmci_ctx_dbell_create(u32 context_id
, struct vmci_handle handle
)
943 struct vmci_ctx
*context
;
946 if (context_id
== VMCI_INVALID_ID
|| vmci_handle_is_invalid(handle
))
947 return VMCI_ERROR_INVALID_ARGS
;
949 context
= vmci_ctx_get(context_id
);
951 return VMCI_ERROR_NOT_FOUND
;
953 spin_lock(&context
->lock
);
954 if (!vmci_handle_arr_has_entry(context
->doorbell_array
, handle
))
955 result
= vmci_handle_arr_append_entry(&context
->doorbell_array
,
958 result
= VMCI_ERROR_DUPLICATE_ENTRY
;
960 spin_unlock(&context
->lock
);
961 vmci_ctx_put(context
);
967 * Unregisters a doorbell handle that was previously registered
968 * with vmci_ctx_dbell_create.
970 int vmci_ctx_dbell_destroy(u32 context_id
, struct vmci_handle handle
)
972 struct vmci_ctx
*context
;
973 struct vmci_handle removed_handle
;
975 if (context_id
== VMCI_INVALID_ID
|| vmci_handle_is_invalid(handle
))
976 return VMCI_ERROR_INVALID_ARGS
;
978 context
= vmci_ctx_get(context_id
);
980 return VMCI_ERROR_NOT_FOUND
;
982 spin_lock(&context
->lock
);
984 vmci_handle_arr_remove_entry(context
->doorbell_array
, handle
);
985 vmci_handle_arr_remove_entry(context
->pending_doorbell_array
, handle
);
986 spin_unlock(&context
->lock
);
988 vmci_ctx_put(context
);
990 return vmci_handle_is_invalid(removed_handle
) ?
991 VMCI_ERROR_NOT_FOUND
: VMCI_SUCCESS
;
995 * Unregisters all doorbell handles that were previously
996 * registered with vmci_ctx_dbell_create.
998 int vmci_ctx_dbell_destroy_all(u32 context_id
)
1000 struct vmci_ctx
*context
;
1001 struct vmci_handle handle
;
1003 if (context_id
== VMCI_INVALID_ID
)
1004 return VMCI_ERROR_INVALID_ARGS
;
1006 context
= vmci_ctx_get(context_id
);
1007 if (context
== NULL
)
1008 return VMCI_ERROR_NOT_FOUND
;
1010 spin_lock(&context
->lock
);
1012 struct vmci_handle_arr
*arr
= context
->doorbell_array
;
1013 handle
= vmci_handle_arr_remove_tail(arr
);
1014 } while (!vmci_handle_is_invalid(handle
));
1016 struct vmci_handle_arr
*arr
= context
->pending_doorbell_array
;
1017 handle
= vmci_handle_arr_remove_tail(arr
);
1018 } while (!vmci_handle_is_invalid(handle
));
1019 spin_unlock(&context
->lock
);
1021 vmci_ctx_put(context
);
1023 return VMCI_SUCCESS
;
1027 * Registers a notification of a doorbell handle initiated by the
1028 * specified source context. The notification of doorbells are
1029 * subject to the same isolation rules as datagram delivery. To
1030 * allow host side senders of notifications a finer granularity
1031 * of sender rights than those assigned to the sending context
1032 * itself, the host context is required to specify a different
1033 * set of privilege flags that will override the privileges of
1034 * the source context.
1036 int vmci_ctx_notify_dbell(u32 src_cid
,
1037 struct vmci_handle handle
,
1040 struct vmci_ctx
*dst_context
;
1043 if (vmci_handle_is_invalid(handle
))
1044 return VMCI_ERROR_INVALID_ARGS
;
1046 /* Get the target VM's VMCI context. */
1047 dst_context
= vmci_ctx_get(handle
.context
);
1049 pr_devel("Invalid context (ID=0x%x)\n", handle
.context
);
1050 return VMCI_ERROR_NOT_FOUND
;
1053 if (src_cid
!= handle
.context
) {
1056 if (VMCI_CONTEXT_IS_VM(src_cid
) &&
1057 VMCI_CONTEXT_IS_VM(handle
.context
)) {
1058 pr_devel("Doorbell notification from VM to VM not supported (src=0x%x, dst=0x%x)\n",
1059 src_cid
, handle
.context
);
1060 result
= VMCI_ERROR_DST_UNREACHABLE
;
1064 result
= vmci_dbell_get_priv_flags(handle
, &dst_priv_flags
);
1065 if (result
< VMCI_SUCCESS
) {
1066 pr_warn("Failed to get privilege flags for destination (handle=0x%x:0x%x)\n",
1067 handle
.context
, handle
.resource
);
1071 if (src_cid
!= VMCI_HOST_CONTEXT_ID
||
1072 src_priv_flags
== VMCI_NO_PRIVILEGE_FLAGS
) {
1073 src_priv_flags
= vmci_context_get_priv_flags(src_cid
);
1076 if (vmci_deny_interaction(src_priv_flags
, dst_priv_flags
)) {
1077 result
= VMCI_ERROR_NO_ACCESS
;
1082 if (handle
.context
== VMCI_HOST_CONTEXT_ID
) {
1083 result
= vmci_dbell_host_context_notify(src_cid
, handle
);
1085 spin_lock(&dst_context
->lock
);
1087 if (!vmci_handle_arr_has_entry(dst_context
->doorbell_array
,
1089 result
= VMCI_ERROR_NOT_FOUND
;
1091 if (!vmci_handle_arr_has_entry(
1092 dst_context
->pending_doorbell_array
,
1094 result
= vmci_handle_arr_append_entry(
1095 &dst_context
->pending_doorbell_array
,
1097 if (result
== VMCI_SUCCESS
) {
1098 ctx_signal_notify(dst_context
);
1099 wake_up(&dst_context
->host_context
.wait_queue
);
1102 result
= VMCI_SUCCESS
;
1105 spin_unlock(&dst_context
->lock
);
1109 vmci_ctx_put(dst_context
);
1114 bool vmci_ctx_supports_host_qp(struct vmci_ctx
*context
)
1116 return context
&& context
->user_version
>= VMCI_VERSION_HOSTQP
;
1120 * Registers that a new queue pair handle has been allocated by
1123 int vmci_ctx_qp_create(struct vmci_ctx
*context
, struct vmci_handle handle
)
1127 if (context
== NULL
|| vmci_handle_is_invalid(handle
))
1128 return VMCI_ERROR_INVALID_ARGS
;
1130 if (!vmci_handle_arr_has_entry(context
->queue_pair_array
, handle
))
1131 result
= vmci_handle_arr_append_entry(
1132 &context
->queue_pair_array
, handle
);
1134 result
= VMCI_ERROR_DUPLICATE_ENTRY
;
1140 * Unregisters a queue pair handle that was previously registered
1141 * with vmci_ctx_qp_create.
1143 int vmci_ctx_qp_destroy(struct vmci_ctx
*context
, struct vmci_handle handle
)
1145 struct vmci_handle hndl
;
1147 if (context
== NULL
|| vmci_handle_is_invalid(handle
))
1148 return VMCI_ERROR_INVALID_ARGS
;
1150 hndl
= vmci_handle_arr_remove_entry(context
->queue_pair_array
, handle
);
1152 return vmci_handle_is_invalid(hndl
) ?
1153 VMCI_ERROR_NOT_FOUND
: VMCI_SUCCESS
;
1157 * Determines whether a given queue pair handle is registered
1158 * with the given context.
1160 bool vmci_ctx_qp_exists(struct vmci_ctx
*context
, struct vmci_handle handle
)
1162 if (context
== NULL
|| vmci_handle_is_invalid(handle
))
1165 return vmci_handle_arr_has_entry(context
->queue_pair_array
, handle
);
1169 * vmci_context_get_priv_flags() - Retrieve privilege flags.
1170 * @context_id: The context ID of the VMCI context.
1172 * Retrieves privilege flags of the given VMCI context ID.
1174 u32
vmci_context_get_priv_flags(u32 context_id
)
1176 if (vmci_host_code_active()) {
1178 struct vmci_ctx
*context
;
1180 context
= vmci_ctx_get(context_id
);
1182 return VMCI_LEAST_PRIVILEGE_FLAGS
;
1184 flags
= context
->priv_flags
;
1185 vmci_ctx_put(context
);
1188 return VMCI_NO_PRIVILEGE_FLAGS
;
1190 EXPORT_SYMBOL_GPL(vmci_context_get_priv_flags
);
1193 * vmci_is_context_owner() - Determimnes if user is the context owner
1194 * @context_id: The context ID of the VMCI context.
1195 * @uid: The host user id (real kernel value).
1197 * Determines whether a given UID is the owner of given VMCI context.
1199 bool vmci_is_context_owner(u32 context_id
, kuid_t uid
)
1201 bool is_owner
= false;
1203 if (vmci_host_code_active()) {
1204 struct vmci_ctx
*context
= vmci_ctx_get(context_id
);
1207 is_owner
= uid_eq(context
->cred
->uid
, uid
);
1208 vmci_ctx_put(context
);
1214 EXPORT_SYMBOL_GPL(vmci_is_context_owner
);