vmci_context.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. /*
  2. * VMware VMCI Driver
  3. *
  4. * Copyright (C) 2012 VMware, Inc. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation version 2 and no later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include <linux/vmw_vmci_defs.h>
  16. #include <linux/vmw_vmci_api.h>
  17. #include <linux/highmem.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/sched.h>
  21. #include <linux/slab.h>
  22. #include "vmci_queue_pair.h"
  23. #include "vmci_datagram.h"
  24. #include "vmci_doorbell.h"
  25. #include "vmci_context.h"
  26. #include "vmci_driver.h"
  27. #include "vmci_event.h"
  28. /*
  29. * List of current VMCI contexts. Contexts can be added by
  30. * vmci_ctx_create() and removed via vmci_ctx_destroy().
  31. * These, along with context lookup, are protected by the
  32. * list structure's lock.
  33. */
  34. static struct {
  35. struct list_head head;
  36. spinlock_t lock; /* Spinlock for context list operations */
  37. } ctx_list = {
  38. .head = LIST_HEAD_INIT(ctx_list.head),
  39. .lock = __SPIN_LOCK_UNLOCKED(ctx_list.lock),
  40. };
  41. /* Used by contexts that did not set up notify flag pointers */
  42. static bool ctx_dummy_notify;
  43. static void ctx_signal_notify(struct vmci_ctx *context)
  44. {
  45. *context->notify = true;
  46. }
  47. static void ctx_clear_notify(struct vmci_ctx *context)
  48. {
  49. *context->notify = false;
  50. }
  51. /*
  52. * If nothing requires the attention of the guest, clears both
  53. * notify flag and call.
  54. */
  55. static void ctx_clear_notify_call(struct vmci_ctx *context)
  56. {
  57. if (context->pending_datagrams == 0 &&
  58. vmci_handle_arr_get_size(context->pending_doorbell_array) == 0)
  59. ctx_clear_notify(context);
  60. }
  61. /*
  62. * Sets the context's notify flag iff datagrams are pending for this
  63. * context. Called from vmci_setup_notify().
  64. */
  65. void vmci_ctx_check_signal_notify(struct vmci_ctx *context)
  66. {
  67. spin_lock(&context->lock);
  68. if (context->pending_datagrams)
  69. ctx_signal_notify(context);
  70. spin_unlock(&context->lock);
  71. }
  72. /*
  73. * Allocates and initializes a VMCI context.
  74. */
  75. struct vmci_ctx *vmci_ctx_create(u32 cid, u32 priv_flags,
  76. uintptr_t event_hnd,
  77. int user_version,
  78. const struct cred *cred)
  79. {
  80. struct vmci_ctx *context;
  81. int error;
  82. if (cid == VMCI_INVALID_ID) {
  83. pr_devel("Invalid context ID for VMCI context\n");
  84. error = -EINVAL;
  85. goto err_out;
  86. }
  87. if (priv_flags & ~VMCI_PRIVILEGE_ALL_FLAGS) {
  88. pr_devel("Invalid flag (flags=0x%x) for VMCI context\n",
  89. priv_flags);
  90. error = -EINVAL;
  91. goto err_out;
  92. }
  93. if (user_version == 0) {
  94. pr_devel("Invalid suer_version %d\n", user_version);
  95. error = -EINVAL;
  96. goto err_out;
  97. }
  98. context = kzalloc(sizeof(*context), GFP_KERNEL);
  99. if (!context) {
  100. pr_warn("Failed to allocate memory for VMCI context\n");
  101. error = -EINVAL;
  102. goto err_out;
  103. }
  104. kref_init(&context->kref);
  105. spin_lock_init(&context->lock);
  106. INIT_LIST_HEAD(&context->list_item);
  107. INIT_LIST_HEAD(&context->datagram_queue);
  108. INIT_LIST_HEAD(&context->notifier_list);
  109. /* Initialize host-specific VMCI context. */
  110. init_waitqueue_head(&context->host_context.wait_queue);
  111. context->queue_pair_array = vmci_handle_arr_create(0);
  112. if (!context->queue_pair_array) {
  113. error = -ENOMEM;
  114. goto err_free_ctx;
  115. }
  116. context->doorbell_array = vmci_handle_arr_create(0);
  117. if (!context->doorbell_array) {
  118. error = -ENOMEM;
  119. goto err_free_qp_array;
  120. }
  121. context->pending_doorbell_array = vmci_handle_arr_create(0);
  122. if (!context->pending_doorbell_array) {
  123. error = -ENOMEM;
  124. goto err_free_db_array;
  125. }
  126. context->user_version = user_version;
  127. context->priv_flags = priv_flags;
  128. if (cred)
  129. context->cred = get_cred(cred);
  130. context->notify = &ctx_dummy_notify;
  131. context->notify_page = NULL;
  132. /*
  133. * If we collide with an existing context we generate a new
  134. * and use it instead. The VMX will determine if regeneration
  135. * is okay. Since there isn't 4B - 16 VMs running on a given
  136. * host, the below loop will terminate.
  137. */
  138. spin_lock(&ctx_list.lock);
  139. while (vmci_ctx_exists(cid)) {
  140. /* We reserve the lowest 16 ids for fixed contexts. */
  141. cid = max(cid, VMCI_RESERVED_CID_LIMIT - 1) + 1;
  142. if (cid == VMCI_INVALID_ID)
  143. cid = VMCI_RESERVED_CID_LIMIT;
  144. }
  145. context->cid = cid;
  146. list_add_tail_rcu(&context->list_item, &ctx_list.head);
  147. spin_unlock(&ctx_list.lock);
  148. return context;
  149. err_free_db_array:
  150. vmci_handle_arr_destroy(context->doorbell_array);
  151. err_free_qp_array:
  152. vmci_handle_arr_destroy(context->queue_pair_array);
  153. err_free_ctx:
  154. kfree(context);
  155. err_out:
  156. return ERR_PTR(error);
  157. }
  158. /*
  159. * Destroy VMCI context.
  160. */
  161. void vmci_ctx_destroy(struct vmci_ctx *context)
  162. {
  163. spin_lock(&ctx_list.lock);
  164. list_del_rcu(&context->list_item);
  165. spin_unlock(&ctx_list.lock);
  166. synchronize_rcu();
  167. vmci_ctx_put(context);
  168. }
  169. /*
  170. * Fire notification for all contexts interested in given cid.
  171. */
  172. static int ctx_fire_notification(u32 context_id, u32 priv_flags)
  173. {
  174. u32 i, array_size;
  175. struct vmci_ctx *sub_ctx;
  176. struct vmci_handle_arr *subscriber_array;
  177. struct vmci_handle context_handle =
  178. vmci_make_handle(context_id, VMCI_EVENT_HANDLER);
  179. /*
  180. * We create an array to hold the subscribers we find when
  181. * scanning through all contexts.
  182. */
  183. subscriber_array = vmci_handle_arr_create(0);
  184. if (subscriber_array == NULL)
  185. return VMCI_ERROR_NO_MEM;
  186. /*
  187. * Scan all contexts to find who is interested in being
  188. * notified about given contextID.
  189. */
  190. rcu_read_lock();
  191. list_for_each_entry_rcu(sub_ctx, &ctx_list.head, list_item) {
  192. struct vmci_handle_list *node;
  193. /*
  194. * We only deliver notifications of the removal of
  195. * contexts, if the two contexts are allowed to
  196. * interact.
  197. */
  198. if (vmci_deny_interaction(priv_flags, sub_ctx->priv_flags))
  199. continue;
  200. list_for_each_entry_rcu(node, &sub_ctx->notifier_list, node) {
  201. if (!vmci_handle_is_equal(node->handle, context_handle))
  202. continue;
  203. vmci_handle_arr_append_entry(&subscriber_array,
  204. vmci_make_handle(sub_ctx->cid,
  205. VMCI_EVENT_HANDLER));
  206. }
  207. }
  208. rcu_read_unlock();
  209. /* Fire event to all subscribers. */
  210. array_size = vmci_handle_arr_get_size(subscriber_array);
  211. for (i = 0; i < array_size; i++) {
  212. int result;
  213. struct vmci_event_ctx ev;
  214. ev.msg.hdr.dst = vmci_handle_arr_get_entry(subscriber_array, i);
  215. ev.msg.hdr.src = vmci_make_handle(VMCI_HYPERVISOR_CONTEXT_ID,
  216. VMCI_CONTEXT_RESOURCE_ID);
  217. ev.msg.hdr.payload_size = sizeof(ev) - sizeof(ev.msg.hdr);
  218. ev.msg.event_data.event = VMCI_EVENT_CTX_REMOVED;
  219. ev.payload.context_id = context_id;
  220. result = vmci_datagram_dispatch(VMCI_HYPERVISOR_CONTEXT_ID,
  221. &ev.msg.hdr, false);
  222. if (result < VMCI_SUCCESS) {
  223. pr_devel("Failed to enqueue event datagram (type=%d) for context (ID=0x%x)\n",
  224. ev.msg.event_data.event,
  225. ev.msg.hdr.dst.context);
  226. /* We continue to enqueue on next subscriber. */
  227. }
  228. }
  229. vmci_handle_arr_destroy(subscriber_array);
  230. return VMCI_SUCCESS;
  231. }
  232. /*
  233. * Returns the current number of pending datagrams. The call may
  234. * also serve as a synchronization point for the datagram queue,
  235. * as no enqueue operations can occur concurrently.
  236. */
  237. int vmci_ctx_pending_datagrams(u32 cid, u32 *pending)
  238. {
  239. struct vmci_ctx *context;
  240. context = vmci_ctx_get(cid);
  241. if (context == NULL)
  242. return VMCI_ERROR_INVALID_ARGS;
  243. spin_lock(&context->lock);
  244. if (pending)
  245. *pending = context->pending_datagrams;
  246. spin_unlock(&context->lock);
  247. vmci_ctx_put(context);
  248. return VMCI_SUCCESS;
  249. }
  250. /*
  251. * Queues a VMCI datagram for the appropriate target VM context.
  252. */
  253. int vmci_ctx_enqueue_datagram(u32 cid, struct vmci_datagram *dg)
  254. {
  255. struct vmci_datagram_queue_entry *dq_entry;
  256. struct vmci_ctx *context;
  257. struct vmci_handle dg_src;
  258. size_t vmci_dg_size;
  259. vmci_dg_size = VMCI_DG_SIZE(dg);
  260. if (vmci_dg_size > VMCI_MAX_DG_SIZE) {
  261. pr_devel("Datagram too large (bytes=%Zu)\n", vmci_dg_size);
  262. return VMCI_ERROR_INVALID_ARGS;
  263. }
  264. /* Get the target VM's VMCI context. */
  265. context = vmci_ctx_get(cid);
  266. if (!context) {
  267. pr_devel("Invalid context (ID=0x%x)\n", cid);
  268. return VMCI_ERROR_INVALID_ARGS;
  269. }
  270. /* Allocate guest call entry and add it to the target VM's queue. */
  271. dq_entry = kmalloc(sizeof(*dq_entry), GFP_KERNEL);
  272. if (dq_entry == NULL) {
  273. pr_warn("Failed to allocate memory for datagram\n");
  274. vmci_ctx_put(context);
  275. return VMCI_ERROR_NO_MEM;
  276. }
  277. dq_entry->dg = dg;
  278. dq_entry->dg_size = vmci_dg_size;
  279. dg_src = dg->src;
  280. INIT_LIST_HEAD(&dq_entry->list_item);
  281. spin_lock(&context->lock);
  282. /*
  283. * We put a higher limit on datagrams from the hypervisor. If
  284. * the pending datagram is not from hypervisor, then we check
  285. * if enqueueing it would exceed the
  286. * VMCI_MAX_DATAGRAM_QUEUE_SIZE limit on the destination. If
  287. * the pending datagram is from hypervisor, we allow it to be
  288. * queued at the destination side provided we don't reach the
  289. * VMCI_MAX_DATAGRAM_AND_EVENT_QUEUE_SIZE limit.
  290. */
  291. if (context->datagram_queue_size + vmci_dg_size >=
  292. VMCI_MAX_DATAGRAM_QUEUE_SIZE &&
  293. (!vmci_handle_is_equal(dg_src,
  294. vmci_make_handle
  295. (VMCI_HYPERVISOR_CONTEXT_ID,
  296. VMCI_CONTEXT_RESOURCE_ID)) ||
  297. context->datagram_queue_size + vmci_dg_size >=
  298. VMCI_MAX_DATAGRAM_AND_EVENT_QUEUE_SIZE)) {
  299. spin_unlock(&context->lock);
  300. vmci_ctx_put(context);
  301. kfree(dq_entry);
  302. pr_devel("Context (ID=0x%x) receive queue is full\n", cid);
  303. return VMCI_ERROR_NO_RESOURCES;
  304. }
  305. list_add(&dq_entry->list_item, &context->datagram_queue);
  306. context->pending_datagrams++;
  307. context->datagram_queue_size += vmci_dg_size;
  308. ctx_signal_notify(context);
  309. wake_up(&context->host_context.wait_queue);
  310. spin_unlock(&context->lock);
  311. vmci_ctx_put(context);
  312. return vmci_dg_size;
  313. }
  314. /*
  315. * Verifies whether a context with the specified context ID exists.
  316. * FIXME: utility is dubious as no decisions can be reliably made
  317. * using this data as context can appear and disappear at any time.
  318. */
  319. bool vmci_ctx_exists(u32 cid)
  320. {
  321. struct vmci_ctx *context;
  322. bool exists = false;
  323. rcu_read_lock();
  324. list_for_each_entry_rcu(context, &ctx_list.head, list_item) {
  325. if (context->cid == cid) {
  326. exists = true;
  327. break;
  328. }
  329. }
  330. rcu_read_unlock();
  331. return exists;
  332. }
  333. /*
  334. * Retrieves VMCI context corresponding to the given cid.
  335. */
  336. struct vmci_ctx *vmci_ctx_get(u32 cid)
  337. {
  338. struct vmci_ctx *c, *context = NULL;
  339. if (cid == VMCI_INVALID_ID)
  340. return NULL;
  341. rcu_read_lock();
  342. list_for_each_entry_rcu(c, &ctx_list.head, list_item) {
  343. if (c->cid == cid) {
  344. /*
  345. * The context owner drops its own reference to the
  346. * context only after removing it from the list and
  347. * waiting for RCU grace period to expire. This
  348. * means that we are not about to increase the
  349. * reference count of something that is in the
  350. * process of being destroyed.
  351. */
  352. context = c;
  353. kref_get(&context->kref);
  354. break;
  355. }
  356. }
  357. rcu_read_unlock();
  358. return context;
  359. }
  360. /*
  361. * Deallocates all parts of a context data structure. This
  362. * function doesn't lock the context, because it assumes that
  363. * the caller was holding the last reference to context.
  364. */
  365. static void ctx_free_ctx(struct kref *kref)
  366. {
  367. struct vmci_ctx *context = container_of(kref, struct vmci_ctx, kref);
  368. struct vmci_datagram_queue_entry *dq_entry, *dq_entry_tmp;
  369. struct vmci_handle temp_handle;
  370. struct vmci_handle_list *notifier, *tmp;
  371. /*
  372. * Fire event to all contexts interested in knowing this
  373. * context is dying.
  374. */
  375. ctx_fire_notification(context->cid, context->priv_flags);
  376. /*
  377. * Cleanup all queue pair resources attached to context. If
  378. * the VM dies without cleaning up, this code will make sure
  379. * that no resources are leaked.
  380. */
  381. temp_handle = vmci_handle_arr_get_entry(context->queue_pair_array, 0);
  382. while (!vmci_handle_is_equal(temp_handle, VMCI_INVALID_HANDLE)) {
  383. if (vmci_qp_broker_detach(temp_handle,
  384. context) < VMCI_SUCCESS) {
  385. /*
  386. * When vmci_qp_broker_detach() succeeds it
  387. * removes the handle from the array. If
  388. * detach fails, we must remove the handle
  389. * ourselves.
  390. */
  391. vmci_handle_arr_remove_entry(context->queue_pair_array,
  392. temp_handle);
  393. }
  394. temp_handle =
  395. vmci_handle_arr_get_entry(context->queue_pair_array, 0);
  396. }
  397. /*
  398. * It is fine to destroy this without locking the callQueue, as
  399. * this is the only thread having a reference to the context.
  400. */
  401. list_for_each_entry_safe(dq_entry, dq_entry_tmp,
  402. &context->datagram_queue, list_item) {
  403. WARN_ON(dq_entry->dg_size != VMCI_DG_SIZE(dq_entry->dg));
  404. list_del(&dq_entry->list_item);
  405. kfree(dq_entry->dg);
  406. kfree(dq_entry);
  407. }
  408. list_for_each_entry_safe(notifier, tmp,
  409. &context->notifier_list, node) {
  410. list_del(&notifier->node);
  411. kfree(notifier);
  412. }
  413. vmci_handle_arr_destroy(context->queue_pair_array);
  414. vmci_handle_arr_destroy(context->doorbell_array);
  415. vmci_handle_arr_destroy(context->pending_doorbell_array);
  416. vmci_ctx_unset_notify(context);
  417. if (context->cred)
  418. put_cred(context->cred);
  419. kfree(context);
  420. }
  421. /*
  422. * Drops reference to VMCI context. If this is the last reference to
  423. * the context it will be deallocated. A context is created with
  424. * a reference count of one, and on destroy, it is removed from
  425. * the context list before its reference count is decremented. Thus,
  426. * if we reach zero, we are sure that nobody else are about to increment
  427. * it (they need the entry in the context list for that), and so there
  428. * is no need for locking.
  429. */
  430. void vmci_ctx_put(struct vmci_ctx *context)
  431. {
  432. kref_put(&context->kref, ctx_free_ctx);
  433. }
  434. /*
  435. * Dequeues the next datagram and returns it to caller.
  436. * The caller passes in a pointer to the max size datagram
  437. * it can handle and the datagram is only unqueued if the
  438. * size is less than max_size. If larger max_size is set to
  439. * the size of the datagram to give the caller a chance to
  440. * set up a larger buffer for the guestcall.
  441. */
  442. int vmci_ctx_dequeue_datagram(struct vmci_ctx *context,
  443. size_t *max_size,
  444. struct vmci_datagram **dg)
  445. {
  446. struct vmci_datagram_queue_entry *dq_entry;
  447. struct list_head *list_item;
  448. int rv;
  449. /* Dequeue the next datagram entry. */
  450. spin_lock(&context->lock);
  451. if (context->pending_datagrams == 0) {
  452. ctx_clear_notify_call(context);
  453. spin_unlock(&context->lock);
  454. pr_devel("No datagrams pending\n");
  455. return VMCI_ERROR_NO_MORE_DATAGRAMS;
  456. }
  457. list_item = context->datagram_queue.next;
  458. dq_entry =
  459. list_entry(list_item, struct vmci_datagram_queue_entry, list_item);
  460. /* Check size of caller's buffer. */
  461. if (*max_size < dq_entry->dg_size) {
  462. *max_size = dq_entry->dg_size;
  463. spin_unlock(&context->lock);
  464. pr_devel("Caller's buffer should be at least (size=%u bytes)\n",
  465. (u32) *max_size);
  466. return VMCI_ERROR_NO_MEM;
  467. }
  468. list_del(list_item);
  469. context->pending_datagrams--;
  470. context->datagram_queue_size -= dq_entry->dg_size;
  471. if (context->pending_datagrams == 0) {
  472. ctx_clear_notify_call(context);
  473. rv = VMCI_SUCCESS;
  474. } else {
  475. /*
  476. * Return the size of the next datagram.
  477. */
  478. struct vmci_datagram_queue_entry *next_entry;
  479. list_item = context->datagram_queue.next;
  480. next_entry =
  481. list_entry(list_item, struct vmci_datagram_queue_entry,
  482. list_item);
  483. /*
  484. * The following size_t -> int truncation is fine as
  485. * the maximum size of a (routable) datagram is 68KB.
  486. */
  487. rv = (int)next_entry->dg_size;
  488. }
  489. spin_unlock(&context->lock);
  490. /* Caller must free datagram. */
  491. *dg = dq_entry->dg;
  492. dq_entry->dg = NULL;
  493. kfree(dq_entry);
  494. return rv;
  495. }
  496. /*
  497. * Reverts actions set up by vmci_setup_notify(). Unmaps and unlocks the
  498. * page mapped/locked by vmci_setup_notify().
  499. */
  500. void vmci_ctx_unset_notify(struct vmci_ctx *context)
  501. {
  502. struct page *notify_page;
  503. spin_lock(&context->lock);
  504. notify_page = context->notify_page;
  505. context->notify = &ctx_dummy_notify;
  506. context->notify_page = NULL;
  507. spin_unlock(&context->lock);
  508. if (notify_page) {
  509. kunmap(notify_page);
  510. put_page(notify_page);
  511. }
  512. }
  513. /*
  514. * Add remote_cid to list of contexts current contexts wants
  515. * notifications from/about.
  516. */
  517. int vmci_ctx_add_notification(u32 context_id, u32 remote_cid)
  518. {
  519. struct vmci_ctx *context;
  520. struct vmci_handle_list *notifier, *n;
  521. int result;
  522. bool exists = false;
  523. context = vmci_ctx_get(context_id);
  524. if (!context)
  525. return VMCI_ERROR_NOT_FOUND;
  526. if (VMCI_CONTEXT_IS_VM(context_id) && VMCI_CONTEXT_IS_VM(remote_cid)) {
  527. pr_devel("Context removed notifications for other VMs not supported (src=0x%x, remote=0x%x)\n",
  528. context_id, remote_cid);
  529. result = VMCI_ERROR_DST_UNREACHABLE;
  530. goto out;
  531. }
  532. if (context->priv_flags & VMCI_PRIVILEGE_FLAG_RESTRICTED) {
  533. result = VMCI_ERROR_NO_ACCESS;
  534. goto out;
  535. }
  536. notifier = kmalloc(sizeof(struct vmci_handle_list), GFP_KERNEL);
  537. if (!notifier) {
  538. result = VMCI_ERROR_NO_MEM;
  539. goto out;
  540. }
  541. INIT_LIST_HEAD(&notifier->node);
  542. notifier->handle = vmci_make_handle(remote_cid, VMCI_EVENT_HANDLER);
  543. spin_lock(&context->lock);
  544. list_for_each_entry(n, &context->notifier_list, node) {
  545. if (vmci_handle_is_equal(n->handle, notifier->handle)) {
  546. exists = true;
  547. break;
  548. }
  549. }
  550. if (exists) {
  551. kfree(notifier);
  552. result = VMCI_ERROR_ALREADY_EXISTS;
  553. } else {
  554. list_add_tail_rcu(&notifier->node, &context->notifier_list);
  555. context->n_notifiers++;
  556. result = VMCI_SUCCESS;
  557. }
  558. spin_unlock(&context->lock);
  559. out:
  560. vmci_ctx_put(context);
  561. return result;
  562. }
  563. /*
  564. * Remove remote_cid from current context's list of contexts it is
  565. * interested in getting notifications from/about.
  566. */
  567. int vmci_ctx_remove_notification(u32 context_id, u32 remote_cid)
  568. {
  569. struct vmci_ctx *context;
  570. struct vmci_handle_list *notifier, *tmp;
  571. struct vmci_handle handle;
  572. bool found = false;
  573. context = vmci_ctx_get(context_id);
  574. if (!context)
  575. return VMCI_ERROR_NOT_FOUND;
  576. handle = vmci_make_handle(remote_cid, VMCI_EVENT_HANDLER);
  577. spin_lock(&context->lock);
  578. list_for_each_entry_safe(notifier, tmp,
  579. &context->notifier_list, node) {
  580. if (vmci_handle_is_equal(notifier->handle, handle)) {
  581. list_del_rcu(&notifier->node);
  582. context->n_notifiers--;
  583. found = true;
  584. break;
  585. }
  586. }
  587. spin_unlock(&context->lock);
  588. if (found) {
  589. synchronize_rcu();
  590. kfree(notifier);
  591. }
  592. vmci_ctx_put(context);
  593. return found ? VMCI_SUCCESS : VMCI_ERROR_NOT_FOUND;
  594. }
  595. static int vmci_ctx_get_chkpt_notifiers(struct vmci_ctx *context,
  596. u32 *buf_size, void **pbuf)
  597. {
  598. u32 *notifiers;
  599. size_t data_size;
  600. struct vmci_handle_list *entry;
  601. int i = 0;
  602. if (context->n_notifiers == 0) {
  603. *buf_size = 0;
  604. *pbuf = NULL;
  605. return VMCI_SUCCESS;
  606. }
  607. data_size = context->n_notifiers * sizeof(*notifiers);
  608. if (*buf_size < data_size) {
  609. *buf_size = data_size;
  610. return VMCI_ERROR_MORE_DATA;
  611. }
  612. notifiers = kmalloc(data_size, GFP_ATOMIC); /* FIXME: want GFP_KERNEL */
  613. if (!notifiers)
  614. return VMCI_ERROR_NO_MEM;
  615. list_for_each_entry(entry, &context->notifier_list, node)
  616. notifiers[i++] = entry->handle.context;
  617. *buf_size = data_size;
  618. *pbuf = notifiers;
  619. return VMCI_SUCCESS;
  620. }
  621. static int vmci_ctx_get_chkpt_doorbells(struct vmci_ctx *context,
  622. u32 *buf_size, void **pbuf)
  623. {
  624. struct dbell_cpt_state *dbells;
  625. size_t n_doorbells;
  626. int i;
  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 = kmalloc(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 = vmci_handle_arr_create(0);
  741. if (!context->pending_doorbell_array) {
  742. context->pending_doorbell_array = *db_handle_array;
  743. *db_handle_array = NULL;
  744. result = VMCI_ERROR_NO_MEM;
  745. }
  746. *qp_handle_array = NULL;
  747. spin_unlock(&context->lock);
  748. vmci_ctx_put(context);
  749. return result;
  750. }
  751. /*
  752. * Releases handle arrays with pending notifications previously
  753. * retrieved using vmci_ctx_rcv_notifications_get. If the
  754. * notifications were not successfully handed over to the guest,
  755. * success must be false.
  756. */
  757. void vmci_ctx_rcv_notifications_release(u32 context_id,
  758. struct vmci_handle_arr *db_handle_array,
  759. struct vmci_handle_arr *qp_handle_array,
  760. bool success)
  761. {
  762. struct vmci_ctx *context = vmci_ctx_get(context_id);
  763. spin_lock(&context->lock);
  764. if (!success) {
  765. struct vmci_handle handle;
  766. /*
  767. * New notifications may have been added while we were not
  768. * holding the context lock, so we transfer any new pending
  769. * doorbell notifications to the old array, and reinstate the
  770. * old array.
  771. */
  772. handle = vmci_handle_arr_remove_tail(
  773. context->pending_doorbell_array);
  774. while (!vmci_handle_is_invalid(handle)) {
  775. if (!vmci_handle_arr_has_entry(db_handle_array,
  776. handle)) {
  777. vmci_handle_arr_append_entry(
  778. &db_handle_array, handle);
  779. }
  780. handle = vmci_handle_arr_remove_tail(
  781. context->pending_doorbell_array);
  782. }
  783. vmci_handle_arr_destroy(context->pending_doorbell_array);
  784. context->pending_doorbell_array = db_handle_array;
  785. db_handle_array = NULL;
  786. } else {
  787. ctx_clear_notify_call(context);
  788. }
  789. spin_unlock(&context->lock);
  790. vmci_ctx_put(context);
  791. if (db_handle_array)
  792. vmci_handle_arr_destroy(db_handle_array);
  793. if (qp_handle_array)
  794. vmci_handle_arr_destroy(qp_handle_array);
  795. }
  796. /*
  797. * Registers that a new doorbell handle has been allocated by the
  798. * context. Only doorbell handles registered can be notified.
  799. */
  800. int vmci_ctx_dbell_create(u32 context_id, struct vmci_handle handle)
  801. {
  802. struct vmci_ctx *context;
  803. int result;
  804. if (context_id == VMCI_INVALID_ID || vmci_handle_is_invalid(handle))
  805. return VMCI_ERROR_INVALID_ARGS;
  806. context = vmci_ctx_get(context_id);
  807. if (context == NULL)
  808. return VMCI_ERROR_NOT_FOUND;
  809. spin_lock(&context->lock);
  810. if (!vmci_handle_arr_has_entry(context->doorbell_array, handle)) {
  811. vmci_handle_arr_append_entry(&context->doorbell_array, handle);
  812. result = VMCI_SUCCESS;
  813. } else {
  814. result = VMCI_ERROR_DUPLICATE_ENTRY;
  815. }
  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. vmci_handle_arr_append_entry(
  928. &dst_context->pending_doorbell_array,
  929. handle);
  930. ctx_signal_notify(dst_context);
  931. wake_up(&dst_context->host_context.wait_queue);
  932. }
  933. result = VMCI_SUCCESS;
  934. }
  935. spin_unlock(&dst_context->lock);
  936. }
  937. out:
  938. vmci_ctx_put(dst_context);
  939. return result;
  940. }
  941. bool vmci_ctx_supports_host_qp(struct vmci_ctx *context)
  942. {
  943. return context && context->user_version >= VMCI_VERSION_HOSTQP;
  944. }
  945. /*
  946. * Registers that a new queue pair handle has been allocated by
  947. * the context.
  948. */
  949. int vmci_ctx_qp_create(struct vmci_ctx *context, struct vmci_handle handle)
  950. {
  951. int result;
  952. if (context == NULL || vmci_handle_is_invalid(handle))
  953. return VMCI_ERROR_INVALID_ARGS;
  954. if (!vmci_handle_arr_has_entry(context->queue_pair_array, handle)) {
  955. vmci_handle_arr_append_entry(&context->queue_pair_array,
  956. handle);
  957. result = VMCI_SUCCESS;
  958. } else {
  959. result = VMCI_ERROR_DUPLICATE_ENTRY;
  960. }
  961. return result;
  962. }
  963. /*
  964. * Unregisters a queue pair handle that was previously registered
  965. * with vmci_ctx_qp_create.
  966. */
  967. int vmci_ctx_qp_destroy(struct vmci_ctx *context, struct vmci_handle handle)
  968. {
  969. struct vmci_handle hndl;
  970. if (context == NULL || vmci_handle_is_invalid(handle))
  971. return VMCI_ERROR_INVALID_ARGS;
  972. hndl = vmci_handle_arr_remove_entry(context->queue_pair_array, handle);
  973. return vmci_handle_is_invalid(hndl) ?
  974. VMCI_ERROR_NOT_FOUND : VMCI_SUCCESS;
  975. }
  976. /*
  977. * Determines whether a given queue pair handle is registered
  978. * with the given context.
  979. */
  980. bool vmci_ctx_qp_exists(struct vmci_ctx *context, struct vmci_handle handle)
  981. {
  982. if (context == NULL || vmci_handle_is_invalid(handle))
  983. return false;
  984. return vmci_handle_arr_has_entry(context->queue_pair_array, handle);
  985. }
  986. /*
  987. * vmci_context_get_priv_flags() - Retrieve privilege flags.
  988. * @context_id: The context ID of the VMCI context.
  989. *
  990. * Retrieves privilege flags of the given VMCI context ID.
  991. */
  992. u32 vmci_context_get_priv_flags(u32 context_id)
  993. {
  994. if (vmci_host_code_active()) {
  995. u32 flags;
  996. struct vmci_ctx *context;
  997. context = vmci_ctx_get(context_id);
  998. if (!context)
  999. return VMCI_LEAST_PRIVILEGE_FLAGS;
  1000. flags = context->priv_flags;
  1001. vmci_ctx_put(context);
  1002. return flags;
  1003. }
  1004. return VMCI_NO_PRIVILEGE_FLAGS;
  1005. }
  1006. EXPORT_SYMBOL_GPL(vmci_context_get_priv_flags);
  1007. /*
  1008. * vmci_is_context_owner() - Determimnes if user is the context owner
  1009. * @context_id: The context ID of the VMCI context.
  1010. * @uid: The host user id (real kernel value).
  1011. *
  1012. * Determines whether a given UID is the owner of given VMCI context.
  1013. */
  1014. bool vmci_is_context_owner(u32 context_id, kuid_t uid)
  1015. {
  1016. bool is_owner = false;
  1017. if (vmci_host_code_active()) {
  1018. struct vmci_ctx *context = vmci_ctx_get(context_id);
  1019. if (context) {
  1020. if (context->cred)
  1021. is_owner = uid_eq(context->cred->uid, uid);
  1022. vmci_ctx_put(context);
  1023. }
  1024. }
  1025. return is_owner;
  1026. }
  1027. EXPORT_SYMBOL_GPL(vmci_is_context_owner);