Skip to content

Commit 731109e

Browse files
xiaosuohorms
authored andcommitted
ipvs: use hlist instead of list
Signed-off-by: Changli Gao <xiaosuo@gmail.com> Signed-off-by: Simon Horman <horms@verge.net.au>
1 parent 41ac51e commit 731109e

File tree

2 files changed

+30
-24
lines changed

2 files changed

+30
-24
lines changed

include/net/ip_vs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ struct ip_vs_conn_param {
494494
* IP_VS structure allocated for each dynamically scheduled connection
495495
*/
496496
struct ip_vs_conn {
497-
struct list_head c_list; /* hashed list heads */
497+
struct hlist_node c_list; /* hashed list heads */
498498
#ifdef CONFIG_NET_NS
499499
struct net *net; /* Name space */
500500
#endif

net/netfilter/ipvs/ip_vs_conn.c

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static int ip_vs_conn_tab_mask __read_mostly;
5959
/*
6060
* Connection hash table: for input and output packets lookups of IPVS
6161
*/
62-
static struct list_head *ip_vs_conn_tab __read_mostly;
62+
static struct hlist_head *ip_vs_conn_tab __read_mostly;
6363

6464
/* SLAB cache for IPVS connections */
6565
static struct kmem_cache *ip_vs_conn_cachep __read_mostly;
@@ -201,7 +201,7 @@ static inline int ip_vs_conn_hash(struct ip_vs_conn *cp)
201201
spin_lock(&cp->lock);
202202

203203
if (!(cp->flags & IP_VS_CONN_F_HASHED)) {
204-
list_add(&cp->c_list, &ip_vs_conn_tab[hash]);
204+
hlist_add_head(&cp->c_list, &ip_vs_conn_tab[hash]);
205205
cp->flags |= IP_VS_CONN_F_HASHED;
206206
atomic_inc(&cp->refcnt);
207207
ret = 1;
@@ -234,7 +234,7 @@ static inline int ip_vs_conn_unhash(struct ip_vs_conn *cp)
234234
spin_lock(&cp->lock);
235235

236236
if (cp->flags & IP_VS_CONN_F_HASHED) {
237-
list_del(&cp->c_list);
237+
hlist_del(&cp->c_list);
238238
cp->flags &= ~IP_VS_CONN_F_HASHED;
239239
atomic_dec(&cp->refcnt);
240240
ret = 1;
@@ -259,12 +259,13 @@ __ip_vs_conn_in_get(const struct ip_vs_conn_param *p)
259259
{
260260
unsigned hash;
261261
struct ip_vs_conn *cp;
262+
struct hlist_node *n;
262263

263264
hash = ip_vs_conn_hashkey_param(p, false);
264265

265266
ct_read_lock(hash);
266267

267-
list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
268+
hlist_for_each_entry(cp, n, &ip_vs_conn_tab[hash], c_list) {
268269
if (cp->af == p->af &&
269270
p->cport == cp->cport && p->vport == cp->vport &&
270271
ip_vs_addr_equal(p->af, p->caddr, &cp->caddr) &&
@@ -345,12 +346,13 @@ struct ip_vs_conn *ip_vs_ct_in_get(const struct ip_vs_conn_param *p)
345346
{
346347
unsigned hash;
347348
struct ip_vs_conn *cp;
349+
struct hlist_node *n;
348350

349351
hash = ip_vs_conn_hashkey_param(p, false);
350352

351353
ct_read_lock(hash);
352354

353-
list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
355+
hlist_for_each_entry(cp, n, &ip_vs_conn_tab[hash], c_list) {
354356
if (!ip_vs_conn_net_eq(cp, p->net))
355357
continue;
356358
if (p->pe_data && p->pe->ct_match) {
@@ -394,6 +396,7 @@ struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)
394396
{
395397
unsigned hash;
396398
struct ip_vs_conn *cp, *ret=NULL;
399+
struct hlist_node *n;
397400

398401
/*
399402
* Check for "full" addressed entries
@@ -402,7 +405,7 @@ struct ip_vs_conn *ip_vs_conn_out_get(const struct ip_vs_conn_param *p)
402405

403406
ct_read_lock(hash);
404407

405-
list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
408+
hlist_for_each_entry(cp, n, &ip_vs_conn_tab[hash], c_list) {
406409
if (cp->af == p->af &&
407410
p->vport == cp->cport && p->cport == cp->dport &&
408411
ip_vs_addr_equal(p->af, p->vaddr, &cp->caddr) &&
@@ -818,7 +821,7 @@ ip_vs_conn_new(const struct ip_vs_conn_param *p,
818821
return NULL;
819822
}
820823

821-
INIT_LIST_HEAD(&cp->c_list);
824+
INIT_HLIST_NODE(&cp->c_list);
822825
setup_timer(&cp->timer, ip_vs_conn_expire, (unsigned long)cp);
823826
ip_vs_conn_net_set(cp, p->net);
824827
cp->af = p->af;
@@ -894,22 +897,23 @@ ip_vs_conn_new(const struct ip_vs_conn_param *p,
894897
*/
895898
#ifdef CONFIG_PROC_FS
896899
struct ip_vs_iter_state {
897-
struct seq_net_private p;
898-
struct list_head *l;
900+
struct seq_net_private p;
901+
struct hlist_head *l;
899902
};
900903

901904
static void *ip_vs_conn_array(struct seq_file *seq, loff_t pos)
902905
{
903906
int idx;
904907
struct ip_vs_conn *cp;
905908
struct ip_vs_iter_state *iter = seq->private;
909+
struct hlist_node *n;
906910

907911
for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
908912
ct_read_lock_bh(idx);
909-
list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
913+
hlist_for_each_entry(cp, n, &ip_vs_conn_tab[idx], c_list) {
910914
if (pos-- == 0) {
911915
iter->l = &ip_vs_conn_tab[idx];
912-
return cp;
916+
return cp;
913917
}
914918
}
915919
ct_read_unlock_bh(idx);
@@ -930,23 +934,24 @@ static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
930934
{
931935
struct ip_vs_conn *cp = v;
932936
struct ip_vs_iter_state *iter = seq->private;
933-
struct list_head *e, *l = iter->l;
937+
struct hlist_node *e;
938+
struct hlist_head *l = iter->l;
934939
int idx;
935940

936941
++*pos;
937942
if (v == SEQ_START_TOKEN)
938943
return ip_vs_conn_array(seq, 0);
939944

940945
/* more on same hash chain? */
941-
if ((e = cp->c_list.next) != l)
942-
return list_entry(e, struct ip_vs_conn, c_list);
946+
if ((e = cp->c_list.next))
947+
return hlist_entry(e, struct ip_vs_conn, c_list);
943948

944949
idx = l - ip_vs_conn_tab;
945950
ct_read_unlock_bh(idx);
946951

947952
while (++idx < ip_vs_conn_tab_size) {
948953
ct_read_lock_bh(idx);
949-
list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
954+
hlist_for_each_entry(cp, e, &ip_vs_conn_tab[idx], c_list) {
950955
iter->l = &ip_vs_conn_tab[idx];
951956
return cp;
952957
}
@@ -959,7 +964,7 @@ static void *ip_vs_conn_seq_next(struct seq_file *seq, void *v, loff_t *pos)
959964
static void ip_vs_conn_seq_stop(struct seq_file *seq, void *v)
960965
{
961966
struct ip_vs_iter_state *iter = seq->private;
962-
struct list_head *l = iter->l;
967+
struct hlist_head *l = iter->l;
963968

964969
if (l)
965970
ct_read_unlock_bh(l - ip_vs_conn_tab);
@@ -1148,13 +1153,14 @@ void ip_vs_random_dropentry(struct net *net)
11481153
*/
11491154
for (idx = 0; idx < (ip_vs_conn_tab_size>>5); idx++) {
11501155
unsigned hash = net_random() & ip_vs_conn_tab_mask;
1156+
struct hlist_node *n;
11511157

11521158
/*
11531159
* Lock is actually needed in this loop.
11541160
*/
11551161
ct_write_lock_bh(hash);
11561162

1157-
list_for_each_entry(cp, &ip_vs_conn_tab[hash], c_list) {
1163+
hlist_for_each_entry(cp, n, &ip_vs_conn_tab[hash], c_list) {
11581164
if (cp->flags & IP_VS_CONN_F_TEMPLATE)
11591165
/* connection template */
11601166
continue;
@@ -1202,12 +1208,14 @@ static void ip_vs_conn_flush(struct net *net)
12021208

12031209
flush_again:
12041210
for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
1211+
struct hlist_node *n;
1212+
12051213
/*
12061214
* Lock is actually needed in this loop.
12071215
*/
12081216
ct_write_lock_bh(idx);
12091217

1210-
list_for_each_entry(cp, &ip_vs_conn_tab[idx], c_list) {
1218+
hlist_for_each_entry(cp, n, &ip_vs_conn_tab[idx], c_list) {
12111219
if (!ip_vs_conn_net_eq(cp, net))
12121220
continue;
12131221
IP_VS_DBG(4, "del connection\n");
@@ -1265,8 +1273,7 @@ int __init ip_vs_conn_init(void)
12651273
/*
12661274
* Allocate the connection hash table and initialize its list heads
12671275
*/
1268-
ip_vs_conn_tab = vmalloc(ip_vs_conn_tab_size *
1269-
sizeof(struct list_head));
1276+
ip_vs_conn_tab = vmalloc(ip_vs_conn_tab_size * sizeof(*ip_vs_conn_tab));
12701277
if (!ip_vs_conn_tab)
12711278
return -ENOMEM;
12721279

@@ -1286,9 +1293,8 @@ int __init ip_vs_conn_init(void)
12861293
IP_VS_DBG(0, "Each connection entry needs %Zd bytes at least\n",
12871294
sizeof(struct ip_vs_conn));
12881295

1289-
for (idx = 0; idx < ip_vs_conn_tab_size; idx++) {
1290-
INIT_LIST_HEAD(&ip_vs_conn_tab[idx]);
1291-
}
1296+
for (idx = 0; idx < ip_vs_conn_tab_size; idx++)
1297+
INIT_HLIST_HEAD(&ip_vs_conn_tab[idx]);
12921298

12931299
for (idx = 0; idx < CT_LOCKARRAY_SIZE; idx++) {
12941300
rwlock_init(&__ip_vs_conntbl_lock_array[idx].l);

0 commit comments

Comments
 (0)