Merge tag 'm68knommu-for-v5.9-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux/fpc-iii.git] / drivers / misc / vmw_vmci / vmci_resource.c
blob692daa9eff34115f8766a1128365cef7a3b4f4f3
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * VMware VMCI Driver
5 * Copyright (C) 2012 VMware, Inc. All rights reserved.
6 */
8 #include <linux/vmw_vmci_defs.h>
9 #include <linux/hash.h>
10 #include <linux/types.h>
11 #include <linux/rculist.h>
12 #include <linux/completion.h>
14 #include "vmci_resource.h"
15 #include "vmci_driver.h"
18 #define VMCI_RESOURCE_HASH_BITS 7
19 #define VMCI_RESOURCE_HASH_BUCKETS (1 << VMCI_RESOURCE_HASH_BITS)
21 struct vmci_hash_table {
22 spinlock_t lock;
23 struct hlist_head entries[VMCI_RESOURCE_HASH_BUCKETS];
26 static struct vmci_hash_table vmci_resource_table = {
27 .lock = __SPIN_LOCK_UNLOCKED(vmci_resource_table.lock),
30 static unsigned int vmci_resource_hash(struct vmci_handle handle)
32 return hash_32(handle.resource, VMCI_RESOURCE_HASH_BITS);
36 * Gets a resource (if one exists) matching given handle from the hash table.
38 static struct vmci_resource *vmci_resource_lookup(struct vmci_handle handle,
39 enum vmci_resource_type type)
41 struct vmci_resource *r, *resource = NULL;
42 unsigned int idx = vmci_resource_hash(handle);
44 rcu_read_lock();
45 hlist_for_each_entry_rcu(r,
46 &vmci_resource_table.entries[idx], node) {
47 u32 cid = r->handle.context;
48 u32 rid = r->handle.resource;
50 if (r->type == type &&
51 rid == handle.resource &&
52 (cid == handle.context || cid == VMCI_INVALID_ID ||
53 handle.context == VMCI_INVALID_ID)) {
54 resource = r;
55 break;
58 rcu_read_unlock();
60 return resource;
64 * Find an unused resource ID and return it. The first
65 * VMCI_RESERVED_RESOURCE_ID_MAX are reserved so we start from
66 * its value + 1.
67 * Returns VMCI resource id on success, VMCI_INVALID_ID on failure.
69 static u32 vmci_resource_find_id(u32 context_id,
70 enum vmci_resource_type resource_type)
72 static u32 resource_id = VMCI_RESERVED_RESOURCE_ID_MAX + 1;
73 u32 old_rid = resource_id;
74 u32 current_rid;
77 * Generate a unique resource ID. Keep on trying until we wrap around
78 * in the RID space.
80 do {
81 struct vmci_handle handle;
83 current_rid = resource_id;
84 resource_id++;
85 if (unlikely(resource_id == VMCI_INVALID_ID)) {
86 /* Skip the reserved rids. */
87 resource_id = VMCI_RESERVED_RESOURCE_ID_MAX + 1;
90 handle = vmci_make_handle(context_id, current_rid);
91 if (!vmci_resource_lookup(handle, resource_type))
92 return current_rid;
93 } while (resource_id != old_rid);
95 return VMCI_INVALID_ID;
99 int vmci_resource_add(struct vmci_resource *resource,
100 enum vmci_resource_type resource_type,
101 struct vmci_handle handle)
104 unsigned int idx;
105 int result;
107 spin_lock(&vmci_resource_table.lock);
109 if (handle.resource == VMCI_INVALID_ID) {
110 handle.resource = vmci_resource_find_id(handle.context,
111 resource_type);
112 if (handle.resource == VMCI_INVALID_ID) {
113 result = VMCI_ERROR_NO_HANDLE;
114 goto out;
116 } else if (vmci_resource_lookup(handle, resource_type)) {
117 result = VMCI_ERROR_ALREADY_EXISTS;
118 goto out;
121 resource->handle = handle;
122 resource->type = resource_type;
123 INIT_HLIST_NODE(&resource->node);
124 kref_init(&resource->kref);
125 init_completion(&resource->done);
127 idx = vmci_resource_hash(resource->handle);
128 hlist_add_head_rcu(&resource->node, &vmci_resource_table.entries[idx]);
130 result = VMCI_SUCCESS;
132 out:
133 spin_unlock(&vmci_resource_table.lock);
134 return result;
137 void vmci_resource_remove(struct vmci_resource *resource)
139 struct vmci_handle handle = resource->handle;
140 unsigned int idx = vmci_resource_hash(handle);
141 struct vmci_resource *r;
143 /* Remove resource from hash table. */
144 spin_lock(&vmci_resource_table.lock);
146 hlist_for_each_entry(r, &vmci_resource_table.entries[idx], node) {
147 if (vmci_handle_is_equal(r->handle, resource->handle)) {
148 hlist_del_init_rcu(&r->node);
149 break;
153 spin_unlock(&vmci_resource_table.lock);
154 synchronize_rcu();
156 vmci_resource_put(resource);
157 wait_for_completion(&resource->done);
160 struct vmci_resource *
161 vmci_resource_by_handle(struct vmci_handle resource_handle,
162 enum vmci_resource_type resource_type)
164 struct vmci_resource *r, *resource = NULL;
166 rcu_read_lock();
168 r = vmci_resource_lookup(resource_handle, resource_type);
169 if (r &&
170 (resource_type == r->type ||
171 resource_type == VMCI_RESOURCE_TYPE_ANY)) {
172 resource = vmci_resource_get(r);
175 rcu_read_unlock();
177 return resource;
181 * Get a reference to given resource.
183 struct vmci_resource *vmci_resource_get(struct vmci_resource *resource)
185 kref_get(&resource->kref);
187 return resource;
190 static void vmci_release_resource(struct kref *kref)
192 struct vmci_resource *resource =
193 container_of(kref, struct vmci_resource, kref);
195 /* Verify the resource has been unlinked from hash table */
196 WARN_ON(!hlist_unhashed(&resource->node));
198 /* Signal that container of this resource can now be destroyed */
199 complete(&resource->done);
203 * Resource's release function will get called if last reference.
204 * If it is the last reference, then we are sure that nobody else
205 * can increment the count again (it's gone from the resource hash
206 * table), so there's no need for locking here.
208 int vmci_resource_put(struct vmci_resource *resource)
211 * We propagate the information back to caller in case it wants to know
212 * whether entry was freed.
214 return kref_put(&resource->kref, vmci_release_resource) ?
215 VMCI_SUCCESS_ENTRY_DEAD : VMCI_SUCCESS;
218 struct vmci_handle vmci_resource_handle(struct vmci_resource *resource)
220 return resource->handle;