Commit 43531dc6 authored by Maaz Mombasawala's avatar Maaz Mombasawala Committed by Zack Rusin

drm/vmwgfx: Refactor resource manager's hashtable to use linux/hashtable implementation.

Vmwgfx's hashtab implementation needs to be replaced with linux/hashtable
to reduce maintenance burden.
Refactor cmdbuf resource manager to use linux/hashtable.h implementation
as part of this effort.
Signed-off-by: default avatarMaaz Mombasawala <mombasawalam@vmware.com>
Reviewed-by: default avatarZack Rusin <zackr@vmware.com>
Reviewed-by: default avatarMartin Krastev <krastevm@vmware.com>
Signed-off-by: default avatarZack Rusin <zackr@vmware.com>
Link: https://patchwork.freedesktop.org/patch/msgid/20221022040236.616490-4-zack@kde.org
parent 32807063
// SPDX-License-Identifier: GPL-2.0 OR MIT // SPDX-License-Identifier: GPL-2.0 OR MIT
/************************************************************************** /**************************************************************************
* *
* Copyright 2014-2015 VMware, Inc., Palo Alto, CA., USA * Copyright 2014-2022 VMware, Inc., Palo Alto, CA., USA
* *
* Permission is hereby granted, free of charge, to any person obtaining a * Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the * copy of this software and associated documentation files (the
...@@ -28,6 +28,8 @@ ...@@ -28,6 +28,8 @@
#include "vmwgfx_drv.h" #include "vmwgfx_drv.h"
#include "vmwgfx_resource_priv.h" #include "vmwgfx_resource_priv.h"
#include <linux/hashtable.h>
#define VMW_CMDBUF_RES_MAN_HT_ORDER 12 #define VMW_CMDBUF_RES_MAN_HT_ORDER 12
/** /**
...@@ -59,7 +61,7 @@ struct vmw_cmdbuf_res { ...@@ -59,7 +61,7 @@ struct vmw_cmdbuf_res {
* @resources and @list are protected by the cmdbuf mutex for now. * @resources and @list are protected by the cmdbuf mutex for now.
*/ */
struct vmw_cmdbuf_res_manager { struct vmw_cmdbuf_res_manager {
struct vmwgfx_open_hash resources; DECLARE_HASHTABLE(resources, VMW_CMDBUF_RES_MAN_HT_ORDER);
struct list_head list; struct list_head list;
struct vmw_private *dev_priv; struct vmw_private *dev_priv;
}; };
...@@ -82,14 +84,13 @@ vmw_cmdbuf_res_lookup(struct vmw_cmdbuf_res_manager *man, ...@@ -82,14 +84,13 @@ vmw_cmdbuf_res_lookup(struct vmw_cmdbuf_res_manager *man,
u32 user_key) u32 user_key)
{ {
struct vmwgfx_hash_item *hash; struct vmwgfx_hash_item *hash;
int ret;
unsigned long key = user_key | (res_type << 24); unsigned long key = user_key | (res_type << 24);
ret = vmwgfx_ht_find_item(&man->resources, key, &hash); hash_for_each_possible_rcu(man->resources, hash, head, key) {
if (unlikely(ret != 0)) if (hash->key == key)
return ERR_PTR(ret); return drm_hash_entry(hash, struct vmw_cmdbuf_res, hash)->res;
}
return drm_hash_entry(hash, struct vmw_cmdbuf_res, hash)->res; return ERR_PTR(-EINVAL);
} }
/** /**
...@@ -105,7 +106,7 @@ static void vmw_cmdbuf_res_free(struct vmw_cmdbuf_res_manager *man, ...@@ -105,7 +106,7 @@ static void vmw_cmdbuf_res_free(struct vmw_cmdbuf_res_manager *man,
struct vmw_cmdbuf_res *entry) struct vmw_cmdbuf_res *entry)
{ {
list_del(&entry->head); list_del(&entry->head);
WARN_ON(vmwgfx_ht_remove_item(&man->resources, &entry->hash)); hash_del_rcu(&entry->hash.head);
vmw_resource_unreference(&entry->res); vmw_resource_unreference(&entry->res);
kfree(entry); kfree(entry);
} }
...@@ -159,7 +160,6 @@ void vmw_cmdbuf_res_commit(struct list_head *list) ...@@ -159,7 +160,6 @@ void vmw_cmdbuf_res_commit(struct list_head *list)
void vmw_cmdbuf_res_revert(struct list_head *list) void vmw_cmdbuf_res_revert(struct list_head *list)
{ {
struct vmw_cmdbuf_res *entry, *next; struct vmw_cmdbuf_res *entry, *next;
int ret;
list_for_each_entry_safe(entry, next, list, head) { list_for_each_entry_safe(entry, next, list, head) {
switch (entry->state) { switch (entry->state) {
...@@ -167,8 +167,8 @@ void vmw_cmdbuf_res_revert(struct list_head *list) ...@@ -167,8 +167,8 @@ void vmw_cmdbuf_res_revert(struct list_head *list)
vmw_cmdbuf_res_free(entry->man, entry); vmw_cmdbuf_res_free(entry->man, entry);
break; break;
case VMW_CMDBUF_RES_DEL: case VMW_CMDBUF_RES_DEL:
ret = vmwgfx_ht_insert_item(&entry->man->resources, &entry->hash); hash_add_rcu(entry->man->resources, &entry->hash.head,
BUG_ON(ret); entry->hash.key);
list_move_tail(&entry->head, &entry->man->list); list_move_tail(&entry->head, &entry->man->list);
entry->state = VMW_CMDBUF_RES_COMMITTED; entry->state = VMW_CMDBUF_RES_COMMITTED;
break; break;
...@@ -199,26 +199,20 @@ int vmw_cmdbuf_res_add(struct vmw_cmdbuf_res_manager *man, ...@@ -199,26 +199,20 @@ int vmw_cmdbuf_res_add(struct vmw_cmdbuf_res_manager *man,
struct list_head *list) struct list_head *list)
{ {
struct vmw_cmdbuf_res *cres; struct vmw_cmdbuf_res *cres;
int ret;
cres = kzalloc(sizeof(*cres), GFP_KERNEL); cres = kzalloc(sizeof(*cres), GFP_KERNEL);
if (unlikely(!cres)) if (unlikely(!cres))
return -ENOMEM; return -ENOMEM;
cres->hash.key = user_key | (res_type << 24); cres->hash.key = user_key | (res_type << 24);
ret = vmwgfx_ht_insert_item(&man->resources, &cres->hash); hash_add_rcu(man->resources, &cres->hash.head, cres->hash.key);
if (unlikely(ret != 0)) {
kfree(cres);
goto out_invalid_key;
}
cres->state = VMW_CMDBUF_RES_ADD; cres->state = VMW_CMDBUF_RES_ADD;
cres->res = vmw_resource_reference(res); cres->res = vmw_resource_reference(res);
cres->man = man; cres->man = man;
list_add_tail(&cres->head, list); list_add_tail(&cres->head, list);
out_invalid_key: return 0;
return ret;
} }
/** /**
...@@ -243,24 +237,26 @@ int vmw_cmdbuf_res_remove(struct vmw_cmdbuf_res_manager *man, ...@@ -243,24 +237,26 @@ int vmw_cmdbuf_res_remove(struct vmw_cmdbuf_res_manager *man,
struct list_head *list, struct list_head *list,
struct vmw_resource **res_p) struct vmw_resource **res_p)
{ {
struct vmw_cmdbuf_res *entry; struct vmw_cmdbuf_res *entry = NULL;
struct vmwgfx_hash_item *hash; struct vmwgfx_hash_item *hash;
int ret; unsigned long key = user_key | (res_type << 24);
ret = vmwgfx_ht_find_item(&man->resources, user_key | (res_type << 24), hash_for_each_possible_rcu(man->resources, hash, head, key) {
&hash); if (hash->key == key) {
if (likely(ret != 0)) entry = drm_hash_entry(hash, struct vmw_cmdbuf_res, hash);
break;
}
}
if (unlikely(!entry))
return -EINVAL; return -EINVAL;
entry = drm_hash_entry(hash, struct vmw_cmdbuf_res, hash);
switch (entry->state) { switch (entry->state) {
case VMW_CMDBUF_RES_ADD: case VMW_CMDBUF_RES_ADD:
vmw_cmdbuf_res_free(man, entry); vmw_cmdbuf_res_free(man, entry);
*res_p = NULL; *res_p = NULL;
break; break;
case VMW_CMDBUF_RES_COMMITTED: case VMW_CMDBUF_RES_COMMITTED:
(void) vmwgfx_ht_remove_item(&man->resources, &entry->hash); hash_del_rcu(&entry->hash.head);
list_del(&entry->head); list_del(&entry->head);
entry->state = VMW_CMDBUF_RES_DEL; entry->state = VMW_CMDBUF_RES_DEL;
list_add_tail(&entry->head, list); list_add_tail(&entry->head, list);
...@@ -287,7 +283,6 @@ struct vmw_cmdbuf_res_manager * ...@@ -287,7 +283,6 @@ struct vmw_cmdbuf_res_manager *
vmw_cmdbuf_res_man_create(struct vmw_private *dev_priv) vmw_cmdbuf_res_man_create(struct vmw_private *dev_priv)
{ {
struct vmw_cmdbuf_res_manager *man; struct vmw_cmdbuf_res_manager *man;
int ret;
man = kzalloc(sizeof(*man), GFP_KERNEL); man = kzalloc(sizeof(*man), GFP_KERNEL);
if (!man) if (!man)
...@@ -295,12 +290,8 @@ vmw_cmdbuf_res_man_create(struct vmw_private *dev_priv) ...@@ -295,12 +290,8 @@ vmw_cmdbuf_res_man_create(struct vmw_private *dev_priv)
man->dev_priv = dev_priv; man->dev_priv = dev_priv;
INIT_LIST_HEAD(&man->list); INIT_LIST_HEAD(&man->list);
ret = vmwgfx_ht_create(&man->resources, VMW_CMDBUF_RES_MAN_HT_ORDER); hash_init(man->resources);
if (ret == 0) return man;
return man;
kfree(man);
return ERR_PTR(ret);
} }
/** /**
...@@ -320,7 +311,6 @@ void vmw_cmdbuf_res_man_destroy(struct vmw_cmdbuf_res_manager *man) ...@@ -320,7 +311,6 @@ void vmw_cmdbuf_res_man_destroy(struct vmw_cmdbuf_res_manager *man)
list_for_each_entry_safe(entry, next, &man->list, head) list_for_each_entry_safe(entry, next, &man->list, head)
vmw_cmdbuf_res_free(man, entry); vmw_cmdbuf_res_free(man, entry);
vmwgfx_ht_remove(&man->resources);
kfree(man); kfree(man);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment