Skip to content

Commit 3ede941

Browse files
tklausergopherbot
authored andcommitted
internal/poll: use sync.OnceValue to determine kernel version ≥ 5.3 in CopyFileRange
Change-Id: I13fdf86c3f46bf3c83cb116e9dd3bc4ab1a949d7 Reviewed-on: https://go-review.googlesource.com/c/go/+/573755 Reviewed-by: Ian Lance Taylor <[email protected]> Auto-Submit: Tobias Klauser <[email protected]> Reviewed-by: Emmanuel Odeke <[email protected]> Reviewed-by: David Chase <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: qiulaidongfeng <[email protected]>
1 parent 27fdef6 commit 3ede941

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/internal/poll/copy_file_range_linux.go

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,23 @@ import (
1010
"syscall"
1111
)
1212

13-
var (
14-
kernelVersion53Once sync.Once
15-
kernelVersion53 bool
16-
)
13+
var isKernelVersionGE53 = sync.OnceValue(func() bool {
14+
major, minor := unix.KernelVersion()
15+
// copy_file_range(2) is broken in various ways on kernels older than 5.3,
16+
// see https://go.dev/issue/42400 and
17+
// https://man7.org/linux/man-pages/man2/copy_file_range.2.html#VERSIONS
18+
if major > 5 || (major == 5 && minor >= 3) {
19+
return true
20+
}
21+
return false
22+
})
1723

1824
const maxCopyFileRangeRound = 1 << 30
1925

2026
// CopyFileRange copies at most remain bytes of data from src to dst, using
2127
// the copy_file_range system call. dst and src must refer to regular files.
2228
func CopyFileRange(dst, src *FD, remain int64) (written int64, handled bool, err error) {
23-
kernelVersion53Once.Do(func() {
24-
major, minor := unix.KernelVersion()
25-
// copy_file_range(2) is broken in various ways on kernels older than 5.3,
26-
// see issue #42400 and
27-
// https://man7.org/linux/man-pages/man2/copy_file_range.2.html#VERSIONS
28-
if major > 5 || (major == 5 && minor >= 3) {
29-
kernelVersion53 = true
30-
}
31-
})
32-
33-
if !kernelVersion53 {
29+
if !isKernelVersionGE53() {
3430
return 0, false, nil
3531
}
3632

0 commit comments

Comments
 (0)