inet: Always increment refcount in inet_twsk_schedule
Subsystems (3)
PATCH 1/1 inet: Always increment refcount in inet_twsk_schedule
I am seeing an issue with the reference count of time wait sockets which leads to freeing of active timer object. This occurs in some data stress test setups, so I am unable to determine the exact step when it occured. However, I logged the refcount and was able to find out the code path which leads to this problem.
//Initialize time wait socket and setup timerinet_twsk_alloc() tw_refcnt = 0__inet_twsk_hashdance() tw_refcnt = 3inet_twsk_schedule() tw_refcnt = 4inet_twsk_put() tw_refcnt = 3
//Receive packet 1 in timewait state tcp_timewait_state_process() -> inet_twsk_schedule tw_refcnt = 3 (no change)
TCP: tcp_v4_timewait_ack() -> inet_twsk_put() tw_refcnt = 2
//Receive packet 2 in timewait state tcp_timewait_state_process() -> inet_twsk_schedule tw_refcnt = 2 (no change)
TCP: tcp_v4_timewait_ack() -> inet_twsk_put() tw_refcnt = 1
//Receive packet 3 in timewait state tcp_timewait_state_process() -> inet_twsk_schedule tw_refcnt = 1 (no change)
TCP: tcp_v4_timewait_ack() -> inet_twsk_put() tw_refcnt = 0
After this step, the time wait socket is destroyed along with the active timer object. This leads to a warning being printed which eventually leads to a crash.
ODEBUG: free active (active state 0) object type: timer_list hint:tw_timer_handler+0x0/0x68
It appears that inet_twsk_schedule needs to increment the reference count unconditionally, otherwise the socket will be destroyed since reference count will be decremented each time an ack is sent out as a response for an incoming packet.
--- net/ipv4/inet_timewait_sock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/inet_timewait_sock.c +0 -0
@@ -246,9 +246,9 @@ void inet_twsk_schedule(struct inet_timewait_sock *tw,
const int timeo)
tw->tw_kill = timeo <= 4*HZ;
if (!mod_timer_pinned(&tw->tw_timer, jiffies + timeo)) {
- atomic_inc(&tw->tw_refcnt);
atomic_inc(&tw->tw_dr->tw_count);
}
+ atomic_inc(&tw->tw_refcnt);
}
EXPORT_SYMBOL_GPL(inet_twsk_schedule);
--
Employee of Qualcomm Innovation Center, Inc.
Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project
Quoted 6 lines · On Sun, 2015-07-19 at 03:31 +0000, [email protected] wrote:
I am seeing an issue with the reference count of time wait sockets which leads to freeing of active timer object. This occurs in some data stress test setups, so I am unable to determine the exact step when it occured. However, I logged the refcount and was able to find out the code path which leads to this problem.
Quoted 8 lines
//Initialize time wait socket and setup timer
inet_twsk_alloc()tw_refcnt= 0__inet_twsk_hashdance()tw_refcnt= 3inet_twsk_schedule()tw_refcnt= 4inet_twsk_put()tw_refcnt= 3//Receive packet 1 in timewait state
tcp_timewait_state_process()->inet_twsk_scheduletw_refcnt= 3 (no change)
This is obviously wrong.
If a timewait socket is found, do we increment its refcnt before proceeding.
Quoted 32 lines
TCP:
tcp_v4_timewait_ack()->inet_twsk_put()tw_refcnt= 2//Receive packet 2 in timewait state
tcp_timewait_state_process()->inet_twsk_scheduletw_refcnt= 2 (no change)
TCP:tcp_v4_timewait_ack()->inet_twsk_put()tw_refcnt= 1//Receive packet 3 in timewait state
tcp_timewait_state_process()->inet_twsk_scheduletw_refcnt= 1 (no change)
TCP:tcp_v4_timewait_ack()->inet_twsk_put()tw_refcnt= 0After this step, the time wait socket is destroyed along with the active timer object. This leads to a warning being printed which eventually leads to a crash.
ODEBUG: free active (active state 0) object type:
timer_listhint:tw_timer_handler+0x0/0x68It appears that
inet_twsk_scheduleneeds to increment the reference count unconditionally, otherwise the socket will be destroyed since reference count will be decremented each time an ack is sent out as a response for an incoming packet.Signed-off-by Subash Abhinov Kasiviswanathan <[email protected]>--- net/ipv4/inet_timewait_sock.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/ipv4/inet_timewait_sock.c +0
-0
net/ipv4/inet_timewait_sock.c void inet_twsk_schedule(struct inet_timewait_sock *tw, · line 254
Quoted 10 lines
net/ipv4/inet_timewait_sock.c +1
-1@@ -246,9 +246,9 @@ void inet_twsk_schedule(struct inet_timewait_sock *tw, const int timeo) tw->tw_kill = timeo <= 4*HZ;if (!mod_timer_pinned (&tw->tw_timer, jiffies + timeo)) { -atomic_inc (&tw->tw_refcnt);atomic_inc (&tw->tw_dr->tw_count); } +atomic_inc (&tw->tw_refcnt); }EXPORT_SYMBOL_GPL (inet_twsk_schedule);
This is wrong. You simply add a memory leak here. It might solve your crash, but is not the proper way.
I've received some private mails about tw issues, that turned to be caused by buggy drivers or buggy arch specific code.
Are you crashed observed on x86 ?
Quoted 6 lines
Quoted 6 lines
//Initialize time wait socket and setup timer
inet_twsk_alloc()tw_refcnt= 0__inet_twsk_hashdance()tw_refcnt= 3inet_twsk_schedule()tw_refcnt= 4inet_twsk_put()tw_refcnt= 3
Quoted 8 lines
Quoted 3 lines
//Receive packet 1 in timewait state
tcp_timewait_state_process()->inet_twsk_scheduletw_refcnt= 3 (no change)This is obviously wrong.
If a timewait socket is found, do we increment its refcnt before proceeding.
We do not increment refcount currently when we find a timewait socket.
Quoted 5 lines
I've received some private mails about tw issues, that turned to be caused by buggy drivers or buggy arch specific code.
Are you crashed observed on x86 ?
This is observed on ARM devices. In the current debug, all time wait socket refcount changes were happening in TCP stack only and there was no platform / driver code involved.
According to my understanding, we would need to increment the time wait socket refcount first before proceeding with any subsequent operations. However, I request your expert opinion on this.
Quoted 10 lines · On Mon, 2015-07-20 at 19:14 +0000, [email protected] wrote:
Quoted 10 lines
Quoted 9 lines
//Initialize time wait socket and setup timer
inet_twsk_alloc()tw_refcnt= 0__inet_twsk_hashdance()tw_refcnt= 3inet_twsk_schedule()tw_refcnt= 4inet_twsk_put()tw_refcnt= 3//Receive packet 1 in timewait state
tcp_timewait_state_process()->inet_twsk_scheduletw_refcnt= 3 (no change)
Quoted 5 lines
Quoted 4 lines
This is obviously wrong.
If a timewait socket is found, do we increment its refcnt before proceeding.
We do not increment refcount currently when we find a timewait socket.
Actually we do increment refcnt, for every socket found in ehash.
Carefully read again __inet_lookup_established()
This code is generic for ESTABLISH and TIME-WAIT sockets
If you found a code that performed the lookup without taking the refcnt, please point me at it, this would be a serious bug.
Quoted 6 lines
Quoted 6 lines
I've received some private mails about tw issues, that turned to be caused by buggy drivers or buggy arch specific code.
Are you crashed observed on x86 ?
Quoted 7 lines
This is observed on ARM devices. In the current debug, all time wait socket refcount changes were happening in TCP stack only and there was no platform / driver code involved.
According to my understanding, we would need to increment the time wait socket refcount first before proceeding with any subsequent operations. However, I request your expert opinion on this.
Is it some Android kernel ?
Android had private modules that needed an update in 3.18
Quoted 10 lines
Actually we do increment refcnt, for every socket found in ehash.
Carefully read again
__inet_lookup_established()This code is generic for ESTABLISH and TIME-WAIT sockets
If you found a code that performed the lookup without taking the refcnt, please point me at it, this would be a serious bug.
From my previous observations, it appears as if
1. this check is bypassed
2. the refcount is incremented here but is decremented before it reaches the packet processing in tcp_timewait_state_process()
I will try to debug this and update.
Quoted 3 lines
Is it some Android kernel ?
Android had private modules that needed an update in 3.18
Yes, the kernel is based on Android 3.18.