From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7F4A935898 for ; Tue, 28 Apr 2026 05:58:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777355936; cv=none; b=Nc4NSxnt3xb9e1DkcwBiv/+LEVO5LP6aJAgKde+RSfynvDVliHitJI/oZBN1rINFhDNMgFr38sAo3DEmWMEelR3lZBMdihRF91dXIc9gq50pQJKXpKcJsJHRmchpLrlmrXxRgupzP571vEqErJtz3T8EeiJWIPGKvqjslrDQMo4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777355936; c=relaxed/simple; bh=9moRYPO6IrrwntTn/jZs+00rG1fdJL6QrtpMYRA5m5c=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=LvftZIYph70vGJBqo2z3ddu44LYJV1cWSMyk3N9JC7rBTLZrw+4U2RlY+vd1Z/l0OJ08imwX1uk6JknybeehkF5TDx+uQYepyWl10kObla+L0pDjf9Bu+MUQJVpdYnazQWMGbfM/E2x3QM3D4gdwp192Yt7vjqTA6BrK6TWKSAc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=hqH+WD4B; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--kuniyu.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="hqH+WD4B" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-35e5791871fso13867100a91.3 for ; Mon, 27 Apr 2026 22:58:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777355933; x=1777960733; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=BzUoMAnwF2GHarT+YBQ47BC+wMAAJa5xhvU2fBn99PY=; b=hqH+WD4BXnXIPK1FHgWoCq57JvPkzXhIYw0kKd1OK4VvICC3LqrT77PdpzXbTOrhJb RRtpkWWCoeOHhWo1hw86N2CugAo4aHRxbwmTdMNh/f2h9I/6E1qzmxV0P9K9DYRnqLrS NGmSJm1sMit3z+E3gMnMyA/jWcRsEs/jq8PhuvPLJX53r+RyrVgEW+ISPIOaIhAJCHWp RzJOWTKZywXj/WuWopXHn9t1QZcNvA+4thdMPYy+bfFFPM+VsQrOMq/oQRdL+DjvOVFp Yv2QKGTNG+rZ7yaBIZXHNnhv6FpgRMNUtOHGE+8woAsTXGkl/p3XKYGIYP3hSlLbp4Hi 2hiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777355933; x=1777960733; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=BzUoMAnwF2GHarT+YBQ47BC+wMAAJa5xhvU2fBn99PY=; b=B1fKqz2Z0hZBzqsOA2CzA4di4uzKO/DpSLwuCYRfTf0dnzFLrwR3aKUZZ0/HF7Bys1 pYfbu2gfMJnyE8470xAJ7D0vRQYvCvCa+H+70X0St4xxVafr8px62dY99B//sCq4FtRO 2Q/d+3UA2IK3vuhL35+sAr2I4IPDqIOiZg3dcjugVI6HHNZOcOocW0BVnfaWaIr5yn/t OQRLD4Zgs9531viu8z+7JMlJ0a4NqwYWnR/Ud6XGb5wwalZ8HgnvGnQDLIbVFL5VylFv lEF6RMPHeigxJC6rxobWqmyZMg1BXGvb3uOU0lFXk0pbRn/DzYc583snZqKoJvMyCKZy nLMg== X-Forwarded-Encrypted: i=1; AFNElJ+qYy04jfwAN04QOiQkauGN1QmwTn3fdPqzSCpKpKK6TqMliJwyvrrYaUUY7vxynh2yPa7DG4E=@vger.kernel.org X-Gm-Message-State: AOJu0Yz61sb16cGiPnrUQL/oBM82ZMeSnaqnLeM5sIbPiBlvs7Zdf+z/ jiDYxAs6YltV3xzYWo7XzFcNt5wWDogunhIOwV20ZpeqrWPYJKmhtrCY33eeTYult7J6L8qT0WZ kcNdHvg== X-Received: from pfbay28.prod.google.com ([2002:a05:6a00:301c:b0:82f:a0ba:50d5]) (user=kuniyu job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:e210:b0:39b:bee4:2952 with SMTP id adf61e73a8af0-3a39c2ccbe9mr2070487637.45.1777355932768; Mon, 27 Apr 2026 22:58:52 -0700 (PDT) Date: Tue, 28 Apr 2026 05:58:41 +0000 In-Reply-To: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.54.0.545.g6539524ca2-goog Message-ID: <20260428055851.2204248-1-kuniyu@google.com> Subject: Re: [PATCH] net: datagram: Drain queue before reporting EOF or ENOTCONN From: Kuniyuki Iwashima To: oss@malat.biz Cc: davem@davemloft.net, ebiggers@google.com, kuba@kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org Content-Type: text/plain; charset="UTF-8" From: Petr Malat Date: Mon, 27 Apr 2026 22:23:33 -0700 > If a packet is queued and RCV_SHUTDOWN flag is set after the function > __skb_wait_for_more_packets() checked the queue, the function returns > EOF, which is then propagated by __unix_dgram_recvmsg() and the user > reads EOF although there is a message or messages still pending. > > The function should check if the queue is empty before returning EOF. > As the same is true for disconnect and it's also reasonable for a pending > signal, check in a common place before returning from the function. > > Signed-off-by: Petr Malat > --- > net/core/datagram.c | 38 +++++++++++++++++++++----------------- > 1 file changed, 21 insertions(+), 17 deletions(-) > > diff --git a/net/core/datagram.c b/net/core/datagram.c > index c285c6465923..5952950f7233 100644 > --- a/net/core/datagram.c > +++ b/net/core/datagram.c > @@ -98,40 +98,44 @@ int __skb_wait_for_more_packets(struct sock *sk, > struct sk_buff_head *queue, > /* Socket errors? */ > error = sock_error(sk); > if (error) > - goto out_err; > + goto out; > > if (READ_ONCE(queue->prev) != skb) > goto out; > > /* Socket shut down? */ > - if (sk->sk_shutdown & RCV_SHUTDOWN) > - goto out_noerr; > + if (sk->sk_shutdown & RCV_SHUTDOWN) { > + error = 1; > + goto check_queue; We already have checked the same condition just above, and this is a matter of timing. Even after the duplicated check is evaluated to false, there is a small chance that the concurrent sendmsg() enqueues a new skb. Considering __skb_wait_for_more_packets() is called only when the queue is empty, it's not worth another round when shutdown()ed. > + } > > /* Sequenced packets can come disconnected. > * If so we report the problem > */ > - error = -ENOTCONN; > if (connection_based(sk) && > - !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN)) > - goto out_err; > + !(sk->sk_state == TCP_ESTABLISHED || sk->sk_state == TCP_LISTEN)) { > + error = -ENOTCONN; Also, the queue is always empty if SOCK_SEQPACKET sk is at TCP_CLOSE. > + goto check_queue; > + } > > /* handle signals */ > - if (signal_pending(current)) > - goto interrupted; > + if (signal_pending(current)) { > + error = sock_intr_errno(*timeo_p); > + goto check_queue; and we don't want to delay signal if it arrived first. > + } > > - error = 0; > *timeo_p = schedule_timeout(*timeo_p); > out: > + *err = error < 0 ? error : 0; > finish_wait(sk_sleep(sk), &wait); > return error; > -interrupted: > - error = sock_intr_errno(*timeo_p); > -out_err: > - *err = error; > - goto out; > -out_noerr: > - *err = 0; > - error = 1; > +check_queue: > + /* A packet may have arrived between the initial queue check and any > + * of the early-exit conditions above. Return 0 to let the caller > + * drain the queue before acting on the shutdown / disconnect / signal. > + */ > + if (READ_ONCE(queue->prev) != skb) > + error = 0; > goto out; > } > EXPORT_SYMBOL(__skb_wait_for_more_packets); > -- > 2.47.3 >