git: 6948f4d5e2b2 - main - sendfile: keep the lowat hack more conservative

From: Gleb Smirnoff <glebius_at_FreeBSD.org>
Date: Fri, 02 Oct 2026 02:54:10 UTC
The branch main has been updated by glebius:

URL: https://cgit.FreeBSD.org/src/commit/?id=6948f4d5e2b29100b986a96175ef9efb1ab91616

commit 6948f4d5e2b29100b986a96175ef9efb1ab91616
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2026-10-02 02:53:40 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2026-10-02 02:53:40 +0000

    sendfile: keep the lowat hack more conservative
    
    First, record the original lowat value and later in the wait/EAGAIN loop
    look at the original value, rather than on the value that we just faked.
    This eliminates some blank syscalls, where socket was reported as writable
    and immediate write instantly fails.  In my testing the ratio of such
    syscalls was really small, under 2%, however in a different scenario this
    negative effect can be more profound.
    
    Second, cap the lowat growth to 1/2 of original socket buffer size, rather
    than to current size.  The problem was there before, but it became more
    profound after 587c6c121504.
    
    Note: we are considering to evaluate if the lowat hack is needed at all.
    
    Reviewed by:            tuexen
    Differential Revision:  https://reviews.freebsd.org/D60106
---
 sys/kern/kern_sendfile.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/sys/kern/kern_sendfile.c b/sys/kern/kern_sendfile.c
index b4ad28b27b36..a14b88b2169e 100644
--- a/sys/kern/kern_sendfile.c
+++ b/sys/kern/kern_sendfile.c
@@ -637,6 +637,7 @@ sendfile_getsock(struct thread *td, int s, struct file **sock_fp,
 int
 sendfile_wait_generic(struct socket *so, off_t need, int *space)
 {
+	u_int lowat, newlowat;
 	int error;
 
 	MPASS(need > 0);
@@ -657,9 +658,11 @@ sendfile_wait_generic(struct socket *so, off_t need, int *space)
 	 */
 	error = 0;
 	SOCK_SENDBUF_LOCK(so);
+	lowat = so->so_snd.sb_lowat;
 	if (so->so_snd.sb_flags & SB_AUTOLOWAT) {
-		if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
-			so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
+		newlowat = min(V_tcp_sendspace, so->so_snd.sb_hiwat) / 2;
+		if (so->so_snd.sb_lowat < newlowat)
+			so->so_snd.sb_lowat = newlowat;
 		if (so->so_snd.sb_lowat < PAGE_SIZE &&
 		    so->so_snd.sb_hiwat >= PAGE_SIZE)
 			so->so_snd.sb_lowat = PAGE_SIZE;
@@ -679,7 +682,7 @@ retry_space:
 	}
 
 	*space = sbspace(&so->so_snd);
-	if (*space < need && (*space <= 0 || *space < so->so_snd.sb_lowat)) {
+	if (*space < need && (*space <= 0 || *space < lowat)) {
 		if (so->so_state & SS_NBIO) {
 			error = EAGAIN;
 			goto done;