vmci_event.c 5.0 KB

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