1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (C) 2012 VMware, Inc. All rights reserved.
8 #include <linux/vmw_vmci_defs.h>
9 #include <linux/vmw_vmci_api.h>
10 #include <linux/list.h>
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/rculist.h>
16 #include "vmci_driver.h"
17 #include "vmci_event.h"
19 #define EVENT_MAGIC 0xEABE0000
20 #define VMCI_EVENT_MAX_ATTEMPTS 10
22 struct vmci_subscription
{
25 vmci_event_cb callback
;
27 struct list_head node
; /* on one of subscriber lists */
30 static struct list_head subscriber_array
[VMCI_EVENT_MAX
];
31 static DEFINE_MUTEX(subscriber_mutex
);
33 int __init
vmci_event_init(void)
37 for (i
= 0; i
< VMCI_EVENT_MAX
; i
++)
38 INIT_LIST_HEAD(&subscriber_array
[i
]);
43 void vmci_event_exit(void)
47 /* We free all memory at exit. */
48 for (e
= 0; e
< VMCI_EVENT_MAX
; e
++) {
49 struct vmci_subscription
*cur
, *p2
;
50 list_for_each_entry_safe(cur
, p2
, &subscriber_array
[e
], node
) {
53 * We should never get here because all events
54 * should have been unregistered before we try
55 * to unload the driver module.
57 pr_warn("Unexpected free events occurring\n");
65 * Find entry. Assumes subscriber_mutex is held.
67 static struct vmci_subscription
*event_find(u32 sub_id
)
71 for (e
= 0; e
< VMCI_EVENT_MAX
; e
++) {
72 struct vmci_subscription
*cur
;
73 list_for_each_entry(cur
, &subscriber_array
[e
], node
) {
74 if (cur
->id
== sub_id
)
82 * Actually delivers the events to the subscribers.
83 * The callback function for each subscriber is invoked.
85 static void event_deliver(struct vmci_event_msg
*event_msg
)
87 struct vmci_subscription
*cur
;
88 struct list_head
*subscriber_list
;
91 subscriber_list
= &subscriber_array
[event_msg
->event_data
.event
];
92 list_for_each_entry_rcu(cur
, subscriber_list
, node
) {
93 cur
->callback(cur
->id
, &event_msg
->event_data
,
100 * Dispatcher for the VMCI_EVENT_RECEIVE datagrams. Calls all
101 * subscribers for given event.
103 int vmci_event_dispatch(struct vmci_datagram
*msg
)
105 struct vmci_event_msg
*event_msg
= (struct vmci_event_msg
*)msg
;
107 if (msg
->payload_size
< sizeof(u32
) ||
108 msg
->payload_size
> sizeof(struct vmci_event_data_max
))
109 return VMCI_ERROR_INVALID_ARGS
;
111 if (!VMCI_EVENT_VALID(event_msg
->event_data
.event
))
112 return VMCI_ERROR_EVENT_UNKNOWN
;
114 event_deliver(event_msg
);
119 * vmci_event_subscribe() - Subscribe to a given event.
120 * @event: The event to subscribe to.
121 * @callback: The callback to invoke upon the event.
122 * @callback_data: Data to pass to the callback.
123 * @subscription_id: ID used to track subscription. Used with
124 * vmci_event_unsubscribe()
126 * Subscribes to the provided event. The callback specified will be
127 * fired from RCU critical section and therefore must not sleep.
129 int vmci_event_subscribe(u32 event
,
130 vmci_event_cb callback
,
132 u32
*new_subscription_id
)
134 struct vmci_subscription
*sub
;
137 bool have_new_id
= false;
139 if (!new_subscription_id
) {
140 pr_devel("%s: Invalid subscription (NULL)\n", __func__
);
141 return VMCI_ERROR_INVALID_ARGS
;
144 if (!VMCI_EVENT_VALID(event
) || !callback
) {
145 pr_devel("%s: Failed to subscribe to event (type=%d) (callback=%p) (data=%p)\n",
146 __func__
, event
, callback
, callback_data
);
147 return VMCI_ERROR_INVALID_ARGS
;
150 sub
= kzalloc(sizeof(*sub
), GFP_KERNEL
);
152 return VMCI_ERROR_NO_MEM
;
154 sub
->id
= VMCI_EVENT_MAX
;
156 sub
->callback
= callback
;
157 sub
->callback_data
= callback_data
;
158 INIT_LIST_HEAD(&sub
->node
);
160 mutex_lock(&subscriber_mutex
);
162 /* Creation of a new event is always allowed. */
163 for (attempts
= 0; attempts
< VMCI_EVENT_MAX_ATTEMPTS
; attempts
++) {
164 static u32 subscription_id
;
166 * We try to get an id a couple of time before
167 * claiming we are out of resources.
170 /* Test for duplicate id. */
171 if (!event_find(++subscription_id
)) {
172 sub
->id
= subscription_id
;
179 list_add_rcu(&sub
->node
, &subscriber_array
[event
]);
180 retval
= VMCI_SUCCESS
;
182 retval
= VMCI_ERROR_NO_RESOURCES
;
185 mutex_unlock(&subscriber_mutex
);
187 *new_subscription_id
= sub
->id
;
190 EXPORT_SYMBOL_GPL(vmci_event_subscribe
);
193 * vmci_event_unsubscribe() - unsubscribe from an event.
194 * @sub_id: A subscription ID as provided by vmci_event_subscribe()
196 * Unsubscribe from given event. Removes it from list and frees it.
197 * Will return callback_data if requested by caller.
199 int vmci_event_unsubscribe(u32 sub_id
)
201 struct vmci_subscription
*s
;
203 mutex_lock(&subscriber_mutex
);
204 s
= event_find(sub_id
);
206 list_del_rcu(&s
->node
);
207 mutex_unlock(&subscriber_mutex
);
210 return VMCI_ERROR_NOT_FOUND
;
217 EXPORT_SYMBOL_GPL(vmci_event_unsubscribe
);