xref: /linux/drivers/misc/vmw_vmci/vmci_context.c (revision 7f81907b7e3f93dfed2e903af52659baa4944341)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * VMware VMCI Driver
4  *
5  * Copyright (C) 2012 VMware, Inc. All rights reserved.
6  */
7 
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>
16 
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"
23 
24 /* Use a wide upper bound for the maximum contexts. */
25 #define VMCI_MAX_CONTEXTS 2000
26 
27 /*
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.
32  */
33 static struct {
34 	struct list_head head;
35 	spinlock_t lock; /* Spinlock for context list operations */
36 } ctx_list = {
37 	.head = LIST_HEAD_INIT(ctx_list.head),
38 	.lock = __SPIN_LOCK_UNLOCKED(ctx_list.lock),
39 };
40 
41 /* Used by contexts that did not set up notify flag pointers */
42 static bool ctx_dummy_notify;
43 
44 static void ctx_signal_notify(struct vmci_ctx *context)
45 {
46 	*context->notify = true;
47 }
48 
49 static void ctx_clear_notify(struct vmci_ctx *context)
50 {
51 	*context->notify = false;
52 }
53 
54 /*
55  * If nothing requires the attention of the guest, clears both
56  * notify flag and call.
57  */
58 static void ctx_clear_notify_call(struct vmci_ctx *context)
59 {
60 	if (context->pending_datagrams == 0 &&
61 	    vmci_handle_arr_get_size(context->pending_doorbell_array) == 0)
62 		ctx_clear_notify(context);
63 }
64 
65 /*
66  * Sets the context's notify flag iff datagrams are pending for this
67  * context.  Called from vmci_setup_notify().
68  */
69 void vmci_ctx_check_signal_notify(struct vmci_ctx *context)
70 {
71 	spin_lock(&context->lock);
72 	if (context->pending_datagrams)
73 		ctx_signal_notify(context);
74 	spin_unlock(&context->lock);
75 }
76 
77 /*
78  * Allocates and initializes a VMCI context.
79  */
80 struct vmci_ctx *vmci_ctx_create(u32 cid, u32 priv_flags,
81 				 uintptr_t event_hnd,
82 				 int user_version,
83 				 const struct cred *cred)
84 {
85 	struct vmci_ctx *context;
86 	int error;
87 
88 	if (cid == VMCI_INVALID_ID) {
89 		pr_devel("Invalid context ID for VMCI context\n");
90 		error = -EINVAL;
91 		goto err_out;
92 	}
93 
94 	if (priv_flags & ~VMCI_PRIVILEGE_ALL_FLAGS) {
95 		pr_devel("Invalid flag (flags=0x%x) for VMCI context\n",
96 			 priv_flags);
97 		error = -EINVAL;
98 		goto err_out;
99 	}
100 
101 	if (user_version == 0) {
102 		pr_devel("Invalid suer_version %d\n", user_version);
103 		error = -EINVAL;
104 		goto err_out;
105 	}
106 
107 	context = kzalloc(sizeof(*context), GFP_KERNEL);
108 	if (!context) {
109 		pr_warn("Failed to allocate memory for VMCI context\n");
110 		error = -ENOMEM;
111 		goto err_out;
112 	}
113 
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);
119 
120 	/* Initialize host-specific VMCI context. */
121 	init_waitqueue_head(&context->host_context.wait_queue);
122 
123 	context->queue_pair_array =
124 		vmci_handle_arr_create(0, VMCI_MAX_GUEST_QP_COUNT);
125 	if (!context->queue_pair_array) {
126 		error = -ENOMEM;
127 		goto err_free_ctx;
128 	}
129 
130 	context->doorbell_array =
131 		vmci_handle_arr_create(0, VMCI_MAX_GUEST_DOORBELL_COUNT);
132 	if (!context->doorbell_array) {
133 		error = -ENOMEM;
134 		goto err_free_qp_array;
135 	}
136 
137 	context->pending_doorbell_array =
138 		vmci_handle_arr_create(0, VMCI_MAX_GUEST_DOORBELL_COUNT);
139 	if (!context->pending_doorbell_array) {
140 		error = -ENOMEM;
141 		goto err_free_db_array;
142 	}
143 
144 	context->user_version = user_version;
145 
146 	context->priv_flags = priv_flags;
147 
148 	if (cred)
149 		context->cred = get_cred(cred);
150 
151 	context->notify = &ctx_dummy_notify;
152 	context->notify_page = NULL;
153 
154 	/*
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.
159 	 */
160 	spin_lock(&ctx_list.lock);
161 
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;
167 	}
168 	context->cid = cid;
169 
170 	list_add_tail_rcu(&context->list_item, &ctx_list.head);
171 	spin_unlock(&ctx_list.lock);
172 
173 	return context;
174 
175  err_free_db_array:
176 	vmci_handle_arr_destroy(context->doorbell_array);
177  err_free_qp_array:
178 	vmci_handle_arr_destroy(context->queue_pair_array);
179  err_free_ctx:
180 	kfree(context);
181  err_out:
182 	return ERR_PTR(error);
183 }
184 
185 /*
186  * Destroy VMCI context.
187  */
188 void vmci_ctx_destroy(struct vmci_ctx *context)
189 {
190 	spin_lock(&ctx_list.lock);
191 	list_del_rcu(&context->list_item);
192 	spin_unlock(&ctx_list.lock);
193 	synchronize_rcu();
194 
195 	vmci_ctx_put(context);
196 }
197 
198 /*
199  * Fire notification for all contexts interested in given cid.
200  */
201 static int ctx_fire_notification(u32 context_id, u32 priv_flags)
202 {
203 	u32 i, array_size;
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);
208 
209 	/*
210 	 * We create an array to hold the subscribers we find when
211 	 * scanning through all contexts.
212 	 */
213 	subscriber_array = vmci_handle_arr_create(0, VMCI_MAX_CONTEXTS);
214 	if (subscriber_array == NULL)
215 		return VMCI_ERROR_NO_MEM;
216 
217 	/*
218 	 * Scan all contexts to find who is interested in being
219 	 * notified about given contextID.
220 	 */
221 	rcu_read_lock();
222 	list_for_each_entry_rcu(sub_ctx, &ctx_list.head, list_item) {
223 		struct vmci_handle_list *node;
224 
225 		/*
226 		 * We only deliver notifications of the removal of
227 		 * contexts, if the two contexts are allowed to
228 		 * interact.
229 		 */
230 		if (vmci_deny_interaction(priv_flags, sub_ctx->priv_flags))
231 			continue;
232 
233 		list_for_each_entry_rcu(node, &sub_ctx->notifier_list, node) {
234 			if (!vmci_handle_is_equal(node->handle, context_handle))
235 				continue;
236 
237 			vmci_handle_arr_append_entry(&subscriber_array,
238 					vmci_make_handle(sub_ctx->cid,
239 							 VMCI_EVENT_HANDLER));
240 		}
241 	}
242 	rcu_read_unlock();
243 
244 	/* Fire event to all subscribers. */
245 	array_size = vmci_handle_arr_get_size(subscriber_array);
246 	for (i = 0; i < array_size; i++) {
247 		int result;
248 		struct vmci_event_ctx ev;
249 
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;
256 
257 		result = vmci_datagram_dispatch(VMCI_HYPERVISOR_CONTEXT_ID,
258 						&ev.msg.hdr, false);
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. */
264 		}
265 	}
266 	vmci_handle_arr_destroy(subscriber_array);
267 
268 	return VMCI_SUCCESS;
269 }
270 
271 /*
272  * Queues a VMCI datagram for the appropriate target VM context.
273  */
274 int vmci_ctx_enqueue_datagram(u32 cid, struct vmci_datagram *dg)
275 {
276 	struct vmci_datagram_queue_entry *dq_entry;
277 	struct vmci_ctx *context;
278 	struct vmci_handle dg_src;
279 	size_t vmci_dg_size;
280 
281 	vmci_dg_size = VMCI_DG_SIZE(dg);
282 	if (vmci_dg_size > VMCI_MAX_DG_SIZE) {
283 		pr_devel("Datagram too large (bytes=%zu)\n", vmci_dg_size);
284 		return VMCI_ERROR_INVALID_ARGS;
285 	}
286 
287 	/* Get the target VM's VMCI context. */
288 	context = vmci_ctx_get(cid);
289 	if (!context) {
290 		pr_devel("Invalid context (ID=0x%x)\n", cid);
291 		return VMCI_ERROR_INVALID_ARGS;
292 	}
293 
294 	/* Allocate guest call entry and add it to the target VM's queue. */
295 	dq_entry = kmalloc(sizeof(*dq_entry), GFP_KERNEL);
296 	if (dq_entry == NULL) {
297 		pr_warn("Failed to allocate memory for datagram\n");
298 		vmci_ctx_put(context);
299 		return VMCI_ERROR_NO_MEM;
300 	}
301 	dq_entry->dg = dg;
302 	dq_entry->dg_size = vmci_dg_size;
303 	dg_src = dg->src;
304 	INIT_LIST_HEAD(&dq_entry->list_item);
305 
306 	spin_lock(&context->lock);
307 
308 	/*
309 	 * We put a higher limit on datagrams from the hypervisor.  If
310 	 * the pending datagram is not from hypervisor, then we check
311 	 * if enqueueing it would exceed the
312 	 * VMCI_MAX_DATAGRAM_QUEUE_SIZE limit on the destination.  If
313 	 * the pending datagram is from hypervisor, we allow it to be
314 	 * queued at the destination side provided we don't reach the
315 	 * VMCI_MAX_DATAGRAM_AND_EVENT_QUEUE_SIZE limit.
316 	 */
317 	if (context->datagram_queue_size + vmci_dg_size >=
318 	    VMCI_MAX_DATAGRAM_QUEUE_SIZE &&
319 	    (!vmci_handle_is_equal(dg_src,
320 				vmci_make_handle
321 				(VMCI_HYPERVISOR_CONTEXT_ID,
322 				 VMCI_CONTEXT_RESOURCE_ID)) ||
323 	     context->datagram_queue_size + vmci_dg_size >=
324 	     VMCI_MAX_DATAGRAM_AND_EVENT_QUEUE_SIZE)) {
325 		spin_unlock(&context->lock);
326 		vmci_ctx_put(context);
327 		kfree(dq_entry);
328 		pr_devel("Context (ID=0x%x) receive queue is full\n", cid);
329 		return VMCI_ERROR_NO_RESOURCES;
330 	}
331 
332 	list_add(&dq_entry->list_item, &context->datagram_queue);
333 	context->pending_datagrams++;
334 	context->datagram_queue_size += vmci_dg_size;
335 	ctx_signal_notify(context);
336 	wake_up(&context->host_context.wait_queue);
337 	spin_unlock(&context->lock);
338 	vmci_ctx_put(context);
339 
340 	return vmci_dg_size;
341 }
342 
343 /*
344  * Verifies whether a context with the specified context ID exists.
345  * FIXME: utility is dubious as no decisions can be reliably made
346  * using this data as context can appear and disappear at any time.
347  */
348 bool vmci_ctx_exists(u32 cid)
349 {
350 	struct vmci_ctx *context;
351 	bool exists = false;
352 
353 	rcu_read_lock();
354 
355 	list_for_each_entry_rcu(context, &ctx_list.head, list_item) {
356 		if (context->cid == cid) {
357 			exists = true;
358 			break;
359 		}
360 	}
361 
362 	rcu_read_unlock();
363 	return exists;
364 }
365 
366 /*
367  * Retrieves VMCI context corresponding to the given cid.
368  */
369 struct vmci_ctx *vmci_ctx_get(u32 cid)
370 {
371 	struct vmci_ctx *c, *context = NULL;
372 
373 	if (cid == VMCI_INVALID_ID)
374 		return NULL;
375 
376 	rcu_read_lock();
377 	list_for_each_entry_rcu(c, &ctx_list.head, list_item) {
378 		if (c->cid == cid) {
379 			/*
380 			 * The context owner drops its own reference to the
381 			 * context only after removing it from the list and
382 			 * waiting for RCU grace period to expire. This
383 			 * means that we are not about to increase the
384 			 * reference count of something that is in the
385 			 * process of being destroyed.
386 			 */
387 			context = c;
388 			kref_get(&context->kref);
389 			break;
390 		}
391 	}
392 	rcu_read_unlock();
393 
394 	return context;
395 }
396 
397 /*
398  * Deallocates all parts of a context data structure. This
399  * function doesn't lock the context, because it assumes that
400  * the caller was holding the last reference to context.
401  */
402 static void ctx_free_ctx(struct kref *kref)
403 {
404 	struct vmci_ctx *context = container_of(kref, struct vmci_ctx, kref);
405 	struct vmci_datagram_queue_entry *dq_entry, *dq_entry_tmp;
406 	struct vmci_handle temp_handle;
407 	struct vmci_handle_list *notifier, *tmp;
408 
409 	/*
410 	 * Fire event to all contexts interested in knowing this
411 	 * context is dying.
412 	 */
413 	ctx_fire_notification(context->cid, context->priv_flags);
414 
415 	/*
416 	 * Cleanup all queue pair resources attached to context.  If
417 	 * the VM dies without cleaning up, this code will make sure
418 	 * that no resources are leaked.
419 	 */
420 	temp_handle = vmci_handle_arr_get_entry(context->queue_pair_array, 0);
421 	while (!vmci_handle_is_equal(temp_handle, VMCI_INVALID_HANDLE)) {
422 		if (vmci_qp_broker_detach(temp_handle,
423 					  context) < VMCI_SUCCESS) {
424 			/*
425 			 * When vmci_qp_broker_detach() succeeds it
426 			 * removes the handle from the array.  If
427 			 * detach fails, we must remove the handle
428 			 * ourselves.
429 			 */
430 			vmci_handle_arr_remove_entry(context->queue_pair_array,
431 						     temp_handle);
432 		}
433 		temp_handle =
434 		    vmci_handle_arr_get_entry(context->queue_pair_array, 0);
435 	}
436 
437 	/*
438 	 * It is fine to destroy this without locking the callQueue, as
439 	 * this is the only thread having a reference to the context.
440 	 */
441 	list_for_each_entry_safe(dq_entry, dq_entry_tmp,
442 				 &context->datagram_queue, list_item) {
443 		WARN_ON(dq_entry->dg_size != VMCI_DG_SIZE(dq_entry->dg));
444 		list_del(&dq_entry->list_item);
445 		kfree(dq_entry->dg);
446 		kfree(dq_entry);
447 	}
448 
449 	list_for_each_entry_safe(notifier, tmp,
450 				 &context->notifier_list, node) {
451 		list_del(&notifier->node);
452 		kfree(notifier);
453 	}
454 
455 	vmci_handle_arr_destroy(context->queue_pair_array);
456 	vmci_handle_arr_destroy(context->doorbell_array);
457 	vmci_handle_arr_destroy(context->pending_doorbell_array);
458 	vmci_ctx_unset_notify(context);
459 	if (context->cred)
460 		put_cred(context->cred);
461 	kfree(context);
462 }
463 
464 /*
465  * Drops reference to VMCI context. If this is the last reference to
466  * the context it will be deallocated. A context is created with
467  * a reference count of one, and on destroy, it is removed from
468  * the context list before its reference count is decremented. Thus,
469  * if we reach zero, we are sure that nobody else are about to increment
470  * it (they need the entry in the context list for that), and so there
471  * is no need for locking.
472  */
473 void vmci_ctx_put(struct vmci_ctx *context)
474 {
475 	kref_put(&context->kref, ctx_free_ctx);
476 }
477 
478 /*
479  * Dequeues the next datagram and returns it to caller.
480  * The caller passes in a pointer to the max size datagram
481  * it can handle and the datagram is only unqueued if the
482  * size is less than max_size. If larger max_size is set to
483  * the size of the datagram to give the caller a chance to
484  * set up a larger buffer for the guestcall.
485  */
486 int vmci_ctx_dequeue_datagram(struct vmci_ctx *context,
487 			      size_t *max_size,
488 			      struct vmci_datagram **dg)
489 {
490 	struct vmci_datagram_queue_entry *dq_entry;
491 	struct list_head *list_item;
492 	int rv;
493 
494 	/* Dequeue the next datagram entry. */
495 	spin_lock(&context->lock);
496 	if (context->pending_datagrams == 0) {
497 		ctx_clear_notify_call(context);
498 		spin_unlock(&context->lock);
499 		pr_devel("No datagrams pending\n");
500 		return VMCI_ERROR_NO_MORE_DATAGRAMS;
501 	}
502 
503 	list_item = context->datagram_queue.next;
504 
505 	dq_entry =
506 	    list_entry(list_item, struct vmci_datagram_queue_entry, list_item);
507 
508 	/* Check size of caller's buffer. */
509 	if (*max_size < dq_entry->dg_size) {
510 		*max_size = dq_entry->dg_size;
511 		spin_unlock(&context->lock);
512 		pr_devel("Caller's buffer should be at least (size=%u bytes)\n",
513 			 (u32) *max_size);
514 		return VMCI_ERROR_NO_MEM;
515 	}
516 
517 	list_del(list_item);
518 	context->pending_datagrams--;
519 	context->datagram_queue_size -= dq_entry->dg_size;
520 	if (context->pending_datagrams == 0) {
521 		ctx_clear_notify_call(context);
522 		rv = VMCI_SUCCESS;
523 	} else {
524 		/*
525 		 * Return the size of the next datagram.
526 		 */
527 		struct vmci_datagram_queue_entry *next_entry;
528 
529 		list_item = context->datagram_queue.next;
530 		next_entry =
531 		    list_entry(list_item, struct vmci_datagram_queue_entry,
532 			       list_item);
533 
534 		/*
535 		 * The following size_t -> int truncation is fine as
536 		 * the maximum size of a (routable) datagram is 68KB.
537 		 */
538 		rv = (int)next_entry->dg_size;
539 	}
540 	spin_unlock(&context->lock);
541 
542 	/* Caller must free datagram. */
543 	*dg = dq_entry->dg;
544 	dq_entry->dg = NULL;
545 	kfree(dq_entry);
546 
547 	return rv;
548 }
549 
550 /*
551  * Reverts actions set up by vmci_setup_notify().  Unmaps and unlocks the
552  * page mapped/locked by vmci_setup_notify().
553  */
554 void vmci_ctx_unset_notify(struct vmci_ctx *context)
555 {
556 	struct page *notify_page;
557 
558 	spin_lock(&context->lock);
559 
560 	notify_page = context->notify_page;
561 	context->notify = &ctx_dummy_notify;
562 	context->notify_page = NULL;
563 
564 	spin_unlock(&context->lock);
565 
566 	if (notify_page) {
567 		kunmap(notify_page);
568 		put_page(notify_page);
569 	}
570 }
571 
572 /*
573  * Add remote_cid to list of contexts current contexts wants
574  * notifications from/about.
575  */
576 int vmci_ctx_add_notification(u32 context_id, u32 remote_cid)
577 {
578 	struct vmci_ctx *context;
579 	struct vmci_handle_list *notifier, *n;
580 	int result;
581 	bool exists = false;
582 
583 	context = vmci_ctx_get(context_id);
584 	if (!context)
585 		return VMCI_ERROR_NOT_FOUND;
586 
587 	if (VMCI_CONTEXT_IS_VM(context_id) && VMCI_CONTEXT_IS_VM(remote_cid)) {
588 		pr_devel("Context removed notifications for other VMs not supported (src=0x%x, remote=0x%x)\n",
589 			 context_id, remote_cid);
590 		result = VMCI_ERROR_DST_UNREACHABLE;
591 		goto out;
592 	}
593 
594 	if (context->priv_flags & VMCI_PRIVILEGE_FLAG_RESTRICTED) {
595 		result = VMCI_ERROR_NO_ACCESS;
596 		goto out;
597 	}
598 
599 	notifier = kmalloc(sizeof(struct vmci_handle_list), GFP_KERNEL);
600 	if (!notifier) {
601 		result = VMCI_ERROR_NO_MEM;
602 		goto out;
603 	}
604 
605 	INIT_LIST_HEAD(&notifier->node);
606 	notifier->handle = vmci_make_handle(remote_cid, VMCI_EVENT_HANDLER);
607 
608 	spin_lock(&context->lock);
609 
610 	if (context->n_notifiers < VMCI_MAX_CONTEXTS) {
611 		list_for_each_entry(n, &context->notifier_list, node) {
612 			if (vmci_handle_is_equal(n->handle, notifier->handle)) {
613 				exists = true;
614 				break;
615 			}
616 		}
617 
618 		if (exists) {
619 			kfree(notifier);
620 			result = VMCI_ERROR_ALREADY_EXISTS;
621 		} else {
622 			list_add_tail_rcu(&notifier->node,
623 					  &context->notifier_list);
624 			context->n_notifiers++;
625 			result = VMCI_SUCCESS;
626 		}
627 	} else {
628 		kfree(notifier);
629 		result = VMCI_ERROR_NO_MEM;
630 	}
631 
632 	spin_unlock(&context->lock);
633 
634  out:
635 	vmci_ctx_put(context);
636 	return result;
637 }
638 
639 /*
640  * Remove remote_cid from current context's list of contexts it is
641  * interested in getting notifications from/about.
642  */
643 int vmci_ctx_remove_notification(u32 context_id, u32 remote_cid)
644 {
645 	struct vmci_ctx *context;
646 	struct vmci_handle_list *notifier = NULL, *iter, *tmp;
647 	struct vmci_handle handle;
648 
649 	context = vmci_ctx_get(context_id);
650 	if (!context)
651 		return VMCI_ERROR_NOT_FOUND;
652 
653 	handle = vmci_make_handle(remote_cid, VMCI_EVENT_HANDLER);
654 
655 	spin_lock(&context->lock);
656 	list_for_each_entry_safe(iter, tmp,
657 				 &context->notifier_list, node) {
658 		if (vmci_handle_is_equal(iter->handle, handle)) {
659 			list_del_rcu(&iter->node);
660 			context->n_notifiers--;
661 			notifier = iter;
662 			break;
663 		}
664 	}
665 	spin_unlock(&context->lock);
666 
667 	if (notifier)
668 		kvfree_rcu_mightsleep(notifier);
669 
670 	vmci_ctx_put(context);
671 
672 	return notifier ? VMCI_SUCCESS : VMCI_ERROR_NOT_FOUND;
673 }
674 
675 static int vmci_ctx_get_chkpt_notifiers(struct vmci_ctx *context,
676 					u32 *buf_size, void **pbuf)
677 {
678 	u32 *notifiers;
679 	size_t data_size;
680 	struct vmci_handle_list *entry;
681 	int i = 0;
682 
683 	if (context->n_notifiers == 0) {
684 		*buf_size = 0;
685 		*pbuf = NULL;
686 		return VMCI_SUCCESS;
687 	}
688 
689 	data_size = context->n_notifiers * sizeof(*notifiers);
690 	if (*buf_size < data_size) {
691 		*buf_size = data_size;
692 		return VMCI_ERROR_MORE_DATA;
693 	}
694 
695 	notifiers = kmalloc(data_size, GFP_ATOMIC); /* FIXME: want GFP_KERNEL */
696 	if (!notifiers)
697 		return VMCI_ERROR_NO_MEM;
698 
699 	list_for_each_entry(entry, &context->notifier_list, node)
700 		notifiers[i++] = entry->handle.context;
701 
702 	*buf_size = data_size;
703 	*pbuf = notifiers;
704 	return VMCI_SUCCESS;
705 }
706 
707 static int vmci_ctx_get_chkpt_doorbells(struct vmci_ctx *context,
708 					u32 *buf_size, void **pbuf)
709 {
710 	struct dbell_cpt_state *dbells;
711 	u32 i, n_doorbells;
712 
713 	n_doorbells = vmci_handle_arr_get_size(context->doorbell_array);
714 	if (n_doorbells > 0) {
715 		size_t data_size = n_doorbells * sizeof(*dbells);
716 		if (*buf_size < data_size) {
717 			*buf_size = data_size;
718 			return VMCI_ERROR_MORE_DATA;
719 		}
720 
721 		dbells = kzalloc(data_size, GFP_ATOMIC);
722 		if (!dbells)
723 			return VMCI_ERROR_NO_MEM;
724 
725 		for (i = 0; i < n_doorbells; i++)
726 			dbells[i].handle = vmci_handle_arr_get_entry(
727 						context->doorbell_array, i);
728 
729 		*buf_size = data_size;
730 		*pbuf = dbells;
731 	} else {
732 		*buf_size = 0;
733 		*pbuf = NULL;
734 	}
735 
736 	return VMCI_SUCCESS;
737 }
738 
739 /*
740  * Get current context's checkpoint state of given type.
741  */
742 int vmci_ctx_get_chkpt_state(u32 context_id,
743 			     u32 cpt_type,
744 			     u32 *buf_size,
745 			     void **pbuf)
746 {
747 	struct vmci_ctx *context;
748 	int result;
749 
750 	context = vmci_ctx_get(context_id);
751 	if (!context)
752 		return VMCI_ERROR_NOT_FOUND;
753 
754 	spin_lock(&context->lock);
755 
756 	switch (cpt_type) {
757 	case VMCI_NOTIFICATION_CPT_STATE:
758 		result = vmci_ctx_get_chkpt_notifiers(context, buf_size, pbuf);
759 		break;
760 
761 	case VMCI_WELLKNOWN_CPT_STATE:
762 		/*
763 		 * For compatibility with VMX'en with VM to VM communication, we
764 		 * always return zero wellknown handles.
765 		 */
766 
767 		*buf_size = 0;
768 		*pbuf = NULL;
769 		result = VMCI_SUCCESS;
770 		break;
771 
772 	case VMCI_DOORBELL_CPT_STATE:
773 		result = vmci_ctx_get_chkpt_doorbells(context, buf_size, pbuf);
774 		break;
775 
776 	default:
777 		pr_devel("Invalid cpt state (type=%d)\n", cpt_type);
778 		result = VMCI_ERROR_INVALID_ARGS;
779 		break;
780 	}
781 
782 	spin_unlock(&context->lock);
783 	vmci_ctx_put(context);
784 
785 	return result;
786 }
787 
788 /*
789  * Set current context's checkpoint state of given type.
790  */
791 int vmci_ctx_set_chkpt_state(u32 context_id,
792 			     u32 cpt_type,
793 			     u32 buf_size,
794 			     void *cpt_buf)
795 {
796 	u32 i;
797 	u32 current_id;
798 	int result = VMCI_SUCCESS;
799 	u32 num_ids = buf_size / sizeof(u32);
800 
801 	if (cpt_type == VMCI_WELLKNOWN_CPT_STATE && num_ids > 0) {
802 		/*
803 		 * We would end up here if VMX with VM to VM communication
804 		 * attempts to restore a checkpoint with wellknown handles.
805 		 */
806 		pr_warn("Attempt to restore checkpoint with obsolete wellknown handles\n");
807 		return VMCI_ERROR_OBSOLETE;
808 	}
809 
810 	if (cpt_type != VMCI_NOTIFICATION_CPT_STATE) {
811 		pr_devel("Invalid cpt state (type=%d)\n", cpt_type);
812 		return VMCI_ERROR_INVALID_ARGS;
813 	}
814 
815 	for (i = 0; i < num_ids && result == VMCI_SUCCESS; i++) {
816 		current_id = ((u32 *)cpt_buf)[i];
817 		result = vmci_ctx_add_notification(context_id, current_id);
818 		if (result != VMCI_SUCCESS)
819 			break;
820 	}
821 	if (result != VMCI_SUCCESS)
822 		pr_devel("Failed to set cpt state (type=%d) (error=%d)\n",
823 			 cpt_type, result);
824 
825 	return result;
826 }
827 
828 /*
829  * Retrieves the specified context's pending notifications in the
830  * form of a handle array. The handle arrays returned are the
831  * actual data - not a copy and should not be modified by the
832  * caller. They must be released using
833  * vmci_ctx_rcv_notifications_release.
834  */
835 int vmci_ctx_rcv_notifications_get(u32 context_id,
836 				   struct vmci_handle_arr **db_handle_array,
837 				   struct vmci_handle_arr **qp_handle_array)
838 {
839 	struct vmci_ctx *context;
840 	int result = VMCI_SUCCESS;
841 
842 	context = vmci_ctx_get(context_id);
843 	if (context == NULL)
844 		return VMCI_ERROR_NOT_FOUND;
845 
846 	spin_lock(&context->lock);
847 
848 	*db_handle_array = context->pending_doorbell_array;
849 	context->pending_doorbell_array =
850 		vmci_handle_arr_create(0, VMCI_MAX_GUEST_DOORBELL_COUNT);
851 	if (!context->pending_doorbell_array) {
852 		context->pending_doorbell_array = *db_handle_array;
853 		*db_handle_array = NULL;
854 		result = VMCI_ERROR_NO_MEM;
855 	}
856 	*qp_handle_array = NULL;
857 
858 	spin_unlock(&context->lock);
859 	vmci_ctx_put(context);
860 
861 	return result;
862 }
863 
864 /*
865  * Releases handle arrays with pending notifications previously
866  * retrieved using vmci_ctx_rcv_notifications_get. If the
867  * notifications were not successfully handed over to the guest,
868  * success must be false.
869  */
870 void vmci_ctx_rcv_notifications_release(u32 context_id,
871 					struct vmci_handle_arr *db_handle_array,
872 					struct vmci_handle_arr *qp_handle_array,
873 					bool success)
874 {
875 	struct vmci_ctx *context = vmci_ctx_get(context_id);
876 
877 	spin_lock(&context->lock);
878 	if (!success) {
879 		struct vmci_handle handle;
880 
881 		/*
882 		 * New notifications may have been added while we were not
883 		 * holding the context lock, so we transfer any new pending
884 		 * doorbell notifications to the old array, and reinstate the
885 		 * old array.
886 		 */
887 
888 		handle = vmci_handle_arr_remove_tail(
889 					context->pending_doorbell_array);
890 		while (!vmci_handle_is_invalid(handle)) {
891 			if (!vmci_handle_arr_has_entry(db_handle_array,
892 						       handle)) {
893 				vmci_handle_arr_append_entry(
894 						&db_handle_array, handle);
895 			}
896 			handle = vmci_handle_arr_remove_tail(
897 					context->pending_doorbell_array);
898 		}
899 		vmci_handle_arr_destroy(context->pending_doorbell_array);
900 		context->pending_doorbell_array = db_handle_array;
901 		db_handle_array = NULL;
902 	} else {
903 		ctx_clear_notify_call(context);
904 	}
905 	spin_unlock(&context->lock);
906 	vmci_ctx_put(context);
907 
908 	if (db_handle_array)
909 		vmci_handle_arr_destroy(db_handle_array);
910 
911 	if (qp_handle_array)
912 		vmci_handle_arr_destroy(qp_handle_array);
913 }
914 
915 /*
916  * Registers that a new doorbell handle has been allocated by the
917  * context. Only doorbell handles registered can be notified.
918  */
919 int vmci_ctx_dbell_create(u32 context_id, struct vmci_handle handle)
920 {
921 	struct vmci_ctx *context;
922 	int result;
923 
924 	if (context_id == VMCI_INVALID_ID || vmci_handle_is_invalid(handle))
925 		return VMCI_ERROR_INVALID_ARGS;
926 
927 	context = vmci_ctx_get(context_id);
928 	if (context == NULL)
929 		return VMCI_ERROR_NOT_FOUND;
930 
931 	spin_lock(&context->lock);
932 	if (!vmci_handle_arr_has_entry(context->doorbell_array, handle))
933 		result = vmci_handle_arr_append_entry(&context->doorbell_array,
934 						      handle);
935 	else
936 		result = VMCI_ERROR_DUPLICATE_ENTRY;
937 
938 	spin_unlock(&context->lock);
939 	vmci_ctx_put(context);
940 
941 	return result;
942 }
943 
944 /*
945  * Unregisters a doorbell handle that was previously registered
946  * with vmci_ctx_dbell_create.
947  */
948 int vmci_ctx_dbell_destroy(u32 context_id, struct vmci_handle handle)
949 {
950 	struct vmci_ctx *context;
951 	struct vmci_handle removed_handle;
952 
953 	if (context_id == VMCI_INVALID_ID || vmci_handle_is_invalid(handle))
954 		return VMCI_ERROR_INVALID_ARGS;
955 
956 	context = vmci_ctx_get(context_id);
957 	if (context == NULL)
958 		return VMCI_ERROR_NOT_FOUND;
959 
960 	spin_lock(&context->lock);
961 	removed_handle =
962 	    vmci_handle_arr_remove_entry(context->doorbell_array, handle);
963 	vmci_handle_arr_remove_entry(context->pending_doorbell_array, handle);
964 	spin_unlock(&context->lock);
965 
966 	vmci_ctx_put(context);
967 
968 	return vmci_handle_is_invalid(removed_handle) ?
969 	    VMCI_ERROR_NOT_FOUND : VMCI_SUCCESS;
970 }
971 
972 /*
973  * Registers a notification of a doorbell handle initiated by the
974  * specified source context. The notification of doorbells are
975  * subject to the same isolation rules as datagram delivery. To
976  * allow host side senders of notifications a finer granularity
977  * of sender rights than those assigned to the sending context
978  * itself, the host context is required to specify a different
979  * set of privilege flags that will override the privileges of
980  * the source context.
981  */
982 int vmci_ctx_notify_dbell(u32 src_cid,
983 			  struct vmci_handle handle,
984 			  u32 src_priv_flags)
985 {
986 	struct vmci_ctx *dst_context;
987 	int result;
988 
989 	if (vmci_handle_is_invalid(handle))
990 		return VMCI_ERROR_INVALID_ARGS;
991 
992 	/* Get the target VM's VMCI context. */
993 	dst_context = vmci_ctx_get(handle.context);
994 	if (!dst_context) {
995 		pr_devel("Invalid context (ID=0x%x)\n", handle.context);
996 		return VMCI_ERROR_NOT_FOUND;
997 	}
998 
999 	if (src_cid != handle.context) {
1000 		u32 dst_priv_flags;
1001 
1002 		if (VMCI_CONTEXT_IS_VM(src_cid) &&
1003 		    VMCI_CONTEXT_IS_VM(handle.context)) {
1004 			pr_devel("Doorbell notification from VM to VM not supported (src=0x%x, dst=0x%x)\n",
1005 				 src_cid, handle.context);
1006 			result = VMCI_ERROR_DST_UNREACHABLE;
1007 			goto out;
1008 		}
1009 
1010 		result = vmci_dbell_get_priv_flags(handle, &dst_priv_flags);
1011 		if (result < VMCI_SUCCESS) {
1012 			pr_warn("Failed to get privilege flags for destination (handle=0x%x:0x%x)\n",
1013 				handle.context, handle.resource);
1014 			goto out;
1015 		}
1016 
1017 		if (src_cid != VMCI_HOST_CONTEXT_ID ||
1018 		    src_priv_flags == VMCI_NO_PRIVILEGE_FLAGS) {
1019 			src_priv_flags = vmci_context_get_priv_flags(src_cid);
1020 		}
1021 
1022 		if (vmci_deny_interaction(src_priv_flags, dst_priv_flags)) {
1023 			result = VMCI_ERROR_NO_ACCESS;
1024 			goto out;
1025 		}
1026 	}
1027 
1028 	if (handle.context == VMCI_HOST_CONTEXT_ID) {
1029 		result = vmci_dbell_host_context_notify(src_cid, handle);
1030 	} else {
1031 		spin_lock(&dst_context->lock);
1032 
1033 		if (!vmci_handle_arr_has_entry(dst_context->doorbell_array,
1034 					       handle)) {
1035 			result = VMCI_ERROR_NOT_FOUND;
1036 		} else {
1037 			if (!vmci_handle_arr_has_entry(
1038 					dst_context->pending_doorbell_array,
1039 					handle)) {
1040 				result = vmci_handle_arr_append_entry(
1041 					&dst_context->pending_doorbell_array,
1042 					handle);
1043 				if (result == VMCI_SUCCESS) {
1044 					ctx_signal_notify(dst_context);
1045 					wake_up(&dst_context->host_context.wait_queue);
1046 				}
1047 			} else {
1048 				result = VMCI_SUCCESS;
1049 			}
1050 		}
1051 		spin_unlock(&dst_context->lock);
1052 	}
1053 
1054  out:
1055 	vmci_ctx_put(dst_context);
1056 
1057 	return result;
1058 }
1059 
1060 bool vmci_ctx_supports_host_qp(struct vmci_ctx *context)
1061 {
1062 	return context && context->user_version >= VMCI_VERSION_HOSTQP;
1063 }
1064 
1065 /*
1066  * Registers that a new queue pair handle has been allocated by
1067  * the context.
1068  */
1069 int vmci_ctx_qp_create(struct vmci_ctx *context, struct vmci_handle handle)
1070 {
1071 	int result;
1072 
1073 	if (context == NULL || vmci_handle_is_invalid(handle))
1074 		return VMCI_ERROR_INVALID_ARGS;
1075 
1076 	if (!vmci_handle_arr_has_entry(context->queue_pair_array, handle))
1077 		result = vmci_handle_arr_append_entry(
1078 			&context->queue_pair_array, handle);
1079 	else
1080 		result = VMCI_ERROR_DUPLICATE_ENTRY;
1081 
1082 	return result;
1083 }
1084 
1085 /*
1086  * Unregisters a queue pair handle that was previously registered
1087  * with vmci_ctx_qp_create.
1088  */
1089 int vmci_ctx_qp_destroy(struct vmci_ctx *context, struct vmci_handle handle)
1090 {
1091 	struct vmci_handle hndl;
1092 
1093 	if (context == NULL || vmci_handle_is_invalid(handle))
1094 		return VMCI_ERROR_INVALID_ARGS;
1095 
1096 	hndl = vmci_handle_arr_remove_entry(context->queue_pair_array, handle);
1097 
1098 	return vmci_handle_is_invalid(hndl) ?
1099 		VMCI_ERROR_NOT_FOUND : VMCI_SUCCESS;
1100 }
1101 
1102 /*
1103  * Determines whether a given queue pair handle is registered
1104  * with the given context.
1105  */
1106 bool vmci_ctx_qp_exists(struct vmci_ctx *context, struct vmci_handle handle)
1107 {
1108 	if (context == NULL || vmci_handle_is_invalid(handle))
1109 		return false;
1110 
1111 	return vmci_handle_arr_has_entry(context->queue_pair_array, handle);
1112 }
1113 
1114 /*
1115  * vmci_context_get_priv_flags() - Retrieve privilege flags.
1116  * @context_id: The context ID of the VMCI context.
1117  *
1118  * Retrieves privilege flags of the given VMCI context ID.
1119  */
1120 u32 vmci_context_get_priv_flags(u32 context_id)
1121 {
1122 	if (vmci_host_code_active()) {
1123 		u32 flags;
1124 		struct vmci_ctx *context;
1125 
1126 		context = vmci_ctx_get(context_id);
1127 		if (!context)
1128 			return VMCI_LEAST_PRIVILEGE_FLAGS;
1129 
1130 		flags = context->priv_flags;
1131 		vmci_ctx_put(context);
1132 		return flags;
1133 	}
1134 	return VMCI_NO_PRIVILEGE_FLAGS;
1135 }
1136 EXPORT_SYMBOL_GPL(vmci_context_get_priv_flags);
1137 
1138 /*
1139  * vmci_is_context_owner() - Determimnes if user is the context owner
1140  * @context_id: The context ID of the VMCI context.
1141  * @uid:        The host user id (real kernel value).
1142  *
1143  * Determines whether a given UID is the owner of given VMCI context.
1144  */
1145 bool vmci_is_context_owner(u32 context_id, kuid_t uid)
1146 {
1147 	bool is_owner = false;
1148 
1149 	if (vmci_host_code_active()) {
1150 		struct vmci_ctx *context = vmci_ctx_get(context_id);
1151 		if (context) {
1152 			if (context->cred)
1153 				is_owner = uid_eq(context->cred->uid, uid);
1154 			vmci_ctx_put(context);
1155 		}
1156 	}
1157 
1158 	return is_owner;
1159 }
1160 EXPORT_SYMBOL_GPL(vmci_is_context_owner);
1161