Skip to content

Commit a47666e

Browse files
alexaringteigland
authored andcommitted
fs: dlm: make connection hash lockless
There are some problems with the connections_lock. During my experiements I saw sometimes circular dependencies with sock_lock. The reason here might be code parts which runs nodeid2con() before or after sock_lock is acquired. Another issue are missing locks in for_conn() iteration. Maybe this works fine because for_conn() is running in a context where connection_hash cannot be manipulated by others anymore. However this patch changes the connection_hash to be protected by sleepable rcu. The hotpath function __find_con() is implemented lockless as it is only a reader of connection_hash and this hopefully fixes the circular locking dependencies. The iteration for_conn() will still call some sleepable functionality, that's why we use sleepable rcu in this case. This patch removes the kmemcache functionality as I think I need to make some free() functionality via call_rcu(). However allocation time isn't here an issue. The dlm_allow_con will not be protected by a lock anymore as I think it's enough to just set and flush workqueues afterwards. Signed-off-by: Alexander Aring <aahringo@redhat.com> Signed-off-by: David Teigland <teigland@redhat.com>
1 parent aa7ab1e commit a47666e

2 files changed

Lines changed: 37 additions & 50 deletions

File tree

‎fs/dlm/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ menuconfig DLM
44
depends on INET
55
depends on SYSFS && CONFIGFS_FS && (IPV6 || IPV6=n)
66
select IP_SCTP
7+
select SRCU
78
help
89
A general purpose distributed lock manager for kernel or userspace
910
applications.

‎fs/dlm/lowcomms.c‎

Lines changed: 36 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ struct connection {
126126
struct work_struct rwork; /* Receive workqueue */
127127
struct work_struct swork; /* Send workqueue */
128128
wait_queue_head_t shutdown_wait; /* wait for graceful shutdown */
129+
struct rcu_head rcu;
129130
};
130131
#define sock2con(x) ((struct connection *)(x)->sk_user_data)
131132

@@ -167,8 +168,8 @@ static struct workqueue_struct *recv_workqueue;
167168
static struct workqueue_struct *send_workqueue;
168169

169170
static struct hlist_head connection_hash[CONN_HASH_SIZE];
170-
static DEFINE_MUTEX(connections_lock);
171-
static struct kmem_cache *con_cache;
171+
static DEFINE_SPINLOCK(connections_lock);
172+
DEFINE_STATIC_SRCU(connections_srcu);
172173

173174
static void process_recv_sockets(struct work_struct *work);
174175
static void process_send_sockets(struct work_struct *work);
@@ -184,23 +185,28 @@ static inline int nodeid_hash(int nodeid)
184185

185186
static struct connection *__find_con(int nodeid)
186187
{
187-
int r;
188+
int r, idx;
188189
struct connection *con;
189190

190191
r = nodeid_hash(nodeid);
191192

192-
hlist_for_each_entry(con, &connection_hash[r], list) {
193-
if (con->nodeid == nodeid)
193+
idx = srcu_read_lock(&connections_srcu);
194+
hlist_for_each_entry_rcu(con, &connection_hash[r], list) {
195+
if (con->nodeid == nodeid) {
196+
srcu_read_unlock(&connections_srcu, idx);
194197
return con;
198+
}
195199
}
200+
srcu_read_unlock(&connections_srcu, idx);
201+
196202
return NULL;
197203
}
198204

199205
/*
200206
* If 'allocation' is zero then we don't attempt to create a new
201207
* connection structure for this node.
202208
*/
203-
static struct connection *__nodeid2con(int nodeid, gfp_t alloc)
209+
static struct connection *nodeid2con(int nodeid, gfp_t alloc)
204210
{
205211
struct connection *con = NULL;
206212
int r;
@@ -209,13 +215,10 @@ static struct connection *__nodeid2con(int nodeid, gfp_t alloc)
209215
if (con || !alloc)
210216
return con;
211217

212-
con = kmem_cache_zalloc(con_cache, alloc);
218+
con = kzalloc(sizeof(*con), alloc);
213219
if (!con)
214220
return NULL;
215221

216-
r = nodeid_hash(nodeid);
217-
hlist_add_head(&con->list, &connection_hash[r]);
218-
219222
con->nodeid = nodeid;
220223
mutex_init(&con->sock_mutex);
221224
INIT_LIST_HEAD(&con->writequeue);
@@ -233,31 +236,27 @@ static struct connection *__nodeid2con(int nodeid, gfp_t alloc)
233236
con->rx_action = zerocon->rx_action;
234237
}
235238

239+
r = nodeid_hash(nodeid);
240+
241+
spin_lock(&connections_lock);
242+
hlist_add_head_rcu(&con->list, &connection_hash[r]);
243+
spin_unlock(&connections_lock);
244+
236245
return con;
237246
}
238247

239248
/* Loop round all connections */
240249
static void foreach_conn(void (*conn_func)(struct connection *c))
241250
{
242-
int i;
243-
struct hlist_node *n;
251+
int i, idx;
244252
struct connection *con;
245253

254+
idx = srcu_read_lock(&connections_srcu);
246255
for (i = 0; i < CONN_HASH_SIZE; i++) {
247-
hlist_for_each_entry_safe(con, n, &connection_hash[i], list)
256+
hlist_for_each_entry_rcu(con, &connection_hash[i], list)
248257
conn_func(con);
249258
}
250-
}
251-
252-
static struct connection *nodeid2con(int nodeid, gfp_t allocation)
253-
{
254-
struct connection *con;
255-
256-
mutex_lock(&connections_lock);
257-
con = __nodeid2con(nodeid, allocation);
258-
mutex_unlock(&connections_lock);
259-
260-
return con;
259+
srcu_read_unlock(&connections_srcu, idx);
261260
}
262261

263262
static struct dlm_node_addr *find_node_addr(int nodeid)
@@ -792,12 +791,9 @@ static int accept_from_sock(struct connection *con)
792791
struct connection *newcon;
793792
struct connection *addcon;
794793

795-
mutex_lock(&connections_lock);
796794
if (!dlm_allow_conn) {
797-
mutex_unlock(&connections_lock);
798795
return -1;
799796
}
800-
mutex_unlock(&connections_lock);
801797

802798
mutex_lock_nested(&con->sock_mutex, 0);
803799

@@ -847,7 +843,7 @@ static int accept_from_sock(struct connection *con)
847843
struct connection *othercon = newcon->othercon;
848844

849845
if (!othercon) {
850-
othercon = kmem_cache_zalloc(con_cache, GFP_NOFS);
846+
othercon = kzalloc(sizeof(*othercon), GFP_NOFS);
851847
if (!othercon) {
852848
log_print("failed to allocate incoming socket");
853849
mutex_unlock(&newcon->sock_mutex);
@@ -1612,16 +1608,17 @@ static void free_conn(struct connection *con)
16121608
{
16131609
close_connection(con, true, true, true);
16141610
if (con->othercon)
1615-
kmem_cache_free(con_cache, con->othercon);
1616-
hlist_del(&con->list);
1617-
kmem_cache_free(con_cache, con);
1611+
kfree_rcu(con->othercon, rcu);
1612+
spin_lock(&connections_lock);
1613+
hlist_del_rcu(&con->list);
1614+
spin_unlock(&connections_lock);
1615+
kfree_rcu(con, rcu);
16181616
}
16191617

16201618
static void work_flush(void)
16211619
{
1622-
int ok;
1620+
int ok, idx;
16231621
int i;
1624-
struct hlist_node *n;
16251622
struct connection *con;
16261623

16271624
do {
@@ -1631,9 +1628,10 @@ static void work_flush(void)
16311628
flush_workqueue(recv_workqueue);
16321629
if (send_workqueue)
16331630
flush_workqueue(send_workqueue);
1631+
idx = srcu_read_lock(&connections_srcu);
16341632
for (i = 0; i < CONN_HASH_SIZE && ok; i++) {
1635-
hlist_for_each_entry_safe(con, n,
1636-
&connection_hash[i], list) {
1633+
hlist_for_each_entry_rcu(con, &connection_hash[i],
1634+
list) {
16371635
ok &= test_bit(CF_READ_PENDING, &con->flags);
16381636
ok &= test_bit(CF_WRITE_PENDING, &con->flags);
16391637
if (con->othercon) {
@@ -1644,6 +1642,7 @@ static void work_flush(void)
16441642
}
16451643
}
16461644
}
1645+
srcu_read_unlock(&connections_srcu, idx);
16471646
} while (!ok);
16481647
}
16491648

@@ -1652,9 +1651,7 @@ void dlm_lowcomms_stop(void)
16521651
/* Set all the flags to prevent any
16531652
socket activity.
16541653
*/
1655-
mutex_lock(&connections_lock);
16561654
dlm_allow_conn = 0;
1657-
mutex_unlock(&connections_lock);
16581655

16591656
if (recv_workqueue)
16601657
flush_workqueue(recv_workqueue);
@@ -1666,8 +1663,6 @@ void dlm_lowcomms_stop(void)
16661663
clean_writequeues();
16671664
foreach_conn(free_conn);
16681665
work_stop();
1669-
1670-
kmem_cache_destroy(con_cache);
16711666
}
16721667

16731668
int dlm_lowcomms_start(void)
@@ -1686,16 +1681,9 @@ int dlm_lowcomms_start(void)
16861681
goto fail;
16871682
}
16881683

1689-
error = -ENOMEM;
1690-
con_cache = kmem_cache_create("dlm_conn", sizeof(struct connection),
1691-
__alignof__(struct connection), 0,
1692-
NULL);
1693-
if (!con_cache)
1694-
goto fail;
1695-
16961684
error = work_start();
16971685
if (error)
1698-
goto fail_destroy;
1686+
goto fail;
16991687

17001688
dlm_allow_conn = 1;
17011689

@@ -1714,10 +1702,8 @@ int dlm_lowcomms_start(void)
17141702
con = nodeid2con(0,0);
17151703
if (con) {
17161704
close_connection(con, false, true, true);
1717-
kmem_cache_free(con_cache, con);
1705+
kfree_rcu(con, rcu);
17181706
}
1719-
fail_destroy:
1720-
kmem_cache_destroy(con_cache);
17211707
fail:
17221708
return error;
17231709
}

0 commit comments

Comments
 (0)