Skip to content

Commit 3881ac0

Browse files
committed
dlm: improve rsb searches
By pre-allocating rsb structs before searching the hash table, they can be inserted immediately. This avoids always having to repeat the search when adding the struct to hash list. This also adds space to the rsb struct for a max resource name, so an rsb allocation can be used by any request. The constant size also allows us to finally use a slab for the rsb structs. Signed-off-by: David Teigland <teigland@redhat.com>
1 parent 3d6aa67 commit 3881ac0

7 files changed

Lines changed: 121 additions & 48 deletions

File tree

‎fs/dlm/config.c‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ struct dlm_cluster {
102102
unsigned int cl_protocol;
103103
unsigned int cl_timewarn_cs;
104104
unsigned int cl_waitwarn_us;
105+
unsigned int cl_new_rsb_count;
105106
};
106107

107108
enum {
@@ -116,6 +117,7 @@ enum {
116117
CLUSTER_ATTR_PROTOCOL,
117118
CLUSTER_ATTR_TIMEWARN_CS,
118119
CLUSTER_ATTR_WAITWARN_US,
120+
CLUSTER_ATTR_NEW_RSB_COUNT,
119121
};
120122

121123
struct cluster_attribute {
@@ -168,6 +170,7 @@ CLUSTER_ATTR(log_debug, 0);
168170
CLUSTER_ATTR(protocol, 0);
169171
CLUSTER_ATTR(timewarn_cs, 1);
170172
CLUSTER_ATTR(waitwarn_us, 0);
173+
CLUSTER_ATTR(new_rsb_count, 0);
171174

172175
static struct configfs_attribute *cluster_attrs[] = {
173176
[CLUSTER_ATTR_TCP_PORT] = &cluster_attr_tcp_port.attr,
@@ -181,6 +184,7 @@ static struct configfs_attribute *cluster_attrs[] = {
181184
[CLUSTER_ATTR_PROTOCOL] = &cluster_attr_protocol.attr,
182185
[CLUSTER_ATTR_TIMEWARN_CS] = &cluster_attr_timewarn_cs.attr,
183186
[CLUSTER_ATTR_WAITWARN_US] = &cluster_attr_waitwarn_us.attr,
187+
[CLUSTER_ATTR_NEW_RSB_COUNT] = &cluster_attr_new_rsb_count.attr,
184188
NULL,
185189
};
186190

@@ -450,6 +454,7 @@ static struct config_group *make_cluster(struct config_group *g,
450454
cl->cl_protocol = dlm_config.ci_protocol;
451455
cl->cl_timewarn_cs = dlm_config.ci_timewarn_cs;
452456
cl->cl_waitwarn_us = dlm_config.ci_waitwarn_us;
457+
cl->cl_new_rsb_count = dlm_config.ci_new_rsb_count;
453458

454459
space_list = &sps->ss_group;
455460
comm_list = &cms->cs_group;
@@ -1041,6 +1046,7 @@ int dlm_our_addr(struct sockaddr_storage *addr, int num)
10411046
#define DEFAULT_PROTOCOL 0
10421047
#define DEFAULT_TIMEWARN_CS 500 /* 5 sec = 500 centiseconds */
10431048
#define DEFAULT_WAITWARN_US 0
1049+
#define DEFAULT_NEW_RSB_COUNT 128
10441050

10451051
struct dlm_config_info dlm_config = {
10461052
.ci_tcp_port = DEFAULT_TCP_PORT,
@@ -1053,6 +1059,7 @@ struct dlm_config_info dlm_config = {
10531059
.ci_log_debug = DEFAULT_LOG_DEBUG,
10541060
.ci_protocol = DEFAULT_PROTOCOL,
10551061
.ci_timewarn_cs = DEFAULT_TIMEWARN_CS,
1056-
.ci_waitwarn_us = DEFAULT_WAITWARN_US
1062+
.ci_waitwarn_us = DEFAULT_WAITWARN_US,
1063+
.ci_new_rsb_count = DEFAULT_NEW_RSB_COUNT
10571064
};
10581065

‎fs/dlm/config.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct dlm_config_info {
2828
int ci_protocol;
2929
int ci_timewarn_cs;
3030
int ci_waitwarn_us;
31+
int ci_new_rsb_count;
3132
};
3233

3334
extern struct dlm_config_info dlm_config;

‎fs/dlm/dlm_internal.h‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ struct dlm_rsb {
293293
int res_recover_locks_count;
294294

295295
char *res_lvbptr;
296-
char res_name[1];
296+
char res_name[DLM_RESNAME_MAXLEN+1];
297297
};
298298

299299
/* find_rsb() flags */
@@ -477,6 +477,10 @@ struct dlm_ls {
477477
struct mutex ls_timeout_mutex;
478478
struct list_head ls_timeout;
479479

480+
spinlock_t ls_new_rsb_spin;
481+
int ls_new_rsb_count;
482+
struct list_head ls_new_rsb; /* new rsb structs */
483+
480484
struct list_head ls_nodes; /* current nodes in ls */
481485
struct list_head ls_nodes_gone; /* dead node list, recovery */
482486
int ls_num_nodes; /* number of nodes in ls */

‎fs/dlm/lock.c‎

Lines changed: 82 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -327,27 +327,77 @@ static void queue_bast(struct dlm_rsb *r, struct dlm_lkb *lkb, int rqmode)
327327
* Basic operations on rsb's and lkb's
328328
*/
329329

330-
static struct dlm_rsb *create_rsb(struct dlm_ls *ls, char *name, int len)
330+
static int pre_rsb_struct(struct dlm_ls *ls)
331+
{
332+
struct dlm_rsb *r1, *r2;
333+
int count = 0;
334+
335+
spin_lock(&ls->ls_new_rsb_spin);
336+
if (ls->ls_new_rsb_count > dlm_config.ci_new_rsb_count / 2) {
337+
spin_unlock(&ls->ls_new_rsb_spin);
338+
return 0;
339+
}
340+
spin_unlock(&ls->ls_new_rsb_spin);
341+
342+
r1 = dlm_allocate_rsb(ls);
343+
r2 = dlm_allocate_rsb(ls);
344+
345+
spin_lock(&ls->ls_new_rsb_spin);
346+
if (r1) {
347+
list_add(&r1->res_hashchain, &ls->ls_new_rsb);
348+
ls->ls_new_rsb_count++;
349+
}
350+
if (r2) {
351+
list_add(&r2->res_hashchain, &ls->ls_new_rsb);
352+
ls->ls_new_rsb_count++;
353+
}
354+
count = ls->ls_new_rsb_count;
355+
spin_unlock(&ls->ls_new_rsb_spin);
356+
357+
if (!count)
358+
return -ENOMEM;
359+
return 0;
360+
}
361+
362+
/* If ls->ls_new_rsb is empty, return -EAGAIN, so the caller can
363+
unlock any spinlocks, go back and call pre_rsb_struct again.
364+
Otherwise, take an rsb off the list and return it. */
365+
366+
static int get_rsb_struct(struct dlm_ls *ls, char *name, int len,
367+
struct dlm_rsb **r_ret)
331368
{
332369
struct dlm_rsb *r;
370+
int count;
333371

334-
r = dlm_allocate_rsb(ls, len);
335-
if (!r)
336-
return NULL;
372+
spin_lock(&ls->ls_new_rsb_spin);
373+
if (list_empty(&ls->ls_new_rsb)) {
374+
count = ls->ls_new_rsb_count;
375+
spin_unlock(&ls->ls_new_rsb_spin);
376+
log_debug(ls, "find_rsb retry %d %d %s",
377+
count, dlm_config.ci_new_rsb_count, name);
378+
return -EAGAIN;
379+
}
380+
381+
r = list_first_entry(&ls->ls_new_rsb, struct dlm_rsb, res_hashchain);
382+
list_del(&r->res_hashchain);
383+
ls->ls_new_rsb_count--;
384+
spin_unlock(&ls->ls_new_rsb_spin);
337385

338386
r->res_ls = ls;
339387
r->res_length = len;
340388
memcpy(r->res_name, name, len);
341389
mutex_init(&r->res_mutex);
342390

391+
INIT_LIST_HEAD(&r->res_hashchain);
343392
INIT_LIST_HEAD(&r->res_lookup);
344393
INIT_LIST_HEAD(&r->res_grantqueue);
345394
INIT_LIST_HEAD(&r->res_convertqueue);
346395
INIT_LIST_HEAD(&r->res_waitqueue);
347396
INIT_LIST_HEAD(&r->res_root_list);
348397
INIT_LIST_HEAD(&r->res_recover_list);
349398

350-
return r;
399+
*r_ret = r;
400+
return 0;
351401
}
352402

353403
static int search_rsb_list(struct list_head *head, char *name, int len,
@@ -405,16 +455,6 @@ static int _search_rsb(struct dlm_ls *ls, char *name, int len, int b,
405455
return error;
406456
}
407457

408-
static int search_rsb(struct dlm_ls *ls, char *name, int len, int b,
409-
unsigned int flags, struct dlm_rsb **r_ret)
410-
{
411-
int error;
412-
spin_lock(&ls->ls_rsbtbl[b].lock);
413-
error = _search_rsb(ls, name, len, b, flags, r_ret);
414-
spin_unlock(&ls->ls_rsbtbl[b].lock);
415-
return error;
416-
}
417-
418458
/*
419459
* Find rsb in rsbtbl and potentially create/add one
420460
*
@@ -432,35 +472,48 @@ static int search_rsb(struct dlm_ls *ls, char *name, int len, int b,
432472
static int find_rsb(struct dlm_ls *ls, char *name, int namelen,
433473
unsigned int flags, struct dlm_rsb **r_ret)
434474
{
435-
struct dlm_rsb *r = NULL, *tmp;
475+
struct dlm_rsb *r = NULL;
436476
uint32_t hash, bucket;
437-
int error = -EINVAL;
477+
int error;
438478

439-
if (namelen > DLM_RESNAME_MAXLEN)
479+
if (namelen > DLM_RESNAME_MAXLEN) {
480+
error = -EINVAL;
440481
goto out;
482+
}
441483

442484
if (dlm_no_directory(ls))
443485
flags |= R_CREATE;
444486

445-
error = 0;
446487
hash = jhash(name, namelen, 0);
447488
bucket = hash & (ls->ls_rsbtbl_size - 1);
448489

449-
error = search_rsb(ls, name, namelen, bucket, flags, &r);
490+
retry:
491+
if (flags & R_CREATE) {
492+
error = pre_rsb_struct(ls);
493+
if (error < 0)
494+
goto out;
495+
}
496+
497+
spin_lock(&ls->ls_rsbtbl[bucket].lock);
498+
499+
error = _search_rsb(ls, name, namelen, bucket, flags, &r);
450500
if (!error)
451-
goto out;
501+
goto out_unlock;
452502

453503
if (error == -EBADR && !(flags & R_CREATE))
454-
goto out;
504+
goto out_unlock;
455505

456506
/* the rsb was found but wasn't a master copy */
457507
if (error == -ENOTBLK)
458-
goto out;
508+
goto out_unlock;
459509

460-
error = -ENOMEM;
461-
r = create_rsb(ls, name, namelen);
462-
if (!r)
463-
goto out;
510+
error = get_rsb_struct(ls, name, namelen, &r);
511+
if (error == -EAGAIN) {
512+
spin_unlock(&ls->ls_rsbtbl[bucket].lock);
513+
goto retry;
514+
}
515+
if (error)
516+
goto out_unlock;
464517

465518
r->res_hash = hash;
466519
r->res_bucket = bucket;
@@ -474,18 +527,10 @@ static int find_rsb(struct dlm_ls *ls, char *name, int namelen,
474527
nodeid = 0;
475528
r->res_nodeid = nodeid;
476529
}
477-
478-
spin_lock(&ls->ls_rsbtbl[bucket].lock);
479-
error = _search_rsb(ls, name, namelen, bucket, 0, &tmp);
480-
if (!error) {
481-
spin_unlock(&ls->ls_rsbtbl[bucket].lock);
482-
dlm_free_rsb(r);
483-
r = tmp;
484-
goto out;
485-
}
486530
list_add(&r->res_hashchain, &ls->ls_rsbtbl[bucket].list);
487-
spin_unlock(&ls->ls_rsbtbl[bucket].lock);
488531
error = 0;
532+
out_unlock:
533+
spin_unlock(&ls->ls_rsbtbl[bucket].lock);
489534
out:
490535
*r_ret = r;
491536
return error;

‎fs/dlm/lockspace.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,9 @@ static int new_lockspace(const char *name, int namelen, void **lockspace,
493493
INIT_LIST_HEAD(&ls->ls_timeout);
494494
mutex_init(&ls->ls_timeout_mutex);
495495

496+
INIT_LIST_HEAD(&ls->ls_new_rsb);
497+
spin_lock_init(&ls->ls_new_rsb_spin);
498+
496499
INIT_LIST_HEAD(&ls->ls_nodes);
497500
INIT_LIST_HEAD(&ls->ls_nodes_gone);
498501
ls->ls_num_nodes = 0;
@@ -764,6 +767,13 @@ static int release_lockspace(struct dlm_ls *ls, int force)
764767

765768
vfree(ls->ls_rsbtbl);
766769

770+
while (!list_empty(&ls->ls_new_rsb)) {
771+
rsb = list_first_entry(&ls->ls_new_rsb, struct dlm_rsb,
772+
res_hashchain);
773+
list_del(&rsb->res_hashchain);
774+
dlm_free_rsb(rsb);
775+
}
776+
767777
/*
768778
* Free structures on any other lists
769779
*/

‎fs/dlm/memory.c‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "memory.h"
1717

1818
static struct kmem_cache *lkb_cache;
19+
static struct kmem_cache *rsb_cache;
1920

2021

2122
int __init dlm_memory_init(void)
@@ -26,13 +27,23 @@ int __init dlm_memory_init(void)
2627
__alignof__(struct dlm_lkb), 0, NULL);
2728
if (!lkb_cache)
2829
ret = -ENOMEM;
30+
31+
rsb_cache = kmem_cache_create("dlm_rsb", sizeof(struct dlm_rsb),
32+
__alignof__(struct dlm_rsb), 0, NULL);
33+
if (!rsb_cache) {
34+
kmem_cache_destroy(lkb_cache);
35+
ret = -ENOMEM;
36+
}
37+
2938
return ret;
3039
}
3140

3241
void dlm_memory_exit(void)
3342
{
3443
if (lkb_cache)
3544
kmem_cache_destroy(lkb_cache);
45+
if (rsb_cache)
46+
kmem_cache_destroy(rsb_cache);
3647
}
3748

3849
char *dlm_allocate_lvb(struct dlm_ls *ls)
@@ -48,24 +59,19 @@ void dlm_free_lvb(char *p)
4859
kfree(p);
4960
}
5061

51-
/* FIXME: have some minimal space built-in to rsb for the name and
52-
kmalloc a separate name if needed, like dentries are done */
53-
54-
struct dlm_rsb *dlm_allocate_rsb(struct dlm_ls *ls, int namelen)
62+
struct dlm_rsb *dlm_allocate_rsb(struct dlm_ls *ls)
5563
{
5664
struct dlm_rsb *r;
5765

58-
DLM_ASSERT(namelen <= DLM_RESNAME_MAXLEN,);
59-
60-
r = kzalloc(sizeof(*r) + namelen, GFP_NOFS);
66+
r = kmem_cache_zalloc(rsb_cache, GFP_NOFS);
6167
return r;
6268
}
6369

6470
void dlm_free_rsb(struct dlm_rsb *r)
6571
{
6672
if (r->res_lvbptr)
6773
dlm_free_lvb(r->res_lvbptr);
68-
kfree(r);
74+
kmem_cache_free(rsb_cache, r);
6975
}
7076

7177
struct dlm_lkb *dlm_allocate_lkb(struct dlm_ls *ls)

‎fs/dlm/memory.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
int dlm_memory_init(void);
1818
void dlm_memory_exit(void);
19-
struct dlm_rsb *dlm_allocate_rsb(struct dlm_ls *ls, int namelen);
19+
struct dlm_rsb *dlm_allocate_rsb(struct dlm_ls *ls);
2020
void dlm_free_rsb(struct dlm_rsb *r);
2121
struct dlm_lkb *dlm_allocate_lkb(struct dlm_ls *ls);
2222
void dlm_free_lkb(struct dlm_lkb *l);

0 commit comments

Comments
 (0)