[PATCH RFC v15 13/30] virt: gunyah: Add resource tickets
From: Elliot Berman
Date: Fri Dec 15 2023 - 19:23:22 EST
Some VM functions need to acquire Gunyah resources. For instance, Gunyah
vCPUs are exposed to the host as a resource. The Gunyah vCPU function
will register a resource ticket and be able to interact with the
hypervisor once the resource ticket is filled.
Resource tickets are the mechanism for functions to acquire ownership of
Gunyah resources. Gunyah functions can be created before the VM's
resources are created and made available to Linux. A resource ticket
identifies a type of resource and a label of a resource which the ticket
holder is interested in.
Resources are created by Gunyah as configured in the VM's devicetree
configuration. Gunyah doesn't process the label and that makes it
possible for userspace to create multiple resources with the same label.
Resource ticket owners need to be prepared for populate to be called
multiple times if userspace created multiple resources with the same
label.
Reviewed-by: Alex Elder <elder@xxxxxxxxxx>
Signed-off-by: Elliot Berman <quic_eberman@xxxxxxxxxxx>
---
drivers/virt/gunyah/vm_mgr.c | 127 +++++++++++++++++++++++++++++++++++++++++-
drivers/virt/gunyah/vm_mgr.h | 3 +
include/linux/gunyah_vm_mgr.h | 36 ++++++++++++
3 files changed, 165 insertions(+), 1 deletion(-)
diff --git a/drivers/virt/gunyah/vm_mgr.c b/drivers/virt/gunyah/vm_mgr.c
index 81bc73047e1e..fb22459b21c8 100644
--- a/drivers/virt/gunyah/vm_mgr.c
+++ b/drivers/virt/gunyah/vm_mgr.c
@@ -185,6 +185,106 @@ void gunyah_vm_function_unregister(struct gunyah_vm_function *fn)
}
EXPORT_SYMBOL_GPL(gunyah_vm_function_unregister);
+int gunyah_vm_add_resource_ticket(struct gunyah_vm *ghvm,
+ struct gunyah_vm_resource_ticket *ticket)
+{
+ struct gunyah_vm_resource_ticket *iter;
+ struct gunyah_resource *ghrsc, *rsc_iter;
+ int ret = 0;
+
+ mutex_lock(&ghvm->resources_lock);
+ list_for_each_entry(iter, &ghvm->resource_tickets, vm_list) {
+ if (iter->resource_type == ticket->resource_type &&
+ iter->label == ticket->label) {
+ ret = -EEXIST;
+ goto out;
+ }
+ }
+
+ if (!try_module_get(ticket->owner)) {
+ ret = -ENODEV;
+ goto out;
+ }
+
+ list_add(&ticket->vm_list, &ghvm->resource_tickets);
+ INIT_LIST_HEAD(&ticket->resources);
+
+ list_for_each_entry_safe(ghrsc, rsc_iter, &ghvm->resources, list) {
+ if (ghrsc->type == ticket->resource_type &&
+ ghrsc->rm_label == ticket->label) {
+ if (ticket->populate(ticket, ghrsc))
+ list_move(&ghrsc->list, &ticket->resources);
+ }
+ }
+out:
+ mutex_unlock(&ghvm->resources_lock);
+ return ret;
+}
+EXPORT_SYMBOL_GPL(gunyah_vm_add_resource_ticket);
+
+void gunyah_vm_remove_resource_ticket(struct gunyah_vm *ghvm,
+ struct gunyah_vm_resource_ticket *ticket)
+{
+ struct gunyah_resource *ghrsc, *iter;
+
+ mutex_lock(&ghvm->resources_lock);
+ list_for_each_entry_safe(ghrsc, iter, &ticket->resources, list) {
+ ticket->unpopulate(ticket, ghrsc);
+ list_move(&ghrsc->list, &ghvm->resources);
+ }
+
+ module_put(ticket->owner);
+ list_del(&ticket->vm_list);
+ mutex_unlock(&ghvm->resources_lock);
+}
+EXPORT_SYMBOL_GPL(gunyah_vm_remove_resource_ticket);
+
+static void gunyah_vm_add_resource(struct gunyah_vm *ghvm,
+ struct gunyah_resource *ghrsc)
+{
+ struct gunyah_vm_resource_ticket *ticket;
+
+ mutex_lock(&ghvm->resources_lock);
+ list_for_each_entry(ticket, &ghvm->resource_tickets, vm_list) {
+ if (ghrsc->type == ticket->resource_type &&
+ ghrsc->rm_label == ticket->label) {
+ if (ticket->populate(ticket, ghrsc))
+ list_add(&ghrsc->list, &ticket->resources);
+ else
+ list_add(&ghrsc->list, &ghvm->resources);
+ /* unconditonal -- we prevent multiple identical
+ * resource tickets so there will not be some other
+ * ticket elsewhere in the list if populate() failed.
+ */
+ goto found;
+ }
+ }
+ list_add(&ghrsc->list, &ghvm->resources);
+found:
+ mutex_unlock(&ghvm->resources_lock);
+}
+
+static void gunyah_vm_clean_resources(struct gunyah_vm *ghvm)
+{
+ struct gunyah_vm_resource_ticket *ticket, *titer;
+ struct gunyah_resource *ghrsc, *riter;
+
+ mutex_lock(&ghvm->resources_lock);
+ if (!list_empty(&ghvm->resource_tickets)) {
+ dev_warn(ghvm->parent, "Dangling resource tickets:\n");
+ list_for_each_entry_safe(ticket, titer, &ghvm->resource_tickets,
+ vm_list) {
+ dev_warn(ghvm->parent, " %pS\n", ticket->populate);
+ gunyah_vm_remove_resource_ticket(ghvm, ticket);
+ }
+ }
+
+ list_for_each_entry_safe(ghrsc, riter, &ghvm->resources, list) {
+ gunyah_rm_free_resource(ghrsc);
+ }
+ mutex_unlock(&ghvm->resources_lock);
+}
+
static int gunyah_vm_rm_notification_status(struct gunyah_vm *ghvm, void *data)
{
struct gunyah_rm_vm_status_payload *payload = data;
@@ -263,6 +363,9 @@ static __must_check struct gunyah_vm *gunyah_vm_alloc(struct gunyah_rm *rm)
init_waitqueue_head(&ghvm->vm_status_wait);
kref_init(&ghvm->kref);
ghvm->vm_status = GUNYAH_RM_VM_STATUS_NO_STATE;
+ mutex_init(&ghvm->resources_lock);
+ INIT_LIST_HEAD(&ghvm->resources);
+ INIT_LIST_HEAD(&ghvm->resource_tickets);
INIT_LIST_HEAD(&ghvm->functions);
mutex_init(&ghvm->fn_lock);
@@ -271,7 +374,9 @@ static __must_check struct gunyah_vm *gunyah_vm_alloc(struct gunyah_rm *rm)
static int gunyah_vm_start(struct gunyah_vm *ghvm)
{
- int ret;
+ struct gunyah_rm_hyp_resources *resources;
+ struct gunyah_resource *ghrsc;
+ int ret, i, n;
down_write(&ghvm->status_lock);
if (ghvm->vm_status != GUNYAH_RM_VM_STATUS_NO_STATE) {
@@ -307,6 +412,25 @@ static int gunyah_vm_start(struct gunyah_vm *ghvm)
}
ghvm->vm_status = GUNYAH_RM_VM_STATUS_READY;
+ ret = gunyah_rm_get_hyp_resources(ghvm->rm, ghvm->vmid, &resources);
+ if (ret) {
+ dev_warn(ghvm->parent,
+ "Failed to get hypervisor resources for VM: %d\n",
+ ret);
+ goto err;
+ }
+
+ for (i = 0, n = le32_to_cpu(resources->n_entries); i < n; i++) {
+ ghrsc = gunyah_rm_alloc_resource(ghvm->rm,
+ &resources->entries[i]);
+ if (!ghrsc) {
+ ret = -ENOMEM;
+ goto err;
+ }
+
+ gunyah_vm_add_resource(ghvm, ghrsc);
+ }
+
ret = gunyah_rm_vm_start(ghvm->rm, ghvm->vmid);
if (ret) {
dev_warn(ghvm->parent, "Failed to start VM: %d\n", ret);
@@ -404,6 +528,7 @@ static void _gunyah_vm_put(struct kref *kref)
gunyah_vm_stop(ghvm);
gunyah_vm_remove_functions(ghvm);
+ gunyah_vm_clean_resources(ghvm);
if (ghvm->vm_status != GUNYAH_RM_VM_STATUS_NO_STATE &&
ghvm->vm_status != GUNYAH_RM_VM_STATUS_LOAD &&
diff --git a/drivers/virt/gunyah/vm_mgr.h b/drivers/virt/gunyah/vm_mgr.h
index a9e0eaf212fd..b94f6d16b787 100644
--- a/drivers/virt/gunyah/vm_mgr.h
+++ b/drivers/virt/gunyah/vm_mgr.h
@@ -34,6 +34,9 @@ struct gunyah_vm {
struct kref kref;
struct mutex fn_lock;
struct list_head functions;
+ struct mutex resources_lock;
+ struct list_head resources;
+ struct list_head resource_tickets;
};
#endif
diff --git a/include/linux/gunyah_vm_mgr.h b/include/linux/gunyah_vm_mgr.h
index f40e57f32fd8..b02c6e89b207 100644
--- a/include/linux/gunyah_vm_mgr.h
+++ b/include/linux/gunyah_vm_mgr.h
@@ -97,4 +97,40 @@ void gunyah_vm_function_unregister(struct gunyah_vm_function *f);
module_gunyah_vm_function(_name); \
MODULE_ALIAS_GUNYAH_VM_FUNCTION(_type, _idx)
+/**
+ * struct gunyah_vm_resource_ticket - Represents a ticket to reserve access to VM resource(s)
+ * @vm_list: for @gunyah_vm->resource_tickets
+ * @resources: List of resource(s) associated with this ticket
+ * (members are from @gunyah_resource->list)
+ * @resource_type: Type of resource this ticket reserves
+ * @label: Label of the resource from resource manager this ticket reserves.
+ * @owner: owner of the ticket
+ * @populate: callback provided by the ticket owner and called when a resource is found that
+ * matches @resource_type and @label. Note that this callback could be called
+ * multiple times if userspace created mutliple resources with the same type/label.
+ * This callback may also have significant delay after gunyah_vm_add_resource_ticket()
+ * since gunyah_vm_add_resource_ticket() could be called before the VM starts.
+ * @unpopulate: callback provided by the ticket owner and called when the ticket owner should no
+ * longer use the resource provided in the argument. When unpopulate() returns,
+ * the ticket owner should not be able to use the resource any more as the resource
+ * might being freed.
+ */
+struct gunyah_vm_resource_ticket {
+ struct list_head vm_list;
+ struct list_head resources;
+ enum gunyah_resource_type resource_type;
+ u32 label;
+
+ struct module *owner;
+ bool (*populate)(struct gunyah_vm_resource_ticket *ticket,
+ struct gunyah_resource *ghrsc);
+ void (*unpopulate)(struct gunyah_vm_resource_ticket *ticket,
+ struct gunyah_resource *ghrsc);
+};
+
+int gunyah_vm_add_resource_ticket(struct gunyah_vm *ghvm,
+ struct gunyah_vm_resource_ticket *ticket);
+void gunyah_vm_remove_resource_ticket(struct gunyah_vm *ghvm,
+ struct gunyah_vm_resource_ticket *ticket);
+
#endif
--
2.43.0