From nobody Tue Dec 16 22:34:19 2025 Received: from mta20.hihonor.com (mta20.honor.com [81.70.206.69]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A4B7022688B; Fri, 30 May 2025 10:41:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=81.70.206.69 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748601662; cv=none; b=Bj1iop1GkYziXFlgMCsDamjCznHxw8L5x6y991mo714ObPFZlpeZhK823S+OevC2U1J9JYJqcqz1efhPcyfICpODnhmoH4rkqEylFmK26XbP0etD5j3rJtaErzgsh10UCqH2USqR5mCUrmNna5f0Cc4OML6l9I1CIJY+WmamhSc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748601662; c=relaxed/simple; bh=3FwFsP+bAsxLQZtn2gjT+Rh+iqx6TpoONFUb2IdXGEQ=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=WCFcUTz6tWbl1GUEDnBE57dHM2WMoJQ6hhQaBdSs1VfFXNTqqSfGrDiZvW2HQo63rTVzcZxg/dTUZ9KmqR0lrB8n3sV3J5j8g1+FpuCPZAGZ21vMrmGbBprN9W6cclkL381bMni+Qgh9Gap6bhjFaRftjkDdQ2DNUC+ZIdGxQ8E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=honor.com; spf=pass smtp.mailfrom=honor.com; arc=none smtp.client-ip=81.70.206.69 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=honor.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=honor.com 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: w011.hihonor.com (10.68.20.122) To a010.hihonor.com (10.68.16.52) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 =20 +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 *fi= le_in, loff_t pos_in, struct inode *inode_out =3D file_inode(file_out); uint64_t count =3D *req_count; loff_t size_in; + bool splice =3D flags & COPY_FILE_SPLICE; + bool has_memory_file; int ret; =20 - ret =3D 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 =3D 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 =3D generic_file_rw_checks(file_in, file_out); + if (ret) + return ret; + } =20 /* * 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 !=3D @@ -1581,23 +1607,30 @@ ssize_t vfs_copy_file_range(struct file *file_in, l= off_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 =3D 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 =3D 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 <=3D 0) + if (!splice) { + if (is_copy_memory_file_to_file(file_in, file_out)) { + ret =3D 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 =3D 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 =3D 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 =3D 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 <=3D 0) + splice =3D true; + } else if (samesb) { + /* Fallback to splice for same sb copy for backward compat */ splice =3D true; - } else if (samesb) { - /* Fallback to splice for same sb copy for backward compat */ - splice =3D true; + } } - file_end_write(file_out); =20 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)) =20 /* Wrap a directory iterator that needs exclusive inode access */ int wrap_directory_iterator(struct file *, struct dir_context *, --=20 2.17.1 From nobody Tue Dec 16 22:34:19 2025 Received: from mta20.hihonor.com (mta20.honor.com [81.70.206.69]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A4B0F2248B9; Fri, 30 May 2025 10:41:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=81.70.206.69 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748601662; cv=none; b=FAeXmepk0t9qqU3DiDobXbm5TFk9aghRkQJhQWJ21xP5lM/vxaVF3RqFUKWBkafLpvi2myQiRCHsPAFcxEGBXY2f7Bk0wswo3soQ82RJRgZy6blTq5V5dQ+pKSkMkGU+1nzYKI+4w2eXJ84xRDupk7dJ4SGFrLyLNQ3XtWw/cd8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748601662; c=relaxed/simple; bh=EivE7ISi+0WG5ukbJcn5823I3TbKyL4AilNB3tIDUZY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=iLrt4bIbMPnfgbTBlM1CJzs4yHzH0HsLo9WdvSi1F4VW14zREjUQ5fCwUom/IoRwkwk8n49gUYGnORE1BYfwRxPHvBcXER12ZbLC1Gfh8WgENGjBkJUuDhUsMkeg6YN2UueB9rHHmLRxqr7CKWzYEPXpEnOLeLvRJiKq6aj7GP4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=honor.com; spf=pass smtp.mailfrom=honor.com; arc=none smtp.client-ip=81.70.206.69 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=honor.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=honor.com Received: from w012.hihonor.com (unknown [10.68.27.189]) by mta20.hihonor.com (SkyGuard) with ESMTPS id 4b808G1VlSzYl3c7; Fri, 30 May 2025 18:38:38 +0800 (CST) Received: from a010.hihonor.com (10.68.16.52) by w012.hihonor.com (10.68.27.189) 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:52 +0800 From: wangtao To: , , , , , , , , CC: , , , , , , , , , , , , , , , , wangtao Subject: [PATCH v3 2/4] dmabuf: Implement copy_file_range for dmabuf Date: Fri, 30 May 2025 18:39:39 +0800 Message-ID: <20250530103941.11092-3-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: w011.hihonor.com (10.68.20.122) To a010.hihonor.com (10.68.16.52) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" First determine if dmabuf reads from or writes to the file. Then call exporter's rw_file callback function. Signed-off-by: wangtao --- drivers/dma-buf/dma-buf.c | 32 ++++++++++++++++++++++++++++++++ include/linux/dma-buf.h | 16 ++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/drivers/dma-buf/dma-buf.c b/drivers/dma-buf/dma-buf.c index 5baa83b85515..fc9bf54c921a 100644 --- a/drivers/dma-buf/dma-buf.c +++ b/drivers/dma-buf/dma-buf.c @@ -523,7 +523,38 @@ static void dma_buf_show_fdinfo(struct seq_file *m, st= ruct file *file) spin_unlock(&dmabuf->name_lock); } =20 +static ssize_t dma_buf_rw_file(struct dma_buf *dmabuf, loff_t my_pos, + struct file *file, loff_t pos, size_t count, bool is_write) +{ + if (!dmabuf->ops->rw_file) + return -EINVAL; + + if (my_pos >=3D dmabuf->size) + count =3D 0; + else + count =3D min_t(size_t, count, dmabuf->size - my_pos); + if (!count) + return 0; + + return dmabuf->ops->rw_file(dmabuf, my_pos, file, pos, count, is_write); +} + +static ssize_t dma_buf_copy_file_range(struct file *file_in, loff_t pos_in, + struct file *file_out, loff_t pos_out, + size_t count, unsigned int flags) +{ + if (is_dma_buf_file(file_in) && file_out->f_op->write_iter) + return dma_buf_rw_file(file_in->private_data, pos_in, + file_out, pos_out, count, true); + else if (is_dma_buf_file(file_out) && file_in->f_op->read_iter) + return dma_buf_rw_file(file_out->private_data, pos_out, + file_in, pos_in, count, false); + else + return -EINVAL; +} + static const struct file_operations dma_buf_fops =3D { + .fop_flags =3D FOP_MEMORY_FILE, .release =3D dma_buf_file_release, .mmap =3D dma_buf_mmap_internal, .llseek =3D dma_buf_llseek, @@ -531,6 +562,7 @@ static const struct file_operations dma_buf_fops =3D { .unlocked_ioctl =3D dma_buf_ioctl, .compat_ioctl =3D compat_ptr_ioctl, .show_fdinfo =3D dma_buf_show_fdinfo, + .copy_file_range =3D dma_buf_copy_file_range, }; =20 /* diff --git a/include/linux/dma-buf.h b/include/linux/dma-buf.h index 36216d28d8bd..d3636e985399 100644 --- a/include/linux/dma-buf.h +++ b/include/linux/dma-buf.h @@ -22,6 +22,7 @@ #include #include #include +#include =20 struct device; struct dma_buf; @@ -285,6 +286,21 @@ struct dma_buf_ops { =20 int (*vmap)(struct dma_buf *dmabuf, struct iosys_map *map); void (*vunmap)(struct dma_buf *dmabuf, struct iosys_map *map); + + /** + * @rw_file: + * + * If an Exporter needs to support Direct I/O file operations, it can + * implement this optional callback. The exporter must verify that no + * other objects hold the sg_table, ensure exclusive access to the + * dmabuf's sg_table, and only then proceed with the I/O operation. + * + * Returns: + * + * 0 on success or a negative error code on failure. + */ + ssize_t (*rw_file)(struct dma_buf *dmabuf, loff_t my_pos, + struct file *file, loff_t pos, size_t count, bool is_write); }; =20 /** --=20 2.17.1 From nobody Tue Dec 16 22:34:19 2025 Received: from mta21.hihonor.com (mta21.honor.com [81.70.160.142]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AD27D1DA5F; Fri, 30 May 2025 10:40:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=81.70.160.142 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748601657; cv=none; b=C9HuAJbI+fEgbhTXaVJa+uqyYrFnHiEIkUalcKwQe4jDIUsOO1o6UPZt9I7CtiQt4YPD0NqhfETBh87I+CRmcTKQeS4BGIld6sqxbOAhey1rOGPOLzImiLtSYt8HYspQCkJ8TZbxPr4+q/hBOUsyPsuw4PVGICD09ITIEGdlgo0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748601657; c=relaxed/simple; bh=0CrEa+93oV3lNWDABrVddBk4IQRMdXd+GbD0mQaKIgs=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=RFyMYMhsnpqcJOPEd14YNlW3deBAqXUby2EYHTvyNB/nbVzmJ+fsI3JhmaYqmSmyRt9WwDgmpLJvNsG5buVnTf58i6Vg4RXa+yLT1fh3zlz08AAprQEZFJJIVkatWAYWYDo0EMHPfYfjT/nhCQ/T+G3trHf1Rl2E4U1uDweqj5g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=honor.com; spf=pass smtp.mailfrom=honor.com; arc=none smtp.client-ip=81.70.160.142 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=honor.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=honor.com Received: from w011.hihonor.com (unknown [10.68.20.122]) by mta21.hihonor.com (SkyGuard) with ESMTPS id 4b808Y0D6pzYl7rs; Fri, 30 May 2025 18:38:53 +0800 (CST) Received: from a010.hihonor.com (10.68.16.52) by w011.hihonor.com (10.68.20.122) 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:52 +0800 From: wangtao To: , , , , , , , , CC: , , , , , , , , , , , , , , , , wangtao Subject: [PATCH v3 3/4] udmabuf: Implement udmabuf rw_file callback Date: Fri, 30 May 2025 18:39:40 +0800 Message-ID: <20250530103941.11092-4-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: w011.hihonor.com (10.68.20.122) To a010.hihonor.com (10.68.16.52) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Construct bio_vec from folios, then call the other file's r/w callbacks for IO operations. Test data shows direct I/O copy_file_range improves performance by over 50% vs direct I/O mmap&read (2557 vs 1534). Test data: | 32x32MB Read 1024MB |Creat-ms|Close-ms| I/O-ms|I/O-MB/s| I/O% |-------------------------|--------|--------|--------|--------|----- | 1)Beg udmabuf buffer R/W| 580 | 323 | 1238 | 867 | 100% | 2) dmabuf buffer R/W| 48 | 5 | 1149 | 934 | 107% | 3) udma+memfd buffer R/W| 597 | 340 | 2157 | 497 | 57% | 4) udma+memfd direct R/W| 573 | 340 | 700 | 1534 | 176% | 5) u+mfd buffer sendfile| 577 | 340 | 1204 | 891 | 102% | 6) u+mfd direct sendfile| 567 | 339 | 2272 | 472 | 54% | 7) u+mfd buffer splice| 570 | 337 | 1114 | 964 | 111% | 8) u+mfd direct splice| 564 | 335 | 793 | 1355 | 156% | 9) udmabuf buffer c_f_r| 577 | 323 | 1059 | 1014 | 116% |10) udmabuf direct c_f_r| 582 | 325 | 420 | 2557 | 294% |11)End udmabuf buffer R/W| 586 | 323 | 1188 | 903 | 104% Signed-off-by: wangtao --- drivers/dma-buf/udmabuf.c | 59 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/drivers/dma-buf/udmabuf.c b/drivers/dma-buf/udmabuf.c index e74e36a8ecda..573275a51674 100644 --- a/drivers/dma-buf/udmabuf.c +++ b/drivers/dma-buf/udmabuf.c @@ -284,6 +284,64 @@ static int end_cpu_udmabuf(struct dma_buf *buf, return 0; } =20 +static ssize_t udmabuf_rw_file(struct dma_buf *dmabuf, loff_t my_pos, + struct file *other, loff_t pos, + size_t count, bool is_write) +{ + struct udmabuf *ubuf =3D dmabuf->priv; + loff_t my_end =3D my_pos + count, bv_beg, bv_end =3D 0; + pgoff_t pg_idx =3D my_pos / PAGE_SIZE; + pgoff_t pg_end =3D DIV_ROUND_UP(my_end, PAGE_SIZE); + size_t i, bv_off, bv_len, bv_num, bv_idx =3D 0, bv_total =3D 0; + struct bio_vec *bvec; + struct kiocb kiocb; + struct iov_iter iter; + unsigned int direction =3D is_write ? ITER_SOURCE : ITER_DEST; + ssize_t ret =3D 0, rw_total =3D 0; + struct folio *folio; + + bv_num =3D min_t(size_t, pg_end - pg_idx + 1, 1024); + bvec =3D kvcalloc(bv_num, sizeof(*bvec), GFP_KERNEL); + if (!bvec) + return -ENOMEM; + + init_sync_kiocb(&kiocb, other); + kiocb.ki_pos =3D pos; + + for (i =3D 0; i < ubuf->nr_pinned && my_pos < my_end; i++) { + folio =3D ubuf->pinned_folios[i]; + bv_beg =3D bv_end; + bv_end +=3D folio_size(folio); + if (bv_end <=3D my_pos) + continue; + + bv_len =3D min(bv_end, my_end) - my_pos; + bv_off =3D my_pos - bv_beg; + my_pos +=3D bv_len; + bv_total +=3D bv_len; + bvec_set_page(&bvec[bv_idx], &folio->page, bv_len, bv_off); + if (++bv_idx < bv_num && my_pos < my_end) + continue; + + /* start R/W if bvec is full or count reaches zero. */ + iov_iter_bvec(&iter, direction, bvec, bv_idx, bv_total); + if (is_write) + ret =3D other->f_op->write_iter(&kiocb, &iter); + else + ret =3D other->f_op->read_iter(&kiocb, &iter); + if (ret <=3D 0) + break; + rw_total +=3D ret; + if (ret < bv_total || fatal_signal_pending(current)) + break; + + bv_idx =3D bv_total =3D 0; + } + kvfree(bvec); + + return rw_total > 0 ? rw_total : ret; +} + static const struct dma_buf_ops udmabuf_ops =3D { .cache_sgt_mapping =3D true, .map_dma_buf =3D map_udmabuf, @@ -294,6 +352,7 @@ static const struct dma_buf_ops udmabuf_ops =3D { .vunmap =3D vunmap_udmabuf, .begin_cpu_access =3D begin_cpu_udmabuf, .end_cpu_access =3D end_cpu_udmabuf, + .rw_file =3D udmabuf_rw_file, }; =20 #define SEALS_WANTED (F_SEAL_SHRINK) --=20 2.17.1 From nobody Tue Dec 16 22:34:19 2025 Received: from mta20.hihonor.com (mta20.honor.com [81.70.206.69]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A4A901DA5F; Fri, 30 May 2025 10:41:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=81.70.206.69 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748601662; cv=none; b=izmFJcEuhaz0dti9dFI3ji1InIE6Gy+B+djpp5xfZXEweBGjkF2/qsgF5bG3LlMwyeI2HIgVKd0l027QA95X+/gadQltfAqZGHon3Q2227sljfatRZQc5TSYc36Cvzgo4TnRwa9q2mFhTJd/5hOhaMx3GP972VW5WSH21LnJRUY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1748601662; c=relaxed/simple; bh=RzPnU0Bkpy/sjTHlR6rlb6bBzrtLK8bjYM4mKwg7rIY=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=WBt49K9oK4qty9vbdI6+UUi7tsrE7WZUa+boatyusRBD3groi0zEqi0gvGBFR/ls4ZfvqxLY6rmBlRO13ES+BrS5nsYdAtSd06yx8IAfqgnDEHHuqjs+XfGmISQ4gHDv96UH923OPCBiKWcDEPuXz4aH1DkTa2GSmOSj+REnz48= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=honor.com; spf=pass smtp.mailfrom=honor.com; arc=none smtp.client-ip=81.70.206.69 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=honor.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=honor.com Received: from w013.hihonor.com (unknown [10.68.26.19]) by mta20.hihonor.com (SkyGuard) with ESMTPS id 4b808G4q3zzYl7DQ; Fri, 30 May 2025 18:38:38 +0800 (CST) Received: from a010.hihonor.com (10.68.16.52) by w013.hihonor.com (10.68.26.19) 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:52 +0800 From: wangtao To: , , , , , , , , CC: , , , , , , , , , , , , , , , , wangtao Subject: [PATCH v3 4/4] dmabuf:system_heap Implement system_heap exporter's rw_file callback. Date: Fri, 30 May 2025 18:39:41 +0800 Message-ID: <20250530103941.11092-5-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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: w011.hihonor.com (10.68.20.122) To a010.hihonor.com (10.68.16.52) Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" First verify system_heap exporter has exclusive dmabuf access. Build bio_vec from sgtable, then invoke target file's r/w callbacks for IO. Outperforms buffer IO mmap/read by 250%, beats direct I/O udmabuf copy_file_range by over 30% with initialization time significantly lower than udmabuf. Test data: | 32x32MB Read 1024MB |Creat-ms|Close-ms| I/O-ms|I/O-MB/s| I/O% |-------------------------|--------|--------|--------|--------|----- | 1)Beg dmabuf buffer R/W| 47 | 5 | 1125 | 954 | 100% | 2) udmabuf buffer R/W| 576 | 323 | 1228 | 874 | 91% | 3) udma+memfd buffer R/W| 596 | 340 | 2166 | 495 | 51% | 4) udma+memfd direct R/W| 570 | 338 | 711 | 1510 | 158% | 5) udmabuf buffer c_f_r| 578 | 329 | 1128 | 952 | 99% | 6) udmabuf direct c_f_r| 570 | 324 | 405 | 2651 | 277% | 7) dmabuf buffer c_f_r| 47 | 5 | 1035 | 1037 | 108% | 8) dmabuf direct c_f_r| 51 | 5 | 309 | 3480 | 364% | 9)End dmabuf buffer R/W| 48 | 5 | 1153 | 931 | 97% | 32x32MB Write 1024MB |Creat-ms|Close-ms| I/O-ms|I/O-MB/s| I/O% |-------------------------|--------|--------|--------|--------|----- | 1)Beg dmabuf buffer R/W| 50 | 5 | 1405 | 764 | 100% | 2) udmabuf buffer R/W| 580 | 341 | 1337 | 803 | 105% | 3) udma+memfd buffer R/W| 588 | 331 | 1820 | 590 | 77% | 4) udma+memfd direct R/W| 585 | 333 | 662 | 1622 | 212% | 5) udmabuf buffer c_f_r| 577 | 329 | 1326 | 810 | 106% | 6) udmabuf direct c_f_r| 580 | 330 | 602 | 1784 | 233% | 7) dmabuf buffer c_f_r| 49 | 5 | 1330 | 807 | 105% | 8) dmabuf direct c_f_r| 49 | 5 | 344 | 3127 | 409% | 9)End dmabuf buffer R/W| 50 | 5 | 1442 | 745 | 97% Signed-off-by: wangtao --- drivers/dma-buf/heaps/system_heap.c | 79 +++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/drivers/dma-buf/heaps/system_heap.c b/drivers/dma-buf/heaps/sy= stem_heap.c index 26d5dc89ea16..d3a1956ebad8 100644 --- a/drivers/dma-buf/heaps/system_heap.c +++ b/drivers/dma-buf/heaps/system_heap.c @@ -20,6 +20,9 @@ #include #include #include +#include +#include +#include =20 static struct dma_heap *sys_heap; =20 @@ -281,6 +284,81 @@ static void system_heap_vunmap(struct dma_buf *dmabuf,= struct iosys_map *map) iosys_map_clear(map); } =20 +static ssize_t system_heap_buffer_rw_other(struct system_heap_buffer *buff= er, + loff_t my_pos, struct file *other, loff_t pos, + size_t count, bool is_write) +{ + struct sg_table *sgt =3D &buffer->sg_table; + struct scatterlist *sg; + loff_t my_end =3D my_pos + count, bv_beg, bv_end =3D 0; + pgoff_t pg_idx =3D my_pos / PAGE_SIZE; + pgoff_t pg_end =3D DIV_ROUND_UP(my_end, PAGE_SIZE); + size_t i, bv_off, bv_len, bv_num, bv_idx =3D 0, bv_total =3D 0; + struct bio_vec *bvec; + struct kiocb kiocb; + struct iov_iter iter; + unsigned int direction =3D is_write ? ITER_SOURCE : ITER_DEST; + ssize_t ret =3D 0, rw_total =3D 0; + + bv_num =3D min_t(size_t, pg_end - pg_idx + 1, 1024); + bvec =3D kvcalloc(bv_num, sizeof(*bvec), GFP_KERNEL); + if (!bvec) + return -ENOMEM; + + init_sync_kiocb(&kiocb, other); + kiocb.ki_pos =3D pos; + + for_each_sg(sgt->sgl, sg, sgt->nents, i) { + if (my_pos >=3D my_end) + break; + bv_beg =3D bv_end; + bv_end +=3D sg->length; + if (bv_end <=3D my_pos) + continue; + + bv_len =3D min(bv_end, my_end) - my_pos; + bv_off =3D sg->offset + my_pos - bv_beg; + my_pos +=3D bv_len; + bv_total +=3D bv_len; + bvec_set_page(&bvec[bv_idx], sg_page(sg), bv_len, bv_off); + if (++bv_idx < bv_num && my_pos < my_end) + continue; + + /* start R/W if bvec is full or count reaches zero. */ + iov_iter_bvec(&iter, direction, bvec, bv_idx, bv_total); + if (is_write) + ret =3D other->f_op->write_iter(&kiocb, &iter); + else + ret =3D other->f_op->read_iter(&kiocb, &iter); + if (ret <=3D 0) + break; + rw_total +=3D ret; + if (ret < bv_total || fatal_signal_pending(current)) + break; + + bv_idx =3D bv_total =3D 0; + } + kvfree(bvec); + + return rw_total > 0 ? rw_total : ret; +} + +static ssize_t system_heap_dma_buf_rw_file(struct dma_buf *dmabuf, + loff_t my_pos, struct file *file, loff_t pos, + size_t count, bool is_write) +{ + struct system_heap_buffer *buffer =3D dmabuf->priv; + ssize_t ret =3D -EBUSY; + + mutex_lock(&buffer->lock); + if (list_empty(&buffer->attachments) && !buffer->vmap_cnt) + ret =3D system_heap_buffer_rw_other(buffer, my_pos, + file, pos, count, is_write); + mutex_unlock(&buffer->lock); + + return ret; +} + static void system_heap_dma_buf_release(struct dma_buf *dmabuf) { struct system_heap_buffer *buffer =3D dmabuf->priv; @@ -308,6 +386,7 @@ static const struct dma_buf_ops system_heap_buf_ops =3D= { .mmap =3D system_heap_mmap, .vmap =3D system_heap_vmap, .vunmap =3D system_heap_vunmap, + .rw_file =3D system_heap_dma_buf_rw_file, .release =3D system_heap_dma_buf_release, }; =20 --=20 2.17.1