Skip to content

Commit 384cc2f

Browse files
thomashvmwairlied
authored andcommitted
drm: Add a hash-tab rcu-safe API
While hashtab should now be RCU-safe, Add a drm_ht_xxx_api for consumers to use to make it obvious what locking mechanism is used. Document the way the rcu-safe interface should be used. Don't use rcu-safe list traversal in modify operations where we should use a spinlock / mutex anyway. Signed-off-by: Thomas Hellstrom <thellstrom@vmware.com> Signed-off-by: Dave Airlie <airlied@redhat.com>
1 parent 7e8d9da commit 384cc2f

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

‎drivers/gpu/drm/drm_hashtab.c‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void drm_ht_verbose_list(struct drm_open_hash *ht, unsigned long key)
6767
hashed_key = hash_long(key, ht->order);
6868
DRM_DEBUG("Key is 0x%08lx, Hashed key is 0x%08x\n", key, hashed_key);
6969
h_list = &ht->table[hashed_key];
70-
hlist_for_each_entry_rcu(entry, list, h_list, head)
70+
hlist_for_each_entry(entry, list, h_list, head)
7171
DRM_DEBUG("count %d, key: 0x%08lx\n", count++, entry->key);
7272
}
7373

@@ -81,7 +81,7 @@ static struct hlist_node *drm_ht_find_key(struct drm_open_hash *ht,
8181

8282
hashed_key = hash_long(key, ht->order);
8383
h_list = &ht->table[hashed_key];
84-
hlist_for_each_entry_rcu(entry, list, h_list, head) {
84+
hlist_for_each_entry(entry, list, h_list, head) {
8585
if (entry->key == key)
8686
return list;
8787
if (entry->key > key)
@@ -90,6 +90,24 @@ static struct hlist_node *drm_ht_find_key(struct drm_open_hash *ht,
9090
return NULL;
9191
}
9292

93+
static struct hlist_node *drm_ht_find_key_rcu(struct drm_open_hash *ht,
94+
unsigned long key)
95+
{
96+
struct drm_hash_item *entry;
97+
struct hlist_head *h_list;
98+
struct hlist_node *list;
99+
unsigned int hashed_key;
100+
101+
hashed_key = hash_long(key, ht->order);
102+
h_list = &ht->table[hashed_key];
103+
hlist_for_each_entry_rcu(entry, list, h_list, head) {
104+
if (entry->key == key)
105+
return list;
106+
if (entry->key > key)
107+
break;
108+
}
109+
return NULL;
110+
}
93111

94112
int drm_ht_insert_item(struct drm_open_hash *ht, struct drm_hash_item *item)
95113
{
@@ -102,7 +120,7 @@ int drm_ht_insert_item(struct drm_open_hash *ht, struct drm_hash_item *item)
102120
hashed_key = hash_long(key, ht->order);
103121
h_list = &ht->table[hashed_key];
104122
parent = NULL;
105-
hlist_for_each_entry_rcu(entry, list, h_list, head) {
123+
hlist_for_each_entry(entry, list, h_list, head) {
106124
if (entry->key == key)
107125
return -EINVAL;
108126
if (entry->key > key)
@@ -152,7 +170,7 @@ int drm_ht_find_item(struct drm_open_hash *ht, unsigned long key,
152170
{
153171
struct hlist_node *list;
154172

155-
list = drm_ht_find_key(ht, key);
173+
list = drm_ht_find_key_rcu(ht, key);
156174
if (!list)
157175
return -EINVAL;
158176

‎include/drm/drm_hashtab.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,19 @@ extern int drm_ht_remove_key(struct drm_open_hash *ht, unsigned long key);
6161
extern int drm_ht_remove_item(struct drm_open_hash *ht, struct drm_hash_item *item);
6262
extern void drm_ht_remove(struct drm_open_hash *ht);
6363

64+
/*
65+
* RCU-safe interface
66+
*
67+
* The user of this API needs to make sure that two or more instances of the
68+
* hash table manipulation functions are never run simultaneously.
69+
* The lookup function drm_ht_find_item_rcu may, however, run simultaneously
70+
* with any of the manipulation functions as long as it's called from within
71+
* an RCU read-locked section.
72+
*/
73+
#define drm_ht_insert_item_rcu drm_ht_insert_item
74+
#define drm_ht_just_insert_please_rcu drm_ht_just_insert_please
75+
#define drm_ht_remove_key_rcu drm_ht_remove_key
76+
#define drm_ht_remove_item_rcu drm_ht_remove_item
77+
#define drm_ht_find_item_rcu drm_ht_find_item
6478

6579
#endif

0 commit comments

Comments
 (0)