tcp: use GFP_ATOMIC in tcp_send_active_reset()
Subsystems (4)
PATCH v2 1/1 tcp: use GFP_ATOMIC in tcp_send_active_reset()
tcp_send_active_reset() can be called from contexts where gfp_any() (in tcp_disconnect()) or sk->sk_allocation (in __tcp_close() and mptcp_do_fastclose()) evaluates to GFP_KERNEL, which includes __GFP_FS and __GFP_DIRECT_RECLAIM.
Allocating with GFP_KERNEL while holding the socket lock (sk_lock) creates a lockdep dependency:
sk_lock -> fs_reclaim
This causes false-positive lockdep circular locking warnings with storage subsystems (such as nvme-tcp) that acquire socket locks in block I/O paths and invoke tcp_disconnect() or close sockets upon teardown:
set->srcu -> sk_lock -> fs_reclaim -> elevator_lock -> set->srcu
Active resets are small RST packet headers that should never enter direct reclaim or block while holding socket locks.
Use sk_gfp_mask(sk, GFP_ATOMIC | __GFP_NOWARN) inside tcp_send_active_reset() and remove its priority argument. This preserves __GFP_MEMALLOC access for SOCK_MEMALLOC sockets, suppresses allocation failure warnings, and aligns with other control packet allocations (e.g. tcp_send_fin(), __tcp_send_ack(), tcp_xmit_probe_skb()).
---
v2: addressed actionnable Sashiko's feedback (https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260825023614.1228551-1-edumazet%40google.com)
v1: https://lore.kernel.org/netdev/[email protected]/
include/net/tcp.h | 3 +-- net/ipv4/tcp.c | 14 ++++++-------- net/ipv4/tcp_output.c | 4 ++-- net/ipv4/tcp_timer.c | 6 +++--- net/mptcp/protocol.c | 3 +-- net/mptcp/protocol.h | 2 +- 6 files changed, 14 insertions(+), 18 deletions(-)
include/net/tcp.h +1 -2
@@ -765,8 +765,7 @@ int tcp_fragment(struct sock *sk, enum tcp_queue tcp_queue,void tcp_send_probe0 (struct sock *);int tcp_write_wakeup (struct sock *,int mib);void tcp_send_fin (struct sock *sk); -void tcp_send_active_reset (struct sock *sk,gfp_t priority, -enum sk_rst_reason reason); +void tcp_send_active_reset (struct sock *sk,enum sk_rst_reason reason);int tcp_send_synack (struct sock *);void tcp_push_one (struct sock *,unsigned int mss_now);void __tcp_send_ack (struct sock *sk,u32 rcv_nxt,u16 flags);
net/ipv4/tcp.c +6 -8
@@ -3182,8 +3182,7 @@ void __tcp_close(struct sock *sk, long timeout)/* Unread data was tossed, zap the connection. */ NET_INC_STATS (sock_net (sk),LINUX_MIB_TCPABORTONCLOSE );tcp_set_state (sk,TCP_CLOSE ); -tcp_send_active_reset (sk, sk->sk_allocation, -SK_RST_REASON_TCP_ABORT_ON_CLOSE ); +tcp_send_active_reset (sk,SK_RST_REASON_TCP_ABORT_ON_CLOSE ); }else if (sock_flag (sk,SOCK_LINGER ) && !sk->sk_lingertime) {/* Check zero linger _after_ checking for unread data. */ sk->sk_prot->disconnect (sk,0 );
@@ -3257,7 +3256,7 @@ void __tcp_close(struct sock *sk, long timeout)struct tcp_sock *tp =tcp_sk (sk);if (READ_ONCE (tp->linger2) <0 ) {tcp_set_state (sk,TCP_CLOSE ); -tcp_send_active_reset (sk,GFP_ATOMIC , +tcp_send_active_reset (sk,SK_RST_REASON_TCP_ABORT_ON_LINGER );__NET_INC_STATS (sock_net (sk),LINUX_MIB_TCPABORTONLINGER );
@@ -3276,7 +3275,7 @@ void __tcp_close(struct sock *sk, long timeout)if (sk->sk_state !=TCP_CLOSE ) {if (tcp_check_oom (sk,0 )) {tcp_set_state (sk,TCP_CLOSE ); -tcp_send_active_reset (sk,GFP_ATOMIC , +tcp_send_active_reset (sk,SK_RST_REASON_TCP_ABORT_ON_MEMORY );__NET_INC_STATS (sock_net (sk),LINUX_MIB_TCPABORTONMEMORY );
@@ -3377,14 +3376,14 @@ int tcp_disconnect(struct sock *sk, int flags) }else if (unlikely (tp->repair)) {WRITE_ONCE (sk->sk_err,ECONNABORTED ); }else if (tcp_need_reset (old_state)) { -tcp_send_active_reset (sk,gfp_any (),SK_RST_REASON_TCP_STATE ); +tcp_send_active_reset (sk,SK_RST_REASON_TCP_STATE );WRITE_ONCE (sk->sk_err,ECONNRESET ); }else if (tp->snd_nxt != tp->write_seq && (1 << old_state) & (TCPF_CLOSING |TCPF_LAST_ACK )) {/* The last check adjusts for discrepancy of Linux wrt. RFC * states */ -tcp_send_active_reset (sk,gfp_any (), +tcp_send_active_reset (sk,SK_RST_REASON_TCP_DISCONNECT_WITH_DATA );WRITE_ONCE (sk->sk_err,ECONNRESET ); }else if (old_state ==TCP_SYN_SENT )
@@ -5147,8 +5146,7 @@ int tcp_abort(struct sock *sk, int err)bh_lock_sock (sk);if (tcp_need_reset (sk->sk_state)) -tcp_send_active_reset (sk,GFP_ATOMIC , -SK_RST_REASON_TCP_STATE ); +tcp_send_active_reset (sk,SK_RST_REASON_TCP_STATE );tcp_done_with_error (sk, err);bh_unlock_sock (sk);
net/ipv4/tcp_output.c +2 -2
@@ -3849,9 +3849,9 @@ void tcp_send_fin(struct sock *sk)* was unread data in the receive queue. This behavior is recommended * by RFC 2525, section 2.17. -DaveM */ -void tcp_send_active_reset (struct sock *sk,gfp_t priority, -enum sk_rst_reason reason) +void tcp_send_active_reset (struct sock *sk,enum sk_rst_reason reason) { +gfp_t priority =sk_gfp_mask (sk,GFP_ATOMIC | __GFP_NOWARN);struct sk_buff *skb;TCP_INC_STATS (sock_net (sk),TCP_MIB_OUTRSTS );
net/ipv4/tcp_timer.c +3 -3
@@ -126,7 +126,7 @@ static int tcp_out_of_resources(struct sock *sk, bool do_reset) (!tp->snd_wnd && !tp->packets_out)) do_reset = true;if (do_reset) -tcp_send_active_reset (sk,GFP_ATOMIC , +tcp_send_active_reset (sk,SK_RST_REASON_TCP_ABORT_ON_MEMORY );tcp_done (sk);__NET_INC_STATS (sock_net (sk),LINUX_MIB_TCPABORTONMEMORY );
@@ -809,7 +809,7 @@ static void tcp_keepalive_timer(struct timer_list *t) goto out; } } -tcp_send_active_reset (sk,GFP_ATOMIC ,SK_RST_REASON_TCP_STATE ); +tcp_send_active_reset (sk,SK_RST_REASON_TCP_STATE ); goto death; }
@@ -836,7 +836,7 @@ static void tcp_keepalive_timer(struct timer_list *t) icsk->icsk_probes_out > 0) || (user_timeout == 0 && icsk->icsk_probes_out >= keepalive_probes(tp))) { -tcp_send_active_reset (sk,GFP_ATOMIC , +tcp_send_active_reset (sk,SK_RST_REASON_TCP_KEEPALIVE_TIMEOUT );tcp_write_err (sk); goto out;
net/mptcp/protocol.c +1 -2
@@ -3109,8 +3109,7 @@ static void mptcp_do_fastclose(struct sock *sk)*/ inet_csk (ssk)->icsk_ack.rcv_mss =TCP_MIN_MSS ; -tcp_send_active_reset (ssk, ssk->sk_allocation, -SK_RST_REASON_TCP_ABORT_ON_CLOSE ); +tcp_send_active_reset (ssk,SK_RST_REASON_TCP_ABORT_ON_CLOSE ); unlock:release_sock (ssk); }
net/mptcp/protocol.h +1 -1
@@ -690,7 +690,7 @@ mptcp_send_active_reset_reason(struct sock *sk)enum sk_rst_reason reason; reason =sk_rst_convert_mptcp_reason (subflow->reset_reason); -tcp_send_active_reset (sk,GFP_ATOMIC , reason); +tcp_send_active_reset (sk, reason); }/* Made the fwd mem carried by the given skb available to the msk,
Signature · 1 line
2.55.0.887.g758fc8c411-goog
Hi Eric,
Quoted 14 lines · Eric Dumazet
tcp_send_active_reset()can be called from contexts wheregfp_any()(intcp_disconnect()) orsk->sk_allocation(in__tcp_close()andmptcp_do_fastclose()) evaluates toGFP_KERNEL, which includes__GFP_FSand__GFP_DIRECT_RECLAIM.Allocating with
GFP_KERNELwhile holding the socket lock (sk_lock) creates a lockdep dependency:
sk_lock->fs_reclaimThis causes false-positive lockdep circular locking warnings with storage subsystems (such as nvme-tcp) that acquire socket locks in block I/O paths and invoke
tcp_disconnect()or close sockets upon teardown:
set->srcu->sk_lock->fs_reclaim->elevator_lock->set->srcu
Quoted 8 lines
Active resets are small RST packet headers that should never enter direct reclaim or block while holding socket locks.
Use
sk_gfp_mask(sk, GFP_ATOMIC | __GFP_NOWARN)insidetcp_send_active_reset()and remove its priority argument. This preserves__GFP_MEMALLOCaccess forSOCK_MEMALLOCsockets, suppresses allocation failure warnings, and aligns with other control packet allocations (e.g.tcp_send_fin(),__tcp_send_ack(),tcp_xmit_probe_skb()).
Thank you for the modification! For the MPTCP part:
Cheers,
Matt
Signature · 1 line
Sponsored by the NGI0 Core fund.
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Jakub Kicinski <[email protected]>:
Quoted 10 lines · On Thu, 27 Aug 2026 09:59:36 +0000 you wrote:
tcp_send_active_reset()can be called from contexts wheregfp_any()(intcp_disconnect()) orsk->sk_allocation(in__tcp_close()andmptcp_do_fastclose()) evaluates toGFP_KERNEL, which includes__GFP_FSand__GFP_DIRECT_RECLAIM.Allocating with
GFP_KERNELwhile holding the socket lock (sk_lock) creates a lockdep dependency:
sk_lock->fs_reclaim[...]
Here is the summary with links:
- [v2,net] tcp: use GFP_ATOMIC in tcp_send_active_reset()
https://git.kernel.org/netdev/net/c/18666c73afe9
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html