Skip to content

Commit 8e0e7d9

Browse files
committed
BUG/MINOR: guid/server: ensure thread-safety on GUID insert/delete
Since 3.0, it is possible to assign a GUID to proxies, listeners and servers. These objects are stored in a global tree guid_tree. Proxies and listeners are static. However, servers may be added or deleted at runtime, which imply that guid_tree must be protected. Fix this by declaring a read-write lock to protect tree access. For now, only guid_insert() and guid_remove() are protected using a write lock. Outside of these, GUID tree is not accessed at runtime. If server CLI commands are extended to support GUID as server identifier, lookup operation should be extended with a read lock protection. Note that during stat-file preloading, GUID tree is accessed for lookup. However, as it is performed on startup which is single threaded, there is no need for lock here. A BUG_ON() has been added to ensure this precondition remains true. This bug could caused a segfault when using dynamic servers with GUID. However, it was never reproduced for now. This must be backported up to 3.0. To avoid a conflict issue, the previous cleanup patch can be merged before it.
1 parent b70880c commit 8e0e7d9

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

include/haproxy/guid.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#ifndef _HAPROXY_GUID_H
22
#define _HAPROXY_GUID_H
33

4+
#include <haproxy/api-t.h>
45
#include <haproxy/guid-t.h>
6+
#include <haproxy/thread-t.h>
7+
8+
__decl_thread(extern HA_RWLOCK_T guid_lock);
59

610
void guid_init(struct guid_node *node);
711
int guid_insert(enum obj_type *obj_type, const char *uid, char **errmsg);

include/haproxy/thread-t.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ enum lock_label {
205205
OCSP_LOCK,
206206
QC_CID_LOCK,
207207
CACHE_LOCK,
208+
GUID_LOCK,
208209
OTHER_LOCK,
209210
/* WT: make sure never to use these ones outside of development,
210211
* we need them for lock profiling!

src/guid.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
#include <haproxy/proxy.h>
77
#include <haproxy/server-t.h>
88
#include <haproxy/tools.h>
9+
#include <haproxy/thread.h>
910

1011
/* GUID global tree */
1112
struct eb_root guid_tree = EB_ROOT_UNIQUE;
13+
__decl_thread(HA_RWLOCK_T guid_lock);
1214

1315
/* Initialize <guid> members. */
1416
void guid_init(struct guid_node *guid)
@@ -60,13 +62,17 @@ int guid_insert(enum obj_type *objt, const char *uid, char **errmsg)
6062
}
6163

6264
guid->node.key = key;
65+
66+
HA_RWLOCK_WRLOCK(GUID_LOCK, &guid_lock);
6367
node = ebis_insert(&guid_tree, &guid->node);
6468
if (node != &guid->node) {
6569
dup = ebpt_entry(node, struct guid_node, node);
70+
HA_RWLOCK_WRUNLOCK(GUID_LOCK, &guid_lock);
6671
dup_name = guid_name(dup);
6772
memprintf(errmsg, "duplicate entry with %s", dup_name);
6873
goto err;
6974
}
75+
HA_RWLOCK_WRUNLOCK(GUID_LOCK, &guid_lock);
7076

7177
guid->obj_type = objt;
7278
return 0;
@@ -82,8 +88,10 @@ int guid_insert(enum obj_type *objt, const char *uid, char **errmsg)
8288
*/
8389
void guid_remove(struct guid_node *guid)
8490
{
91+
HA_RWLOCK_WRLOCK(GUID_LOCK, &guid_lock);
8592
ebpt_delete(&guid->node);
8693
ha_free(&guid->node.key);
94+
HA_RWLOCK_WRUNLOCK(GUID_LOCK, &guid_lock);
8795
}
8896

8997
/* Retrieve an instance from GUID global tree with key <uid>.
@@ -95,6 +103,12 @@ struct guid_node *guid_lookup(const char *uid)
95103
struct ebpt_node *node = NULL;
96104
struct guid_node *guid = NULL;
97105

106+
/* For now, guid_lookup() is only used during startup in single-thread
107+
* mode. If this is not the case anymore, GUID tree access must be
108+
* protected with the read-write lock.
109+
*/
110+
BUG_ON(!(global.mode & MODE_STARTING));
111+
98112
node = ebis_lookup(&guid_tree, uid);
99113
if (node)
100114
guid = ebpt_entry(node, struct guid_node, node);

src/thread.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ static const char *lock_label(enum lock_label label)
462462
case OCSP_LOCK: return "OCSP";
463463
case QC_CID_LOCK: return "QC_CID";
464464
case CACHE_LOCK: return "CACHE";
465+
case GUID_LOCK: return "GUID";
465466
case OTHER_LOCK: return "OTHER";
466467
case DEBUG1_LOCK: return "DEBUG1";
467468
case DEBUG2_LOCK: return "DEBUG2";

0 commit comments

Comments
 (0)