hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/drivers/misc/vmw_vmci/vmci_driver.c
....@@ -1,16 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * VMware VMCI Driver
34 *
45 * 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.
146 */
157
168 #include <linux/vmw_vmci_defs.h>
....@@ -36,6 +28,10 @@
3628 static bool vmci_guest_personality_initialized;
3729 static bool vmci_host_personality_initialized;
3830
31
+static DEFINE_MUTEX(vmci_vsock_mutex); /* protects vmci_vsock_transport_cb */
32
+static vmci_vsock_cb vmci_vsock_transport_cb;
33
+static bool vmci_vsock_cb_host_called;
34
+
3935 /*
4036 * vmci_get_context_id() - Gets the current context ID.
4137 *
....@@ -53,6 +49,69 @@
5349 }
5450 EXPORT_SYMBOL_GPL(vmci_get_context_id);
5551
52
+/*
53
+ * vmci_register_vsock_callback() - Register the VSOCK vmci_transport callback.
54
+ *
55
+ * The callback will be called when the first host or guest becomes active,
56
+ * or if they are already active when this function is called.
57
+ * To unregister the callback, call this function with NULL parameter.
58
+ *
59
+ * Returns 0 on success. -EBUSY if a callback is already registered.
60
+ */
61
+int vmci_register_vsock_callback(vmci_vsock_cb callback)
62
+{
63
+ int err = 0;
64
+
65
+ mutex_lock(&vmci_vsock_mutex);
66
+
67
+ if (vmci_vsock_transport_cb && callback) {
68
+ err = -EBUSY;
69
+ goto out;
70
+ }
71
+
72
+ vmci_vsock_transport_cb = callback;
73
+
74
+ if (!vmci_vsock_transport_cb) {
75
+ vmci_vsock_cb_host_called = false;
76
+ goto out;
77
+ }
78
+
79
+ if (vmci_guest_code_active())
80
+ vmci_vsock_transport_cb(false);
81
+
82
+ if (vmci_host_users() > 0) {
83
+ vmci_vsock_cb_host_called = true;
84
+ vmci_vsock_transport_cb(true);
85
+ }
86
+
87
+out:
88
+ mutex_unlock(&vmci_vsock_mutex);
89
+ return err;
90
+}
91
+EXPORT_SYMBOL_GPL(vmci_register_vsock_callback);
92
+
93
+void vmci_call_vsock_callback(bool is_host)
94
+{
95
+ mutex_lock(&vmci_vsock_mutex);
96
+
97
+ if (!vmci_vsock_transport_cb)
98
+ goto out;
99
+
100
+ /* In the host, this function could be called multiple times,
101
+ * but we want to register it only once.
102
+ */
103
+ if (is_host) {
104
+ if (vmci_vsock_cb_host_called)
105
+ goto out;
106
+
107
+ vmci_vsock_cb_host_called = true;
108
+ }
109
+
110
+ vmci_vsock_transport_cb(is_host);
111
+out:
112
+ mutex_unlock(&vmci_vsock_mutex);
113
+}
114
+
56115 static int __init vmci_drv_init(void)
57116 {
58117 int vmci_err;