vmci_context.c 31 KB

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