From 6fb6ace3089edb868ceaefc2a804a794d6889574 Mon Sep 17 00:00:00 2001
From: Andy Pan <i@andypan.me>
Date: Mon, 19 Aug 2024 14:06:40 +0800
Subject: [PATCH] internal/poll: raise Linux's maxCopyFileRangeRound to
 MAX_RW_COUNT

Change-Id: I23c0c850ba57e7a49b78159d9293d1d25e1d2340
Reviewed-on: https://go-review.googlesource.com/c/go/+/606637
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Damien Neil <dneil@google.com>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
Commit-Queue: Ian Lance Taylor <iant@golang.org>
---
 src/internal/poll/copy_file_range_linux.go | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/internal/poll/copy_file_range_linux.go b/src/internal/poll/copy_file_range_linux.go
index eda799893a5..50b9f1bc08c 100644
--- a/src/internal/poll/copy_file_range_linux.go
+++ b/src/internal/poll/copy_file_range_linux.go
@@ -22,13 +22,19 @@ var isKernelVersionGE53 = sync.OnceValue(func() bool {
 	return major > 5 || (major == 5 && minor >= 3)
 })
 
-const maxCopyFileRangeRound = 1 << 30
+// For best performance, call copy_file_range() with the largest len value
+// possible. Linux sets up a limitation of data transfer for most of its I/O
+// system calls, as MAX_RW_COUNT (INT_MAX & PAGE_MASK). This value equals to
+// the maximum integer value minus a page size that is typically 2^12=4096 bytes.
+// That is to say, it's the maximum integer value with the lowest 12 bits unset,
+// which is 0x7ffff000.
+const maxCopyFileRangeRound = 0x7ffff000
 
 func handleCopyFileRangeErr(err error, copied, written int64) (bool, error) {
 	switch err {
 	case syscall.ENOSYS:
 		// copy_file_range(2) was introduced in Linux 4.5.
-		// Go supports Linux >= 2.6.33, so the system call
+		// Go supports Linux >= 3.2, so the system call
 		// may not be present.
 		//
 		// If we see ENOSYS, we have certainly not transferred
-- 
GitLab