Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp3704686pxb; Tue, 26 Jan 2021 02:33:37 -0800 (PST) X-Google-Smtp-Source: ABdhPJx8MlRbCynCVOTLs2xTUscRZszFn1DPyIk7f+sVWUiy512ibwaLjpc5ESX6veI5sDoqwqs7 X-Received: by 2002:a17:906:97cb:: with SMTP id ef11mr3013430ejb.379.1611657217135; Tue, 26 Jan 2021 02:33:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1611657217; cv=none; d=google.com; s=arc-20160816; b=l+8whSAZTDcuiy3bcu5QVct2MWI2pwFSt60kZU8IEygaHxd68eVTNMyXsUDg/VsFoJ gJ9+0f6QSwBtmzjZ60zTJv0ejf4ZV5MOOxxh4+s3MjZNGZ+hUU/xXwO2Gkk6YRQuq7MN RSWshYitENR0O2Oy5bGplpyhOKcvUZ2ojdBXwZ9J+dOfboPu87IfaHXGlrf4py/esW3U StOiQBGf5EELvwFCrv5XJ03Z+obQjSJAbPhw4KdmlwGMGXLQ6DmKEIHawAYOkGvtU0lY OvhqfzYS0I+J/3Au65yjvNMjS+v/hy6jyUWhaXAMDPuYjrfoixy+WJBdQRqHDDJCEwtO oq+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=clTx3IZZIB7vcYt+Su1+2j4OUUhAaZa8dAoHaFREQp8=; b=xnLwF2P/cB8sq8Oa3pyaBFA8uqbLGhaoLNTYeE3NwxCjvzQfgueI5QMTWasNdHgPQm aKsse6MtbMXheOHrDTjmhns7LEmoiXoyOxVSKkQpuD/qIQT/gIkFKPr2SDsiuyHF9PjU GWW/NHEQaPncqervmVTec4ESESAmtcxOvzgCFGlG2SjoeR6BRYwVwb8ZMd+FNXRW1gT7 mikRnzef61fqyNqjs/tmjSStlsFsPd9Oi6tnvAn6eE+EQkdJuGPg3AchgSGMltnBmHDU HwMzmR4wFxtAGxK/b7DsumUXlml7bi/zNITurt4uubTILQZXmL9NKfzR9IpE4EABmqwf xPQQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a12si7396400edn.568.2021.01.26.02.33.13; Tue, 26 Jan 2021 02:33:37 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2404062AbhAZKb2 (ORCPT + 99 others); Tue, 26 Jan 2021 05:31:28 -0500 Received: from mail109.syd.optusnet.com.au ([211.29.132.80]:58188 "EHLO mail109.syd.optusnet.com.au" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732057AbhAZBfD (ORCPT ); Mon, 25 Jan 2021 20:35:03 -0500 Received: from dread.disaster.area (pa49-180-243-77.pa.nsw.optusnet.com.au [49.180.243.77]) by mail109.syd.optusnet.com.au (Postfix) with ESMTPS id EE17B90CF; Tue, 26 Jan 2021 12:34:14 +1100 (AEDT) Received: from dave by dread.disaster.area with local (Exim 4.92.3) (envelope-from ) id 1l4DFG-002QrL-6o; Tue, 26 Jan 2021 12:34:14 +1100 Date: Tue, 26 Jan 2021 12:34:14 +1100 From: Dave Chinner To: Nicolas Boichat Cc: "Darrick J. Wong" , linux-fsdevel@vger.kernel.org, lkml , Amir Goldstein , Dave Chinner , Luis Lozano , iant@google.com Subject: Re: [BUG] copy_file_range with sysfs file as input Message-ID: <20210126013414.GE4626@dread.disaster.area> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Optus-CM-Score: 0 X-Optus-CM-Analysis: v=2.3 cv=Ubgvt5aN c=1 sm=1 tr=0 cx=a_idp_d a=juxvdbeFDU67v5YkIhU0sw==:117 a=juxvdbeFDU67v5YkIhU0sw==:17 a=kj9zAlcOel0A:10 a=EmqxpYm9HcoA:10 a=7-415B0cAAAA:8 a=Gx1m1vkv1q0fsgJoGIkA:9 a=CjuIK1q_8ugA:10 a=biEYGPWJfzWAr4FL6Ov7:22 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jan 25, 2021 at 03:54:31PM +0800, Nicolas Boichat wrote: > Hi copy_file_range experts, > > We hit this interesting issue when upgrading Go compiler from 1.13 to > 1.15 [1]. Basically we use Go's `io.Copy` to copy the content of > `/sys/kernel/debug/tracing/trace` to a temporary file. > > Under the hood, Go now uses `copy_file_range` syscall to optimize the > copy operation. However, that fails to copy any content when the input > file is from sysfs/tracefs, with an apparent size of 0 (but there is > still content when you `cat` it, of course). > > A repro case is available in comment7 (adapted from the man page), > also copied below [2]. > > Output looks like this (on kernels 5.4.89 (chromeos), 5.7.17 and > 5.10.3 (chromeos)) > $ ./copyfrom /sys/kernel/debug/tracing/trace x > 0 bytes copied That's basically telling you that copy_file_range() was unable to copy anything. The man page says: RETURN VALUE Upon successful completion, copy_file_range() will return the number of bytes copied between files. This could be less than the length originally requested. If the file offset of fd_in is at or past the end of file, no bytes are copied, and copy_file_range() returns zero. THe man page explains it perfectly. Look at the trace file you are trying to copy: $ ls -l /sys/kernel/debug/tracing/trace -rw-r--r-- 1 root root 0 Jan 19 12:17 /sys/kernel/debug/tracing/trace $ cat /sys/kernel/debug/tracing/trace tracer: nop # # entries-in-buffer/entries-written: 0/0 #P:8 # # _-----=> irqs-off # / _----=> need-resched # | / _---=> hardirq/softirq # || / _--=> preempt-depth # ||| / delay # TASK-PID CPU# |||| TIMESTAMP FUNCTION # | | | |||| | | Yup, the sysfs file reports it's size as zero length, so the CFR syscall is saying "there's nothing to copy from this empty file" and so correctly is returning zero without even trying to copy anything because the file offset is at EOF... IOWs, there's no copy_file_range() bug here - it's behaving as documented. 'cat' "works" in this situation because it doesn't check the file size and just attempts to read unconditionally from the file. Hence it happily returns non-existent stale data from busted filesystem implementations that allow data to be read from beyond EOF... Cheers, Dave. -- Dave Chinner david@fromorbit.com