From nobody Wed Dec 17 04:56:36 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