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 8DC0DC5B549 for ; Fri, 30 May 2025 10:40:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0FD526B00E6; Fri, 30 May 2025 06:40:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0ADF66B00F5; Fri, 30 May 2025 06:40:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EDEDE6B00F6; Fri, 30 May 2025 06:40:47 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id CC1636B00E6 for ; Fri, 30 May 2025 06:40:47 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 41F9716028A for ; Fri, 30 May 2025 10:40:47 +0000 (UTC) X-FDA: 83499230934.04.F6C9826 Received: from mta21.hihonor.com (mta21.hihonor.com [81.70.160.142]) by imf23.hostedemail.com (Postfix) with ESMTP id 9763D140002 for ; Fri, 30 May 2025 10:40:44 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=honor.com; spf=pass (imf23.hostedemail.com: domain of tao.wangtao@honor.com designates 81.70.160.142 as permitted sender) smtp.mailfrom=tao.wangtao@honor.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1748601645; 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: references; bh=Uj3XAyfmwvl8VLtOTmHTNtdBRI8MSKWmUatwCV1ZpM4=; b=Bdzl0hAHe5znMgHtlk6LZmoXHz+Yq7deJOaKrdGA0fB+ljXyZ/ykWhsboFE4+DmCHfgh+K w1ZZNH8SNyqP3PmR3oVSuygxNGBimRJnJJUbx4XBpCDUrO6gUEYqJ/p127Mu/hBSQzW8OY 5eNKo46seyWpo8sofX6XLYLkbfjaGj0= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=honor.com; spf=pass (imf23.hostedemail.com: domain of tao.wangtao@honor.com designates 81.70.160.142 as permitted sender) smtp.mailfrom=tao.wangtao@honor.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1748601645; a=rsa-sha256; cv=none; b=fIPsmtIpp72ZNVTCh8TUZUC/ru//Z8hVjoYx3DIZPn71mS9UdOxXtCzinZHPUOtDynuSNy J/pbrRWYll0YeWNYQSiVW5Go/1cYuAIIrEUAQmaGh7o2i+295Rmrtsybz/tHP59iPs0Ayt DU7Oqpq+KLyhFeV+2NJHr0b5apBbhOE= Received: from w003.hihonor.com (unknown [10.68.17.88]) by mta21.hihonor.com (SkyGuard) with ESMTPS id 4b808J0DcCzYkxr7; Fri, 30 May 2025 18:38:40 +0800 (CST) Received: from a010.hihonor.com (10.68.16.52) by w003.hihonor.com (10.68.17.88) 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:39 +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:39 +0800 From: wangtao To: , , , , , , , , CC: , , , , , , , , , , , , , , , , wangtao Subject: [PATCH v3 0/4] Optimizing disk file & dmabuf copies via copy_file_range. Date: Fri, 30 May 2025 18:39:37 +0800 Message-ID: <20250530103941.11092-1-tao.wangtao@honor.com> X-Mailer: git-send-email 2.17.1 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-Rspam-User: X-Rspamd-Queue-Id: 9763D140002 X-Rspamd-Server: rspam09 X-Stat-Signature: 7fwbj733zxsxyqf9a5e8wxdtyoxmqhr5 X-HE-Tag: 1748601644-724832 X-HE-Meta: U2FsdGVkX1+4oKIKz60ygKyFGF8d4h6M6NOPR0i8M/fbntXdvTnvd2l+r4W4Zb844d9qnXNyCbBbWyK+jd5dQEJutJeTLyNoLugTZ+j9zbbM2gKNDLxgaQdSyInB9ejzYnXm7zWOMLBGqwUOGm1S9zJbDoTdfs2+JH0j0pyBHWVPVtJdcZoS/BfoQNT3QYQreLMmGfE6sqAg384b7LBnqmIHNo+1ffXwoGg/nUI9U0pbQ1OweZrBlTcNWKQ7ldpW9/UAp4v5D4S0OhcCgjnoZFS+6U54i5GmIF8pQvZeCOXO4kMbf/IMdzbZeEV+TnQcbh92ebIqSQLxDyXmiqkzPiIbmAPoOXshZj34ydGOnxZKorWVd3ez6Lqjw5G9xgBMsVKRVjKL2oGYIoy67bZe1Tbzjif8RfI+2u0BUdpHt19sPa7UE90Jc2VMd6DIDYcrV1z1XEA8EtcGQw8rrlG5gQNSWQyWq4+fg3CTU97RcCny0EGoB1bozBLgKlpLzbJMw4iR7Y+izN8hcDRoSLuea4fL/ItxUAxZGiUeeRRuSdkHMcmOowaPigg9iFFf49JYXXAa1gcJyRNqYZaTcdLJRXwS3QJvGMZu02iPNSAD5b6OYW7dw4a/12t+WF88sGX/KZY1ZAQKq3M6blWs7FUv70qOYtgPy+l3F2ntmyM5ZmxexBm6ZAdKu+WAJFfXRsv/80o3m5kLzzFl1VOPi0+bY4RWtnrusxCa2nUkOf/N+EH9JjprVQjwI+kdOBmqvNiiQAsJU1+mwoiOYTBD4z1GBbzXcnyr5znmXqj/wuoXRJ7WZorSbb/Kcp26deYHxEpTb5L1MVCDVVGRxCyz45f4E4H9sqU47XI8aPzCm2x5qCndYsbkB0/zFIlxzJ0ahhfK0qq0pZ2DFIQr+Kz4YltXl5Eyz2mAGqlqGufAwDTbpwbNmYXWTiH/Xv9LTgocpsOvtUlYGi+rvrwu4+hwMMY 3mhC8Fo9 6XVVuNucV0s4OUSo4I4Qmo6mHLIawL+LxdUje5M3L0shHPFPEDUbvKRZ0CR/gfxE3r0M19BkHPhP5pF1fQWuXmINutzkdVSPj2jwlxdn5xDuYlgP7WYJhoQ3ZrNj1PtSfrzxeWuyX4m11/oqjDkibhRtzhA60xmX7F+8KtNuLM75jDKgCYBw6NwkveawNwhVtc1XA5SeViveSffOqKlxQcJLdx5SubtCWgeqh8T1ovLLC3Jx7d2eMzIgljCLYHKdou3yfS+gyV+sh8VnbgfctUiTAj0yNHeWcha8+FElRuvW7MF5oermlfk+K7w== 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: Typical dmabuf file reading steps: 1. dmabuf_fd = dmabuf_alloc(len, heap_fd) 2. vaddr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, dmabuf_fd, 0) 3. file_fd = open(file_path, O_RDONLY) 4. read(file_fd, vaddr, len) dmabuf's attachment/map/fence model uses remap_pfn_range for mmap, which lacks Direct I/O support[1]. Buffer IO causes latency when loading large AI model files. My previous patch added dmabuf ioctl for Direct IO file operations, showing good performance on low-power CPUs[2][3]. Christian suggested using existing uAPIs (read/sendfile/splice/c_f_r) instead of new ioctls. sendfile/splice/c_f_r enable zero-copy via Direct IO for disk-disk/network: sendfile(skt_fd, disk_fd): [DISK]-DMA->[pipe(buf)]-DMA->[NIC] sendfile(dst_disk, src_disk): [DISK] -DMA-> [pipe(buf)] -DMA-> [DISK] Analysis shows existing uAPIs can't achieve zero-copy disk-to-dmabuf. Since dmabuf lacks file ops, using tmpfs for disk-to-tmpfs CPU analysis: | Method | CPU Copies | Key Overhead | |-------------------|------------|----------------------------| | 1. Buffer R+W | 2 | Alloc(cache) & 2 CPU copies| | 2. Direct R+W | 1 | GUP(usr_buf) & 1 CPU copy | | 3. Mmap+Buffer R | 1 | Alloc(cache) & 1 CPU copy | | 4. Mmap+Direct R | 0 | GUP(mem_buf) ~50% CPU | | 5. Buffer Sendfile| 1 | Alloc(cache) & 1 CPU copy | | 6. Direct Sendfile| 1 | Small pipe, high IRQ | | 7. Buffer Splice | 1 | Alloc(cache) & 1 CPU copy | | 8. Direct Splice | 1 | Larger pipe buffer | | 9. c_f_r | N/A | Cross-FS blocked | GUP: get_user_page Alloc(cache): allocate page cache Data flows: 1. [DISK] -DMA-> [Alloc(cache)] -COPY-> [usr_buf] -COPY-> [MEM] 2. [DISK] -DMA-> [GUP(usr_buf)] -COPY-> [MEM] 3. [DISK] -DMA-> [Alloc(cache)] -COPY-> [mem_buf] 4. [DISK] -DMA-> [GUP(mem_buf)] 5. [DISK] -DMA-> [pipe(Alloc(cache))] -COPY-> [tmpfs page] 6. [DISK] -DMA-> [pipe(buf)] -COPY-> [tmpfs page] 7. [DISK] -DMA-> [big_pipe(Alloc(cache))] -COPY-> [tmpfs page] 8. [DISK] -DMA-> [big_pipe(buf)] -COPY-> [tmpfs page] 9. [DISK] -DMA-> [tmpfs page] (blocked) Key findings: - Buffer I/O requires page cache allocation and at least one CPU copy - Read+Write incurs excessive CPU copies and will no longer be analyzed. Future approaches will use Read instead of mmap+Read. - Mmap+Direct has zero copies but 50% GUP overhead, and dmabuf doesn't support - sendfile/splice require intermediate pipes, needing 1 CPU copy - c_f_r limitations: Cross-FS blocks + missing memory FS support Modifications: 1. Enable cross-FS c_f_r for memory file types 2. Add dmabuf c_f_r callbacks for [DISK]-DMA->[dmabuf] 3. Test tmpfs c_f_r locally only (no upstream) due to lock_page deadlock risks Performance (1GHz CPU, UFS4@4GB): 1. tmpfs(memfd) direct c_f_r(1197 MB/s): +15% vs mmap&read(1014) 2. udmabuf+memfd(2318 MB/s): +50% vs mmap&read(1457 MB/s) 3. dmabuf direct c_f_r(3405 MB/s): 260% faster than buffer IO(918 MB/s) 40% faster than udmabuf(2318 MB/s) | 32x32MB Read 1024MB |Creat-ms|Close-ms| I/O-ms|I/O-MB/s| I/O% |-------------------------|--------|--------|--------|--------|----- | 1)Beg dmabuf buffer R/W| 52 | 5 | 1170 | 918 | 100% | 2) udmabuf buffer R/W| 591 | 326 | 1281 | 838 | 91% | 3) memfd buffer R/W| 1 | 323 | 2370 | 453 | 49% | 4) memfd direct R/W| 1 | 321 | 1058 | 1014 | 110% | 5) memfd buffer sendfile| 1 | 329 | 1577 | 681 | 74% | 6) memfd direct sendfile| 1 | 327 | 2672 | 401 | 43% | 7) memfd buffer splice| 2 | 321 | 1729 | 621 | 67% | 8) memfd direct splice| 2 | 324 | 1528 | 702 | 76% | 9) memfd buffer c_f_r| 1 | 325 | 1586 | 677 | 73% |10) memfd direct c_f_r| 1 | 323 | 897 | 1197 | 130% |11) u+mfd buffer R/W| 609 | 344 | 2207 | 486 | 52% |12) u+mfd direct R/W| 580 | 342 | 737 | 1457 | 158% |13) u+mfd buffer sendfile| 582 | 343 | 1270 | 845 | 92% |14) u+mfd direct sendfile| 573 | 344 | 2254 | 476 | 51% |15) u+mfd buffer splice| 584 | 341 | 1202 | 893 | 97% |16) u+mfd direct splice| 564 | 340 | 851 | 1263 | 137% |17) u+mfd buffer c_f_r| 585 | 344 | 1244 | 863 | 94% |18) u+mfd direct c_f_r| 578 | 341 | 581 | 1848 | 201% |19) udmabuf buffer c_f_r| 585 | 328 | 1163 | 923 | 100% |20) udmabuf direct c_f_r| 579 | 328 | 464 | 2318 | 252% |21) dmabuf buffer c_f_r| 48 | 5 | 1058 | 1015 | 110% |22) dmabuf direct c_f_r| 48 | 5 | 316 | 3405 | 370% |23)End dmabuf buffer R/W| 48 | 5 | 1173 | 915 | 99% u+mfd = udma+memfd = udmabuf + pre-allocated memfd combo. Cache cleared during tests to simulate real-world large file loading. dmabuf file Use Cases: - Loading large AI models using dmabuf - Real-time data capture and storage with dmabuf - Persisting task snapshots in Android v1: [2] v1 -> v2: [3] Dma-buf exporter verify exclusive access to the dmabuf's sgtable. v2 -> v3: copy_file_range supports copying from disk files to memory files. Implement the copy_file_range callback functions for dmabuf/udmabuf. Reference: [1] https://lore.kernel.org/all/0393cf47-3fa2-4e32-8b3d-d5d5bdece298@amd.com [2] https://lore.kernel.org/all/20250513092803.2096-1-tao.wangtao@honor.com [3] https://lore.kernel.org/all/20250516092148.12778-1-tao.wangtao@honor.com wangtao (4): fs: allow cross-FS copy_file_range for memory-backed files dmabuf: Implement copy_file_range for dmabuf udmabuf: Implement udmabuf rw_file callback dmabuf:system_heap Implement system_heap exporter's rw_file callback. drivers/dma-buf/dma-buf.c | 32 ++++++++++++ drivers/dma-buf/heaps/system_heap.c | 79 +++++++++++++++++++++++++++++ drivers/dma-buf/udmabuf.c | 59 +++++++++++++++++++++ fs/read_write.c | 71 +++++++++++++++++++------- include/linux/dma-buf.h | 16 ++++++ include/linux/fs.h | 2 + 6 files changed, 240 insertions(+), 19 deletions(-) -- 2.17.1