From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6931DC5B559 for ; Fri, 30 May 2025 10:41:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A524B6B00F7; Fri, 30 May 2025 06:41:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9FE446B00FB; Fri, 30 May 2025 06:41:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 78D526B00F9; Fri, 30 May 2025 06:41:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 4F1596B00F8 for ; Fri, 30 May 2025 06:41:00 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id ED03A1A033C for ; Fri, 30 May 2025 10:40:59 +0000 (UTC) X-FDA: 83499231438.28.4D84BE7 Received: from mta20.hihonor.com (mta20.honor.com [81.70.206.69]) by imf23.hostedemail.com (Postfix) with ESMTP id 7C69E140006 for ; Fri, 30 May 2025 10:40:57 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=none; spf=pass (imf23.hostedemail.com: domain of tao.wangtao@honor.com designates 81.70.206.69 as permitted sender) smtp.mailfrom=tao.wangtao@honor.com; dmarc=pass (policy=none) header.from=honor.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1748601658; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=CIdyqe0HriVlofe1Qlz7+PCOzjx5s7VR4yIVeqhl5W8=; b=Q/r/QFUBkgYb529wodN+MiBsUck0EJgXkhEaEffDd7n9w/r2aLc7FpvMs33qqfNRDNgWbY ujTC1Z94VI4JAalkYSWxfqiLDIdyzBohrCJ0ecqkakeYAzOxfxEQwBEca/ez//hek8fa7j 4NedZ/YGmdXL9ndAHHaR5ck+SAjFFzs= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=none; spf=pass (imf23.hostedemail.com: domain of tao.wangtao@honor.com designates 81.70.206.69 as permitted sender) smtp.mailfrom=tao.wangtao@honor.com; dmarc=pass (policy=none) header.from=honor.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1748601658; a=rsa-sha256; cv=none; b=lzy+28uENy9V3vbuoGBmXn6EUocxjqdMEeEwED0X+MxN1uUTwXaB5O7nxHLdwYxLJwGy8v Pr4R1SCuTEC8zF9uFLN/or/M+0o0aR9SBuTPFN9/l8x802tJN3jzjd/Ox5yl4VBmilq4kG glNTThBAjLXOfGF3LZiBTtETB84C5yc= Received: from w001.hihonor.com (unknown [10.68.25.235]) by mta20.hihonor.com (SkyGuard) with ESMTPS id 4b808F6vTHzYkys6; Fri, 30 May 2025 18:38:37 +0800 (CST) Received: from a010.hihonor.com (10.68.16.52) by w001.hihonor.com (10.68.25.235) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Fri, 30 May 2025 18:40:52 +0800 Received: from localhost.localdomain (10.144.18.117) by a010.hihonor.com (10.68.16.52) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.11; Fri, 30 May 2025 18:40:51 +0800 From: wangtao To: , , , , , , , , CC: , , , , , , , , , , , , , , , , wangtao Subject: [PATCH v3 1/4] fs: allow cross-FS copy_file_range for memory-backed files Date: Fri, 30 May 2025 18:39:38 +0800 Message-ID: <20250530103941.11092-2-tao.wangtao@honor.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20250530103941.11092-1-tao.wangtao@honor.com> References: <20250530103941.11092-1-tao.wangtao@honor.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.144.18.117] X-ClientProxiedBy: w011.hihonor.com (10.68.20.122) To a010.hihonor.com (10.68.16.52) X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 7C69E140006 X-Stat-Signature: cydzpawazqu6b53gaajo5j69hrosxdkz X-Rspam-User: X-HE-Tag: 1748601657-220484 X-HE-Meta: U2FsdGVkX1+UIpKcQWV8mR1AFZ//0KHKYRoVw5DXeOSUvZLIse+kLBHPZWGj0/ErAbj5M/o5sML1YeeFa8irCFHqbBj+VmcAFrio8BZYvxmcvN9GJw+Em0REUaxwpFp5SxVoKmZubb/AvclbrOWszXI8WjXTT/F6gXjuhDAQGgt+9hSQccpE4jB5R/HMOdmExvOj0kh4Qp+0Ul3fnIJJohy1qKgtU6HXmYYF4K+8spSNrIG6dsOYOhHvPUSKiwDec23hqYi7JCcvNTV9V2bb+QSh90bTqXzhz/gwfaPGTnJi9g7Q17RESVwlznTL9GxxUl/6g+O8c/NIzf0tj1BSFnwtqQ9ahxFBHkcEIPjcWygm98DDUlYBy1ZE0tAyG3TQRNVOr8jPk7PZpTSm8XsXYPNtUHaXgG+ImGA/rula+BjAUJDpJ5MzyRNoLBb0vZjpC0+RhUG+HCNPTDza0FFsc5YyP8ntzRFFZnpN44OOQlPPtV1sfa74w2PbCCUZPufdR/DmLwCZvlOEPJnoVpVCxjiv1tBp3GeAwOKBtHCBE2/LYC2HbFXr+BW/JfdGu5uUaLsPCCbTYabUjLlfw41kJauDeTY2tlH5/ZCjdnMqfjPgHX/PcTLotS3L8X5Lnl3TPQyN/zZKKMUBYutpvP2YaFddXqGUbofDroK5+myG/AFdy8vEfgvkiFtio/gVmSCe5zydw1OF2wuyt+00tUhv1+G63QZelja0aZmoJQXEhLT30oInRrP749afhKZGukSjH6eKnJMZnIxBxhQTCHKGGy7nCcqUOa0O5SrcrnaLf0DSfo26f1O63xxTyQ4LyVsa5uQHqPr0TVDkUgWK0YqCo3j/1jq86MiCy0k+u0RsZFVp+ahNNOQLyRJeD+2UuI23tDY7X11OH0r/YgV1XvM7L8BoMkiuZ5NhIDwYugmONue9g25Z06eAJ8o1YoeXmGuFDzvu2A3yb9XzkCt1+r3 MtaByTU9 EdG0MPylpAeuMWmIktEnG6q94+2hxgC7ZCYPjL+QlMPYYgnpl9wfSG4i0pDd3TQ33pnJhAs/6RK3NMsVv+vY2m+r35zb0jmvfuwboM/b48MtciZtn/iLNsyozrhkPG1L820qGuZs8bMw26j7/5vAsCxcFcwIoN/e03Xi0rfEU064Yl/xUwMntvRhKBIqSxmpFC34tCUXvmHW4zJB/HCPvT7h9iAiet5Jkssj7 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Memory-backed files can optimize copy performance via copy_file_range callbacks. Compared to mmap&read: reduces GUP (get_user_pages) overhead; vs sendfile/splice: eliminates one memory copy; supports dmabuf zero-copy implementation. Signed-off-by: wangtao --- fs/read_write.c | 71 +++++++++++++++++++++++++++++++++------------- include/linux/fs.h | 2 ++ 2 files changed, 54 insertions(+), 19 deletions(-) diff --git a/fs/read_write.c b/fs/read_write.c index bb0ed26a0b3a..591c6db7b785 100644 --- a/fs/read_write.c +++ b/fs/read_write.c @@ -1469,6 +1469,20 @@ COMPAT_SYSCALL_DEFINE4(sendfile64, int, out_fd, int, in_fd, } #endif +static inline bool is_copy_memory_file_to_file(struct file *file_in, + struct file *file_out) +{ + return (file_in->f_op->fop_flags & FOP_MEMORY_FILE) && + file_in->f_op->copy_file_range && file_out->f_op->write_iter; +} + +static inline bool is_copy_file_to_memory_file(struct file *file_in, + struct file *file_out) +{ + return (file_out->f_op->fop_flags & FOP_MEMORY_FILE) && + file_in->f_op->read_iter && file_out->f_op->copy_file_range; +} + /* * Performs necessary checks before doing a file copy * @@ -1484,11 +1498,23 @@ static int generic_copy_file_checks(struct file *file_in, loff_t pos_in, struct inode *inode_out = file_inode(file_out); uint64_t count = *req_count; loff_t size_in; + bool splice = flags & COPY_FILE_SPLICE; + bool has_memory_file; int ret; - ret = generic_file_rw_checks(file_in, file_out); - if (ret) - return ret; + /* Skip generic checks, allow cross-sb copies for dma-buf/tmpfs */ + has_memory_file = is_copy_memory_file_to_file(file_in, file_out) || + is_copy_file_to_memory_file(file_in, file_out); + if (!splice && has_memory_file) { + if (!(file_in->f_mode & FMODE_READ) || + !(file_out->f_mode & FMODE_WRITE) || + (file_out->f_flags & O_APPEND)) + return -EBADF; + } else { + ret = generic_file_rw_checks(file_in, file_out); + if (ret) + return ret; + } /* * We allow some filesystems to handle cross sb copy, but passing @@ -1500,7 +1526,7 @@ static int generic_copy_file_checks(struct file *file_in, loff_t pos_in, * and several different sets of file_operations, but they all end up * using the same ->copy_file_range() function pointer. */ - if (flags & COPY_FILE_SPLICE) { + if (splice || has_memory_file) { /* cross sb splice is allowed */ } else if (file_out->f_op->copy_file_range) { if (file_in->f_op->copy_file_range != @@ -1581,23 +1607,30 @@ ssize_t vfs_copy_file_range(struct file *file_in, loff_t pos_in, * same sb using clone, but for filesystems where both clone and copy * are supported (e.g. nfs,cifs), we only call the copy method. */ - if (!splice && file_out->f_op->copy_file_range) { - ret = file_out->f_op->copy_file_range(file_in, pos_in, - file_out, pos_out, - len, flags); - } else if (!splice && file_in->f_op->remap_file_range && samesb) { - ret = file_in->f_op->remap_file_range(file_in, pos_in, - file_out, pos_out, - min_t(loff_t, MAX_RW_COUNT, len), - REMAP_FILE_CAN_SHORTEN); - /* fallback to splice */ - if (ret <= 0) + if (!splice) { + if (is_copy_memory_file_to_file(file_in, file_out)) { + ret = file_in->f_op->copy_file_range(file_in, pos_in, + file_out, pos_out, len, flags); + } else if (is_copy_file_to_memory_file(file_in, file_out)) { + ret = file_out->f_op->copy_file_range(file_in, pos_in, + file_out, pos_out, len, flags); + } else if (file_out->f_op->copy_file_range) { + ret = file_out->f_op->copy_file_range(file_in, pos_in, + file_out, pos_out, + len, flags); + } else if (file_in->f_op->remap_file_range && samesb) { + ret = file_in->f_op->remap_file_range(file_in, pos_in, + file_out, pos_out, + min_t(loff_t, MAX_RW_COUNT, len), + REMAP_FILE_CAN_SHORTEN); + /* fallback to splice */ + if (ret <= 0) + splice = true; + } else if (samesb) { + /* Fallback to splice for same sb copy for backward compat */ splice = true; - } else if (samesb) { - /* Fallback to splice for same sb copy for backward compat */ - splice = true; + } } - file_end_write(file_out); if (!splice) diff --git a/include/linux/fs.h b/include/linux/fs.h index 016b0fe1536e..37df1b497418 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -2187,6 +2187,8 @@ struct file_operations { #define FOP_ASYNC_LOCK ((__force fop_flags_t)(1 << 6)) /* File system supports uncached read/write buffered IO */ #define FOP_DONTCACHE ((__force fop_flags_t)(1 << 7)) +/* Supports cross-FS copy_file_range for memory file */ +#define FOP_MEMORY_FILE ((__force fop_flags_t)(1 << 8)) /* Wrap a directory iterator that needs exclusive inode access */ int wrap_directory_iterator(struct file *, struct dir_context *, -- 2.17.1