From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=91.218.175.135 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795397314; b=WsKlaEa3rUreBEcHyMO46FswBRQ/BwdNqYFz/RXRgNZP6JucayjHWVTLjf+BPMgGJ3rr AiIdF2B8JTnlsSYmIUcxVO4dPiz6HWS7/KqY0/nob/ySXPSO6BQ5diEiJOPvZsUCqhvsg 34TUGf7+/DdYKw7KOKvo5X467neh+82I/bngQbT0GOCoLBYrC5EvgL7S/lMOqoX5udw0A NK7V1q7L/ophrsPEn+LBv+3BX95Bcdq4KD7GXavAK9R3Zciz7pFKHLV0Xgtd54wJEjzL0 GToyCB9r2e66Wy0NJnAYU3lL6bLCElLAlh23BKZlCT5/9J89bL2Oh2y4fqUQ5vmQPjA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795397314; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:Message-ID:Date:MIME-Version:User-Agent: Subject:To:Cc:References:From:In-Reply-To:Content-Type: Content-Transfer-Encoding; bh=3nVlInPmwKkJl4MGaIbIDMUH25e+EWVO33tmlKLpUuQ=; b=mSD5Scg2UtnItJuTjkI93zzCT3cfK/a9KE6/QonkarQD7kqhShFN/K6AvmNjuudGU3CG bHT7AtJiU8To2ykH6KT+xLhqNE764vyeLa49rn9MaK5vj04vKyACxuOf/ya7BeWLoQXzo 3L2yO/bn8M6vYZMj/0jxL9vh8xnqnVH/Jk74RADAbBoqvADZUMxz7dQYwc54kRkmJAME1 dzy3OFZVD5A7pt5W7adzNaPRjJ7OnTstMnF0zHmxpHRy+affvrvZV9kKUNTZxEmbQ+OaG G0RlTaTeeyywKgMuSMBXWfeE1IHFQPJEF1X9GoAQYmn3/T7O8aqqiz4qoEo5hMqScBA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=HFXJi+iV; arc=none smtp.remote-ip=91.218.175.135 Received: from mta0.migadu.com (out-135.mta0.migadu.com [91.218.175.135]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AEC8A606A31E for ; Mon, 23 Nov 2026 01:28:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org AEC8A606A31E Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=HFXJi+iV X-Envelope-To: linux-doc@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=yfkDOFNangaKsew0JIB+tHmbNmAGU3mvMCj7TinFEwA=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1795397305; v=1; x=1796002105; b=HFXJi+iVfgmqTCIRPIxX4n5w2KGjTayE6KuyYt42eqaZJ03ZOaxoubQ3tpkB/1aabwoJrRu1 l7vhJdqbDNb2WnBAJoEjY6D3PYH78e2KJ8WzrsY/mpOTbtVe5LeGG7MyJwPFRF9TQb0e7S7Ig/f N8y/Hi81NRbbH4uD4jenQA5g= X-Envelope-To: linux-doc@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 20b39d6dbaa3b65f; Mon, 23 Nov 2026 01:28:25 +0000 X-Mizu-Trace-ID: 20b39d6dbaa3b65f X-Migadu-Flow: FLOW_OUT Message-ID: Date: Mon, 23 Nov 2026 09:28:07 +0800 X-Mailing-List: linux-doc@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [RFC PATCH v2 6/8] fs: add FMODE_CAN_DMABUF_IO and require it for dma-buf I/O contexts To: Christian Brauner , Christoph Hellwig Cc: Anuj Gupta , Nitesh Shetty , Jens Axboe , Sumit Semwal , christian.koenig@amd.com, Philipp Stanner , Dan Williams , Matthew Wilcox , Jan Kara , Alexander Viro , tytso@mit.edu, Andreas Dilger , Baokun Li , Ojaswin Mujoo , "Ritesh Harjani (IBM)" , Zhang Yi , asml.silence@gmail.com, Jonathan Corbet , Shuah Khan , Randy Dunlap , linux-block@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, nvdimm@lists.linux.dev, linux-ext4@vger.kernel.org, linux-xfs@vger.kernel.org, linux-doc@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, Li Wang , Mengmeng Zhao References: <20261113070621.273953-1-mengmeng.zhao@linux.dev> <20261113070621.273953-7-mengmeng.zhao@linux.dev> <20261120-faust-bohnenkraut-reduzieren-a30811186a15@brauner> From: Mengmeng Zhao In-Reply-To: <20261120-faust-bohnenkraut-reduzieren-a30811186a15@brauner> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 11/20/26 17:55, Christian Brauner wrote: > On Fri, Nov 13, 2026 at 03:06:19PM +0800, Mengmeng Zhao wrote: >> From: Mengmeng Zhao >> >> Add a file mode bit for files that can be the target of dma-buf I/O and >> require it when a buffer is registered. Block device files set it when >> the backing disk supports dma-buf I/O, and filesystems can set it for >> inodes whose data can be reached without the page cache. The bit is >> coarse: a driver that installs ->init_dma_buf_io_ctx for every >> namespace, like nvme, still decides per device at registration. >> >> dma_buf_io_ctx_create() is the only caller of ->init_dma_buf_io_ctx, so >> the capability and the O_DIRECT requirement are checked there for every >> file type rather than in each helper; O_DIRECT belongs at registration >> because F_SETFL can change it afterwards. Add bdev_can_dmabuf_io() so >> the open-time and registration checks share one predicate. >> >> Signed-off-by: Mengmeng Zhao >> Signed-off-by: Li Wang >> --- >> block/bdev.c | 8 ++++---- >> block/fops.c | 2 +- >> drivers/dma-buf/dma-buf-io.c | 13 +++++++++---- >> include/linux/blkdev.h | 7 ++++++- >> include/linux/fs.h | 8 +++++++- >> 5 files changed, 27 insertions(+), 11 deletions(-) >> >> diff --git a/block/bdev.c b/block/bdev.c >> index 88e3f552f5d4..4e7a7f7e1e8b 100644 >> --- a/block/bdev.c >> +++ b/block/bdev.c >> @@ -61,14 +61,12 @@ struct block_device *file_bdev(struct file *bdev_file) >> } >> EXPORT_SYMBOL(file_bdev); >> >> -int bdev_init_dma_buf_io_ctx(struct file *file, struct block_device *bdev, >> +int bdev_init_dma_buf_io_ctx(struct block_device *bdev, >> struct dma_buf_io_ctx *ctx) >> { >> - if (!(file->f_flags & O_DIRECT)) >> - return -EINVAL; >> if (ctx->dmabuf->size > UINT_MAX) >> return -EINVAL; >> - if (!bdev->bd_disk->fops->init_dma_buf_io_ctx) >> + if (!bdev_can_dmabuf_io(bdev)) >> return -EOPNOTSUPP; >> return bdev->bd_disk->fops->init_dma_buf_io_ctx(bdev, ctx); >> } >> @@ -1041,6 +1039,8 @@ int bdev_open(struct block_device *bdev, blk_mode_t mode, void *holder, >> >> bdev_file->f_flags |= O_LARGEFILE; >> bdev_file->f_mode |= FMODE_CAN_ODIRECT; >> + if (bdev_can_dmabuf_io(bdev)) >> + bdev_file->f_mode |= FMODE_CAN_DMABUF_IO; >> if (bdev_nowait(bdev)) >> bdev_file->f_mode |= FMODE_NOWAIT; >> if (mode & BLK_OPEN_RESTRICT_WRITES) >> diff --git a/block/fops.c b/block/fops.c >> index 86bf1f298b79..7e5702e047a2 100644 >> --- a/block/fops.c >> +++ b/block/fops.c >> @@ -1008,7 +1008,7 @@ static int blkdev_mmap_prepare(struct vm_area_desc *desc) >> static int blkdev_init_dma_buf_io_ctx(struct file *file, >> struct dma_buf_io_ctx *ctx) >> { >> - return bdev_init_dma_buf_io_ctx(file, file_bdev(file), ctx); >> + return bdev_init_dma_buf_io_ctx(file_bdev(file), ctx); >> } >> >> const struct file_operations def_blk_fops = { >> diff --git a/drivers/dma-buf/dma-buf-io.c b/drivers/dma-buf/dma-buf-io.c >> index 8440151de860..9575b7d65ec5 100644 >> --- a/drivers/dma-buf/dma-buf-io.c >> +++ b/drivers/dma-buf/dma-buf-io.c >> @@ -152,9 +152,11 @@ static void dma_buf_io_ctx_shutdown(struct dma_buf_io_ctx *ctx) >> * @out_ctx: [out] resulting ctx >> * >> * Create a new I/O context from the given DMA buffer for the given file. >> - * It only works with files implementing ->init_dma_buf_io_ctx and fails >> - * with -EOPNOTSUPP otherwise. Once created, the user can create a map >> - * and use it to issue I/O, such as read/write requests, to @file. >> + * It only works with files implementing ->init_dma_buf_io_ctx, >> + * advertising FMODE_CAN_DMABUF_IO and currently opened for direct I/O, >> + * and fails with -EOPNOTSUPP or -EINVAL otherwise. Once created, the >> + * user can create a map and use it to issue I/O, such as read/write >> + * requests, to @file. >> */ >> int dma_buf_io_ctx_create(struct file *file, >> struct dma_buf *dmabuf, >> @@ -164,8 +166,11 @@ int dma_buf_io_ctx_create(struct file *file, >> struct dma_buf_io_ctx *ctx; >> int ret; >> >> - if (!file->f_op->init_dma_buf_io_ctx) >> + if (!file->f_op->init_dma_buf_io_ctx || >> + !(file->f_mode & FMODE_CAN_DMABUF_IO)) >> return -EOPNOTSUPP; >> + if (!(file->f_flags & O_DIRECT)) >> + return -EINVAL; >> >> ctx = kzalloc_obj(*ctx); >> if (!ctx) >> diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h >> index b98f43478cc0..0cfb6fb6b4e2 100644 >> --- a/include/linux/blkdev.h >> +++ b/include/linux/blkdev.h >> @@ -1610,9 +1610,14 @@ struct block_device_operations { >> int (*alternative_gpt_sector)(struct gendisk *disk, sector_t *sector); >> }; >> >> -int bdev_init_dma_buf_io_ctx(struct file *file, struct block_device *bdev, >> +int bdev_init_dma_buf_io_ctx(struct block_device *bdev, >> struct dma_buf_io_ctx *ctx); >> >> +static inline bool bdev_can_dmabuf_io(struct block_device *bdev) >> +{ >> + return !!bdev->bd_disk->fops->init_dma_buf_io_ctx; >> +} >> + >> #ifdef CONFIG_COMPAT >> extern int blkdev_compat_ptr_ioctl(struct block_device *, blk_mode_t, >> unsigned int, unsigned long); >> diff --git a/include/linux/fs.h b/include/linux/fs.h >> index 99caef43ff3c..f5524fc770d4 100644 >> --- a/include/linux/fs.h >> +++ b/include/linux/fs.h >> @@ -121,7 +121,13 @@ typedef int (dio_iodone_t)(struct kiocb *iocb, loff_t offset, >> /* File supports atomic writes */ >> #define FMODE_CAN_ATOMIC_WRITE ((__force fmode_t)(1 << 7)) >> >> -/* FMODE_* bit 8 */ >> +/* >> + * File can be the target of dma-buf I/O. Set it for files whose data can be >> + * reached with direct I/O without the page cache: a dma-buf iterator has no >> + * pages to copy, so a buffered path cannot serve it. The bit is coarse; both >> + * the device behind it and O_DIRECT are checked when a buffer is registered. >> + */ >> +#define FMODE_CAN_DMABUF_IO ((__force fmode_t)(1 << 8)) > > This is stealing yet another FMODE_* bit for special-purpose stuff. > We're really really short of them. Did Christoph suggest this? Hi Christian, thanks for pointing out this. If I understand correctly, Christoph suggests a new file->f_mode to allow dma-buf check in RFC v1[1]. Hi Christoph, do you have any comments or suggestions? I'am happy to adjust this in the next version if there is a better approach. [1] https://lore.kernel.org/all/20261106104403.GC5668@lst.de/