From: "Andrew Stiegmann (stieg)" <astiegmann@vmware.com>
To: linux-kernel@vger.kernel.org
Cc: acking@vmware.com, dtor@vmware.com, dsouders@vmware.com,
cschamp@vmware.com, gregkh@linuxfoundation.org,
akpm@linux-foundation.org,
virtualization@lists.linux-foundation.org,
"Andrew Stiegmann (stieg)" <astiegmann@vmware.com>
Subject: [vmw_vmci RFC 09/11] Apply VMCI resource code
Date: Tue, 15 May 2012 08:07:06 -0700 [thread overview]
Message-ID: <1337094428-20453-10-git-send-email-astiegmann@vmware.com> (raw)
In-Reply-To: <1337094428-20453-1-git-send-email-astiegmann@vmware.com>
Tracks all used resources within the vmci code.
Signed-off-by: Andrew Stiegmann (stieg) <astiegmann@vmware.com>
---
drivers/misc/vmw_vmci/vmci_resource.c | 320 +++++++++++++++++++++++++++++++++
drivers/misc/vmw_vmci/vmci_resource.h | 61 +++++++
2 files changed, 381 insertions(+), 0 deletions(-)
create mode 100644 drivers/misc/vmw_vmci/vmci_resource.c
create mode 100644 drivers/misc/vmw_vmci/vmci_resource.h
diff --git a/drivers/misc/vmw_vmci/vmci_resource.c b/drivers/misc/vmw_vmci/vmci_resource.c
new file mode 100644
index 0000000..240654d
--- /dev/null
+++ b/drivers/misc/vmw_vmci/vmci_resource.c
@@ -0,0 +1,320 @@
+/*
+ * VMware VMCI Driver
+ *
+ * Copyright (C) 2012 VMware, Inc. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation version 2 and no later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <linux/vmw_vmci_defs.h>
+
+#include "vmci_common_int.h"
+#include "vmci_hash_table.h"
+#include "vmci_resource.h"
+#include "vmci_driver.h"
+
+/* 0 through VMCI_RESERVED_RESOURCE_ID_MAX are reserved. */
+static uint32_t resourceID = VMCI_RESERVED_RESOURCE_ID_MAX + 1;
+static spinlock_t resourceIdLock;
+static struct vmci_hash_table *resourceTable = NULL;
+
+/* Public Resource Access Control API. */
+
+/*
+ *------------------------------------------------------------------------------
+ *
+ * vmci_resource_init --
+ *
+ * Initializes the VMCI Resource Access Control API. Creates a hashtable
+ * to hold all resources, and registers vectors and callbacks for
+ * hypercalls.
+ *
+ * Results:
+ * None.
+ *
+ * Side effects:
+ * None.
+ *
+ *------------------------------------------------------------------------------
+ */
+
+int __init vmci_resource_init(void)
+{
+ spin_lock_init(&resourceIdLock);
+
+ resourceTable = vmci_hash_create(128);
+ if (resourceTable == NULL) {
+ pr_warn("Failed creating a resource hash table.");
+ return VMCI_ERROR_NO_MEM;
+ }
+
+ return VMCI_SUCCESS;
+}
+
+/*
+ *------------------------------------------------------------------------------
+ *
+ * vmci_resource_exit --
+ *
+ * Cleans up resources.
+ *
+ * Results:
+ * None.
+ *
+ * Side effects:
+ * None.
+ *
+ *------------------------------------------------------------------------------
+ */
+
+void vmci_resource_exit(void)
+{
+ if (resourceTable)
+ vmci_hash_destroy(resourceTable);
+}
+
+/*
+ *------------------------------------------------------------------------------
+ *
+ * vmci_resource_get_id --
+ *
+ * Return resource ID. The first VMCI_RESERVED_RESOURCE_ID_MAX are
+ * reserved so we start from its value + 1.
+ *
+ * Result:
+ * VMCI resource id on success, VMCI_INVALID_ID on failure.
+ *
+ * Side effects:
+ * None.
+ *
+ *
+ *------------------------------------------------------------------------------
+ */
+
+uint32_t vmci_resource_get_id(uint32_t contextID)
+{
+ uint32_t oldRID = resourceID;
+ uint32_t currentRID;
+ bool foundRID = false;
+
+ /*
+ * Generate a unique resource ID. Keep on trying until we wrap around
+ * in the RID space.
+ */
+ ASSERT(oldRID > VMCI_RESERVED_RESOURCE_ID_MAX);
+
+ do {
+ struct vmci_handle handle;
+
+ spin_lock(&resourceIdLock);
+ currentRID = resourceID;
+ handle = vmci_make_handle(contextID, currentRID);
+ resourceID++;
+ if (unlikely(resourceID == VMCI_INVALID_ID)) {
+ /* Skip the reserved rids. */
+
+ resourceID = VMCI_RESERVED_RESOURCE_ID_MAX + 1;
+ }
+ spin_unlock(&resourceIdLock);
+ foundRID = !vmci_hash_exists(resourceTable, handle);
+ } while (!foundRID && resourceID != oldRID);
+
+ return (unlikely(!foundRID)) ? VMCI_INVALID_ID : currentRID;
+}
+
+/*
+ *------------------------------------------------------------------------------
+ *
+ * vmci_resource_add --
+ *
+ * Results:
+ * VMCI_SUCCESS if successful, error code if not.
+ *
+ * Side effects:
+ * None.
+ *
+ *------------------------------------------------------------------------------
+ */
+
+int vmci_resource_add(struct vmci_resource *resource, // IN
+ enum vmci_resource_type resourceType, // IN
+ struct vmci_handle resourceHandle, // IN
+ VMCIResourceFreeCB containerFreeCB, // IN
+ void *containerObject) // IN
+{
+ int result;
+
+ ASSERT(resource);
+
+ if (VMCI_HANDLE_EQUAL(resourceHandle, VMCI_INVALID_HANDLE)) {
+ pr_devel("Invalid argument resource (handle=0x%x:0x%x)",
+ resourceHandle.context, resourceHandle.resource);
+ return VMCI_ERROR_INVALID_ARGS;
+ }
+
+ vmci_hash_init_entry(&resource->hashEntry, resourceHandle);
+ resource->type = resourceType;
+ resource->containerFreeCB = containerFreeCB;
+ resource->containerObject = containerObject;
+
+ /* Add resource to hashtable. */
+ result = vmci_hash_add(resourceTable, &resource->hashEntry);
+ if (result != VMCI_SUCCESS) {
+ pr_devel("Failed to add entry to hash table "
+ "(result=%d).", result);
+ return result;
+ }
+
+ return result;
+}
+
+/*
+ *------------------------------------------------------------------------------
+ *
+ * vmci_resource_remove --
+ *
+ * Results:
+ * None.
+ *
+ * Side effects:
+ * None.
+ *
+ *------------------------------------------------------------------------------
+ */
+
+void vmci_resource_remove(struct vmci_handle resourceHandle, // IN:
+ enum vmci_resource_type resourceType) // IN:
+{
+ struct vmci_resource *resource =
+ vmci_resource_get(resourceHandle, resourceType);
+
+ if (resource == NULL)
+ return;
+
+ /* Remove resource from hashtable. */
+ vmci_hash_remove(resourceTable, &resource->hashEntry);
+
+ vmci_resource_release(resource);
+ /* resource could be freed by now. */
+}
+
+/*
+ *------------------------------------------------------------------------------
+ *
+ * vmci_resource_get --
+ *
+ * Results:
+ * Resource is successful. Otherwise NULL.
+ *
+ * Side effects:
+ * None.
+ *
+ *------------------------------------------------------------------------------
+ */
+
+struct vmci_resource *vmci_resource_get(struct vmci_handle resourceHandle, // IN
+ enum vmci_resource_type resourceType) // IN
+{
+ struct vmci_resource *resource;
+ struct vmci_hash_entry *entry =
+ vmci_hash_get(resourceTable, resourceHandle);
+
+ if (entry == NULL)
+ return NULL;
+
+ resource = container_of(entry, struct vmci_resource, hashEntry);
+ if (resourceType == VMCI_RESOURCE_TYPE_ANY ||
+ resource->type == resourceType)
+ return resource;
+
+ vmci_hash_release(resourceTable, entry);
+ return NULL;
+}
+
+/*
+ *------------------------------------------------------------------------------
+ *
+ * vmci_resource_hold --
+ *
+ * Hold the given resource. This will hold the hashtable entry. This
+ * is like doing a Get() but without having to lookup the resource by
+ * handle.
+ *
+ * Results:
+ * None.
+ *
+ * Side effects:
+ * None.
+ *
+ *------------------------------------------------------------------------------
+ */
+
+void vmci_resource_hold(struct vmci_resource *resource)
+{
+ ASSERT(resource);
+ vmci_hash_hold(resourceTable, &resource->hashEntry);
+}
+
+/*
+ *------------------------------------------------------------------------------
+ *
+ * vmci_resource_release --
+ *
+ * Results:
+ * None.
+ *
+ * Side effects:
+ * resource's containerFreeCB will get called if last reference.
+ *
+ *------------------------------------------------------------------------------
+ */
+
+int vmci_resource_release(struct vmci_resource *resource)
+{
+ int result;
+
+ ASSERT(resource);
+
+ result = vmci_hash_release(resourceTable, &resource->hashEntry);
+ if (result == VMCI_SUCCESS_ENTRY_DEAD && resource->containerFreeCB)
+ resource->containerFreeCB(resource->containerObject);
+
+ /*
+ * We propagate the information back to caller in case it wants to know
+ * whether entry was freed.
+ */
+ return result;
+}
+
+/*
+ *------------------------------------------------------------------------------
+ *
+ * vmci_resource_handle --
+ *
+ * Get the handle for the given resource.
+ *
+ * Results:
+ * The resource's associated handle.
+ *
+ * Side effects:
+ * None.
+ *
+ *------------------------------------------------------------------------------
+ */
+
+struct vmci_handle vmci_resource_handle(struct vmci_resource *resource)
+{
+ ASSERT(resource);
+ return resource->hashEntry.handle;
+}
diff --git a/drivers/misc/vmw_vmci/vmci_resource.h b/drivers/misc/vmw_vmci/vmci_resource.h
new file mode 100644
index 0000000..f6abb1e
--- /dev/null
+++ b/drivers/misc/vmw_vmci/vmci_resource.h
@@ -0,0 +1,61 @@
+/*
+ * VMware VMCI Driver
+ *
+ * Copyright (C) 2012 VMware, Inc. All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation version 2 and no later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef _VMCI_RESOURCE_H_
+#define _VMCI_RESOURCE_H_
+
+#include <linux/vmw_vmci_defs.h>
+
+#include "vmci_context.h"
+#include "vmci_hash_table.h"
+
+typedef void (*VMCIResourceFreeCB) (void *resource);
+
+enum vmci_resource_type {
+ VMCI_RESOURCE_TYPE_ANY,
+ VMCI_RESOURCE_TYPE_API,
+ VMCI_RESOURCE_TYPE_GROUP,
+ VMCI_RESOURCE_TYPE_DATAGRAM,
+ VMCI_RESOURCE_TYPE_DOORBELL,
+};
+
+struct vmci_resource {
+ struct vmci_hash_entry hashEntry;
+ enum vmci_resource_type type;
+ VMCIResourceFreeCB containerFreeCB; // Callback to free container
+ /* object when refCount is 0. */
+ void *containerObject; // Container object reference.
+};
+
+int vmci_resource_init(void);
+void vmci_resource_exit(void);
+uint32_t vmci_resource_get_id(uint32_t contextID);
+int vmci_resource_add(struct vmci_resource *resource,
+ enum vmci_resource_type resourceType,
+ struct vmci_handle resourceHandle,
+ VMCIResourceFreeCB containerFreeCB, void *containerObject);
+void vmci_resource_remove(struct vmci_handle resourceHandle,
+ enum vmci_resource_type resourceType);
+struct vmci_resource *vmci_resource_get(struct vmci_handle resourceHandle,
+ enum vmci_resource_type resourceType);
+void vmci_resource_hold(struct vmci_resource *resource);
+int vmci_resource_release(struct vmci_resource *resource);
+struct vmci_handle vmci_resource_handle(struct vmci_resource *resource);
+
+#endif /* _VMCI_RESOURCE_H_ */
--
1.7.0.4
next prev parent reply other threads:[~2012-05-15 15:07 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-05-15 15:06 [vmw_vmci RFC 00/11] VMCI for Linux Andrew Stiegmann (stieg)
2012-05-15 15:06 ` [vmw_vmci RFC 01/11] Apply VMCI context code Andrew Stiegmann (stieg)
2012-05-15 23:47 ` Greg KH
2012-05-16 17:01 ` Stephen Hemminger
2012-05-16 18:34 ` Andrew Stiegmann
2012-05-15 15:06 ` [vmw_vmci RFC 02/11] Apply VMCI datagram code Andrew Stiegmann (stieg)
2012-05-15 15:07 ` [vmw_vmci RFC 03/11] Apply VMCI doorbell code Andrew Stiegmann (stieg)
2012-05-15 15:07 ` [vmw_vmci RFC 04/11] Apply VMCI driver code Andrew Stiegmann (stieg)
2012-05-15 15:07 ` [vmw_vmci RFC 05/11] Apply VMCI event code Andrew Stiegmann (stieg)
2012-05-15 15:07 ` [vmw_vmci RFC 06/11] Apply dynamic array code Andrew Stiegmann (stieg)
2012-05-15 15:07 ` [vmw_vmci RFC 07/11] Apply VMCI hash table Andrew Stiegmann (stieg)
2012-05-15 15:07 ` [vmw_vmci RFC 08/11] Apply VMCI queue pairs Andrew Stiegmann (stieg)
2012-05-15 15:07 ` Andrew Stiegmann (stieg) [this message]
2012-05-15 15:07 ` [vmw_vmci RFC 10/11] Apply vmci routing code Andrew Stiegmann (stieg)
2012-05-15 15:07 ` [vmw_vmci RFC 11/11] Apply the header code to make VMCI build Andrew Stiegmann (stieg)
2012-05-15 23:50 ` [vmw_vmci RFC 00/11] VMCI for Linux Greg KH
2012-05-16 8:55 ` Dor Laor
2012-06-01 15:33 ` Andy King
2012-06-04 22:57 ` Greg KH
2012-06-05 7:02 ` Dmitry Torokhov
2012-06-06 5:06 ` Greg KH
2012-06-14 11:52 ` Dor Laor
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1337094428-20453-10-git-send-email-astiegmann@vmware.com \
--to=astiegmann@vmware.com \
--cc=acking@vmware.com \
--cc=akpm@linux-foundation.org \
--cc=cschamp@vmware.com \
--cc=dsouders@vmware.com \
--cc=dtor@vmware.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=virtualization@lists.linux-foundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome