Skip to content

Commit d71209d

Browse files
xemuldavem330
authored andcommitted
[INET]: Use list_head-s in inetpeer.c
The inetpeer.c tracks the LRU list of inet_perr-s, but makes it by hands. Use the list_head-s for this. Signed-off-by: Pavel Emelyanov <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 22649d1 commit d71209d

File tree

2 files changed

+16
-28
lines changed

2 files changed

+16
-28
lines changed

include/net/inetpeer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct inet_peer
2222
__be32 v4daddr; /* peer's address */
2323
__u16 avl_height;
2424
__u16 ip_id_count; /* IP ID for the next packet */
25-
struct inet_peer *unused_next, **unused_prevp;
25+
struct list_head unused;
2626
__u32 dtime; /* the time of last use of not
2727
* referenced entries */
2828
atomic_t refcnt;

net/ipv4/inetpeer.c

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
* 4. Global variable peer_total is modified under the pool lock.
6262
* 5. struct inet_peer fields modification:
6363
* avl_left, avl_right, avl_parent, avl_height: pool lock
64-
* unused_next, unused_prevp: unused node list lock
64+
* unused: unused node list lock
6565
* refcnt: atomically against modifications on other CPU;
6666
* usually under some other lock to prevent node disappearing
6767
* dtime: unused node list lock
@@ -94,8 +94,7 @@ int inet_peer_maxttl __read_mostly = 10 * 60 * HZ; /* usual time to live: 10 min
9494
int inet_peer_gc_mintime __read_mostly = 10 * HZ;
9595
int inet_peer_gc_maxtime __read_mostly = 120 * HZ;
9696

97-
static struct inet_peer *inet_peer_unused_head;
98-
static struct inet_peer **inet_peer_unused_tailp = &inet_peer_unused_head;
97+
static LIST_HEAD(unused_peers);
9998
static DEFINE_SPINLOCK(inet_peer_unused_lock);
10099

101100
static void peer_check_expire(unsigned long dummy);
@@ -138,15 +137,7 @@ void __init inet_initpeers(void)
138137
static void unlink_from_unused(struct inet_peer *p)
139138
{
140139
spin_lock_bh(&inet_peer_unused_lock);
141-
if (p->unused_prevp != NULL) {
142-
/* On unused list. */
143-
*p->unused_prevp = p->unused_next;
144-
if (p->unused_next != NULL)
145-
p->unused_next->unused_prevp = p->unused_prevp;
146-
else
147-
inet_peer_unused_tailp = p->unused_prevp;
148-
p->unused_prevp = NULL; /* mark it as removed */
149-
}
140+
list_del_init(&p->unused);
150141
spin_unlock_bh(&inet_peer_unused_lock);
151142
}
152143

@@ -337,24 +328,24 @@ static void unlink_from_pool(struct inet_peer *p)
337328
/* May be called with local BH enabled. */
338329
static int cleanup_once(unsigned long ttl)
339330
{
340-
struct inet_peer *p;
331+
struct inet_peer *p = NULL;
341332

342333
/* Remove the first entry from the list of unused nodes. */
343334
spin_lock_bh(&inet_peer_unused_lock);
344-
p = inet_peer_unused_head;
345-
if (p != NULL) {
346-
__u32 delta = (__u32)jiffies - p->dtime;
335+
if (!list_empty(&unused_peers)) {
336+
__u32 delta;
337+
338+
p = list_first_entry(&unused_peers, struct inet_peer, unused);
339+
delta = (__u32)jiffies - p->dtime;
340+
347341
if (delta < ttl) {
348342
/* Do not prune fresh entries. */
349343
spin_unlock_bh(&inet_peer_unused_lock);
350344
return -1;
351345
}
352-
inet_peer_unused_head = p->unused_next;
353-
if (p->unused_next != NULL)
354-
p->unused_next->unused_prevp = p->unused_prevp;
355-
else
356-
inet_peer_unused_tailp = p->unused_prevp;
357-
p->unused_prevp = NULL; /* mark as not on the list */
346+
347+
list_del_init(&p->unused);
348+
358349
/* Grab an extra reference to prevent node disappearing
359350
* before unlink_from_pool() call. */
360351
atomic_inc(&p->refcnt);
@@ -412,7 +403,7 @@ struct inet_peer *inet_getpeer(__be32 daddr, int create)
412403

413404
/* Link the node. */
414405
link_to_pool(n);
415-
n->unused_prevp = NULL; /* not on the list */
406+
INIT_LIST_HEAD(&n->unused);
416407
peer_total++;
417408
write_unlock_bh(&peer_pool_lock);
418409

@@ -467,10 +458,7 @@ void inet_putpeer(struct inet_peer *p)
467458
{
468459
spin_lock_bh(&inet_peer_unused_lock);
469460
if (atomic_dec_and_test(&p->refcnt)) {
470-
p->unused_prevp = inet_peer_unused_tailp;
471-
p->unused_next = NULL;
472-
*inet_peer_unused_tailp = p;
473-
inet_peer_unused_tailp = &p->unused_next;
461+
list_add_tail(&p->unused, &unused_peers);
474462
p->dtime = (__u32)jiffies;
475463
}
476464
spin_unlock_bh(&inet_peer_unused_lock);

0 commit comments

Comments
 (0)