vmci_event.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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/list.h>
  10. #include <linux/module.h>
  11. #include <linux/nospec.h>
  12. #include <linux/sched.h>
  13. #include <linux/slab.h>
  14. #include <linux/rculist.h>
  15. #include "vmci_driver.h"
  16. #include "vmci_event.h"
  17. #define EVENT_MAGIC 0xEABE0000
  18. #define VMCI_EVENT_MAX_ATTEMPTS 10
  19. struct vmci_subscription {
  20. u32 id;
  21. u32 event;
  22. vmci_event_cb callback;
  23. void *callback_data;
  24. struct list_head node; /* on one of subscriber lists */
  25. };
  26. static struct list_head subscriber_array[VMCI_EVENT_MAX];
  27. static DEFINE_MUTEX(subscriber_mutex);
  28. int __init vmci_event_init(void)
  29. {
  30. int i;
  31. for (i = 0; i < VMCI_EVENT_MAX; i++)
  32. INIT_LIST_HEAD(&subscriber_array[i]);
  33. return VMCI_SUCCESS;
  34. }
  35. void vmci_event_exit(void)
  36. {
  37. int e;
  38. /* We free all memory at exit. */
  39. for (e = 0; e < VMCI_EVENT_MAX; e++) {
  40. struct vmci_subscription *cur, *p2;
  41. list_for_each_entry_safe(cur, p2, &subscriber_array[e], node) {
  42. /*
  43. * We should never get here because all events
  44. * should have been unregistered before we try
  45. * to unload the driver module.
  46. */
  47. pr_warn("Unexpected free events occurring\n");
  48. list_del(&cur->node);
  49. kfree(cur);
  50. }
  51. }
  52. }
  53. /*
  54. * Find entry. Assumes subscriber_mutex is held.
  55. */
  56. static struct vmci_subscription *event_find(u32 sub_id)
  57. {
  58. int e;
  59. for (e = 0; e < VMCI_EVENT_MAX; e++) {
  60. struct vmci_subscription *cur;
  61. list_for_each_entry(cur, &subscriber_array[e], node) {
  62. if (cur->id == sub_id)
  63. return cur;
  64. }
  65. }
  66. return NULL;
  67. }
  68. /*
  69. * Actually delivers the events to the subscribers.
  70. * The callback function for each subscriber is invoked.
  71. */
  72. static void event_deliver(struct vmci_event_msg *event_msg)
  73. {
  74. struct vmci_subscription *cur;
  75. struct list_head *subscriber_list;
  76. u32 sanitized_event, max_vmci_event;
  77. rcu_read_lock();
  78. max_vmci_event = ARRAY_SIZE(subscriber_array);
  79. sanitized_event = array_index_nospec(event_msg->event_data.event, max_vmci_event);
  80. subscriber_list = &subscriber_array[sanitized_event];
  81. list_for_each_entry_rcu(cur, subscriber_list, node) {
  82. cur->callback(cur->id, &event_msg->event_data,
  83. cur->callback_data);
  84. }
  85. rcu_read_unlock();
  86. }
  87. /*
  88. * Dispatcher for the VMCI_EVENT_RECEIVE datagrams. Calls all
  89. * subscribers for given event.
  90. */
  91. int vmci_event_dispatch(struct vmci_datagram *msg)
  92. {
  93. struct vmci_event_msg *event_msg = (struct vmci_event_msg *)msg;
  94. if (msg->payload_size < sizeof(u32) ||
  95. msg->payload_size > sizeof(struct vmci_event_data_max))
  96. return VMCI_ERROR_INVALID_ARGS;
  97. if (!VMCI_EVENT_VALID(event_msg->event_data.event))
  98. return VMCI_ERROR_EVENT_UNKNOWN;
  99. event_deliver(event_msg);
  100. return VMCI_SUCCESS;
  101. }
  102. /*
  103. * vmci_event_subscribe() - Subscribe to a given event.
  104. * @event: The event to subscribe to.
  105. * @callback: The callback to invoke upon the event.
  106. * @callback_data: Data to pass to the callback.
  107. * @subscription_id: ID used to track subscription. Used with
  108. * vmci_event_unsubscribe()
  109. *
  110. * Subscribes to the provided event. The callback specified will be
  111. * fired from RCU critical section and therefore must not sleep.
  112. */
  113. int vmci_event_subscribe(u32 event,
  114. vmci_event_cb callback,
  115. void *callback_data,
  116. u32 *new_subscription_id)
  117. {
  118. struct vmci_subscription *sub;
  119. int attempts;
  120. int retval;
  121. bool have_new_id = false;
  122. if (!new_subscription_id) {
  123. pr_devel("%s: Invalid subscription (NULL)\n", __func__);
  124. return VMCI_ERROR_INVALID_ARGS;
  125. }
  126. if (!VMCI_EVENT_VALID(event) || !callback) {
  127. pr_devel("%s: Failed to subscribe to event (type=%d) (callback=%p) (data=%p)\n",
  128. __func__, event, callback, callback_data);
  129. return VMCI_ERROR_INVALID_ARGS;
  130. }
  131. sub = kzalloc_obj(*sub);
  132. if (!sub)
  133. return VMCI_ERROR_NO_MEM;
  134. sub->id = VMCI_EVENT_MAX;
  135. sub->event = event;
  136. sub->callback = callback;
  137. sub->callback_data = callback_data;
  138. INIT_LIST_HEAD(&sub->node);
  139. mutex_lock(&subscriber_mutex);
  140. /* Creation of a new event is always allowed. */
  141. for (attempts = 0; attempts < VMCI_EVENT_MAX_ATTEMPTS; attempts++) {
  142. static u32 subscription_id;
  143. /*
  144. * We try to get an id a couple of time before
  145. * claiming we are out of resources.
  146. */
  147. /* Test for duplicate id. */
  148. if (!event_find(++subscription_id)) {
  149. sub->id = subscription_id;
  150. have_new_id = true;
  151. break;
  152. }
  153. }
  154. if (have_new_id) {
  155. list_add_rcu(&sub->node, &subscriber_array[event]);
  156. retval = VMCI_SUCCESS;
  157. } else {
  158. retval = VMCI_ERROR_NO_RESOURCES;
  159. }
  160. mutex_unlock(&subscriber_mutex);
  161. *new_subscription_id = sub->id;
  162. return retval;
  163. }
  164. EXPORT_SYMBOL_GPL(vmci_event_subscribe);
  165. /*
  166. * vmci_event_unsubscribe() - unsubscribe from an event.
  167. * @sub_id: A subscription ID as provided by vmci_event_subscribe()
  168. *
  169. * Unsubscribe from given event. Removes it from list and frees it.
  170. * Will return callback_data if requested by caller.
  171. */
  172. int vmci_event_unsubscribe(u32 sub_id)
  173. {
  174. struct vmci_subscription *s;
  175. mutex_lock(&subscriber_mutex);
  176. s = event_find(sub_id);
  177. if (s)
  178. list_del_rcu(&s->node);
  179. mutex_unlock(&subscriber_mutex);
  180. if (!s)
  181. return VMCI_ERROR_NOT_FOUND;
  182. kvfree_rcu_mightsleep(s);
  183. return VMCI_SUCCESS;
  184. }
  185. EXPORT_SYMBOL_GPL(vmci_event_unsubscribe);