]> Cypherpunks repositories - gostls13.git/commitdiff
internal/poll: use sync.OnceValue to determine kernel version ≥ 5.3 in CopyFileRange
authorTobias Klauser <tklauser@distanz.ch>
Fri, 22 Mar 2024 12:39:13 +0000 (13:39 +0100)
committerGopher Robot <gobot@golang.org>
Mon, 25 Mar 2024 09:22:25 +0000 (09:22 +0000)
Change-Id: I13fdf86c3f46bf3c83cb116e9dd3bc4ab1a949d7
Reviewed-on: https://go-review.googlesource.com/c/go/+/573755
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Emmanuel Odeke <emmanuel@orijtech.com>
Reviewed-by: David Chase <drchase@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: qiulaidongfeng <2645477756@qq.com>
src/internal/poll/copy_file_range_linux.go

index ba33f5145d4896cb34bfe1a14d34fba35e727669..1a32236b123350e329ea849ad47012e4ac1aeb6d 100644 (file)
@@ -10,27 +10,23 @@ import (
        "syscall"
 )
 
-var (
-       kernelVersion53Once sync.Once
-       kernelVersion53     bool
-)
+var isKernelVersionGE53 = sync.OnceValue(func() bool {
+       major, minor := unix.KernelVersion()
+       // copy_file_range(2) is broken in various ways on kernels older than 5.3,
+       // see https://go.dev/issue/42400 and
+       // https://man7.org/linux/man-pages/man2/copy_file_range.2.html#VERSIONS
+       if major > 5 || (major == 5 && minor >= 3) {
+               return true
+       }
+       return false
+})
 
 const maxCopyFileRangeRound = 1 << 30
 
 // CopyFileRange copies at most remain bytes of data from src to dst, using
 // the copy_file_range system call. dst and src must refer to regular files.
 func CopyFileRange(dst, src *FD, remain int64) (written int64, handled bool, err error) {
-       kernelVersion53Once.Do(func() {
-               major, minor := unix.KernelVersion()
-               // copy_file_range(2) is broken in various ways on kernels older than 5.3,
-               // see issue #42400 and
-               // https://man7.org/linux/man-pages/man2/copy_file_range.2.html#VERSIONS
-               if major > 5 || (major == 5 && minor >= 3) {
-                       kernelVersion53 = true
-               }
-       })
-
-       if !kernelVersion53 {
+       if !isKernelVersionGE53() {
                return 0, false, nil
        }