1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#include <linux/vmw_vmci_defs.h>
17#include <linux/hash.h>
18#include <linux/types.h>
19#include <linux/rculist.h>
20#include <linux/completion.h>
21
22#include "vmci_resource.h"
23#include "vmci_driver.h"
24
25
26#define VMCI_RESOURCE_HASH_BITS 7
27#define VMCI_RESOURCE_HASH_BUCKETS (1 << VMCI_RESOURCE_HASH_BITS)
28
29struct vmci_hash_table {
30 spinlock_t lock;
31 struct hlist_head entries[VMCI_RESOURCE_HASH_BUCKETS];
32};
33
34static struct vmci_hash_table vmci_resource_table = {
35 .lock = __SPIN_LOCK_UNLOCKED(vmci_resource_table.lock),
36};
37
38static unsigned int vmci_resource_hash(struct vmci_handle handle)
39{
40 return hash_32(handle.resource, VMCI_RESOURCE_HASH_BITS);
41}
42
43
44
45
46static struct vmci_resource *vmci_resource_lookup(struct vmci_handle handle,
47 enum vmci_resource_type type)
48{
49 struct vmci_resource *r, *resource = NULL;
50 unsigned int idx = vmci_resource_hash(handle);
51
52 rcu_read_lock();
53 hlist_for_each_entry_rcu(r,
54 &vmci_resource_table.entries[idx], node) {
55 u32 cid = r->handle.context;
56 u32 rid = r->handle.resource;
57
58 if (r->type == type &&
59 rid == handle.resource &&
60 (cid == handle.context || cid == VMCI_INVALID_ID)) {
61 resource = r;
62 break;
63 }
64 }
65 rcu_read_unlock();
66
67 return resource;
68}
69
70
71
72
73
74
75
76static u32 vmci_resource_find_id(u32 context_id,
77 enum vmci_resource_type resource_type)
78{
79 static u32 resource_id = VMCI_RESERVED_RESOURCE_ID_MAX + 1;
80 u32 old_rid = resource_id;
81 u32 current_rid;
82
83
84
85
86
87 do {
88 struct vmci_handle handle;
89
90 current_rid = resource_id;
91 resource_id++;
92 if (unlikely(resource_id == VMCI_INVALID_ID)) {
93
94 resource_id = VMCI_RESERVED_RESOURCE_ID_MAX + 1;
95 }
96
97 handle = vmci_make_handle(context_id, current_rid);
98 if (!vmci_resource_lookup(handle, resource_type))
99 return current_rid;
100 } while (resource_id != old_rid);
101
102 return VMCI_INVALID_ID;
103}
104
105
106int vmci_resource_add(struct vmci_resource *resource,
107 enum vmci_resource_type resource_type,
108 struct vmci_handle handle)
109
110{
111 unsigned int idx;
112 int result;
113
114 spin_lock(&vmci_resource_table.lock);
115
116 if (handle.resource == VMCI_INVALID_ID) {
117 handle.resource = vmci_resource_find_id(handle.context,
118 resource_type);
119 if (handle.resource == VMCI_INVALID_ID) {
120 result = VMCI_ERROR_NO_HANDLE;
121 goto out;
122 }
123 } else if (vmci_resource_lookup(handle, resource_type)) {
124 result = VMCI_ERROR_ALREADY_EXISTS;
125 goto out;
126 }
127
128 resource->handle = handle;
129 resource->type = resource_type;
130 INIT_HLIST_NODE(&resource->node);
131 kref_init(&resource->kref);
132 init_completion(&resource->done);
133
134 idx = vmci_resource_hash(resource->handle);
135 hlist_add_head_rcu(&resource->node, &vmci_resource_table.entries[idx]);
136
137 result = VMCI_SUCCESS;
138
139out:
140 spin_unlock(&vmci_resource_table.lock);
141 return result;
142}
143
144void vmci_resource_remove(struct vmci_resource *resource)
145{
146 struct vmci_handle handle = resource->handle;
147 unsigned int idx = vmci_resource_hash(handle);
148 struct vmci_resource *r;
149
150
151 spin_lock(&vmci_resource_table.lock);
152
153 hlist_for_each_entry(r, &vmci_resource_table.entries[idx], node) {
154 if (vmci_handle_is_equal(r->handle, resource->handle)) {
155 hlist_del_init_rcu(&r->node);
156 break;
157 }
158 }
159
160 spin_unlock(&vmci_resource_table.lock);
161 synchronize_rcu();
162
163 vmci_resource_put(resource);
164 wait_for_completion(&resource->done);
165}
166
167struct vmci_resource *
168vmci_resource_by_handle(struct vmci_handle resource_handle,
169 enum vmci_resource_type resource_type)
170{
171 struct vmci_resource *r, *resource = NULL;
172
173 rcu_read_lock();
174
175 r = vmci_resource_lookup(resource_handle, resource_type);
176 if (r &&
177 (resource_type == r->type ||
178 resource_type == VMCI_RESOURCE_TYPE_ANY)) {
179 resource = vmci_resource_get(r);
180 }
181
182 rcu_read_unlock();
183
184 return resource;
185}
186
187
188
189
190struct vmci_resource *vmci_resource_get(struct vmci_resource *resource)
191{
192 kref_get(&resource->kref);
193
194 return resource;
195}
196
197static void vmci_release_resource(struct kref *kref)
198{
199 struct vmci_resource *resource =
200 container_of(kref, struct vmci_resource, kref);
201
202
203 WARN_ON(!hlist_unhashed(&resource->node));
204
205
206 complete(&resource->done);
207}
208
209
210
211
212
213
214
215int vmci_resource_put(struct vmci_resource *resource)
216{
217
218
219
220
221 return kref_put(&resource->kref, vmci_release_resource) ?
222 VMCI_SUCCESS_ENTRY_DEAD : VMCI_SUCCESS;
223}
224
225struct vmci_handle vmci_resource_handle(struct vmci_resource *resource)
226{
227 return resource->handle;
228}
229