From nobody Sat Oct 3 12:03:09 2026 Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C41304C7539 for ; Mon, 28 Sep 2026 13:32:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602371; cv=none; b=uGPvkeYod1DZjva5oJZISi1Huh5nAYp31sMmA+EQyM82IiRzdTnglK2aH6GJRw8t/xeRhDnwCoemWLfXzqubZp4h6ZAHG3G1QmUN33NUM2O8rMNY3cHZi5WgZzCGIwBZ0pRpgOLnCNIFgI8Ik8SV14MpH16+qHRnqNY6rjLwL98= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602371; c=relaxed/simple; bh=OUu5s0DH6j8kEpt73a0qm6d5FwFY3CbjYixE2icdfTg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=pJEhnU2VrPRDVV2STC8ODlfZyQsvfGCjezKLNj6J6HmweSZruwLHZiGxpmXQiWBecsIGwSp/YFZOSIHwIcw4dY4lkDCKmSfLB3l6djPiOFy6eCSSVbqfS69rxokKos/VGxthEo++Y7F7W5JrX6BQr3MBQeOvIm6Dl5Ueqm5WmeU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=RRUSd5IN; arc=none smtp.client-ip=209.85.221.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="RRUSd5IN" Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-4887d3c59ecso1654605f8f.1 for ; Mon, 28 Sep 2026 06:32:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790602366; x=1791207166; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=huu8rtooY5bex1ADru0xT42aS1Jtedee7w+CWw922aM=; b=RRUSd5INZYiTinRF9EKStYjqoxdZOyoTGHiHpRUf5sXabmeqoCRdJDaQUJ52Ax1OpH l3QWe6GBq0K3EET2k6uPq1kvKSfdbLEHh7j+0vZmghTGCXNPN4bzheDgln+rElKRCWt4 +U3qJM3k5zvtAwGnJ4SqLyAMAO2l79AVuiL3qmzok5KBGXTuoyrvmDEuC/YnbIfGarsQ Oeqn46SHZ8XmTOCEythIycXjcO/whuqTR4lfM4998eCiWB6aPsvDXDY308BQi9WRWuan WHtDvd0ARl2w9/BCGAeoPZ2ii6v5PMKylhfqRdMqwjdebpgSj3/b4h684gxS/Tddshsi wwXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790602366; x=1791207166; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=huu8rtooY5bex1ADru0xT42aS1Jtedee7w+CWw922aM=; b=I2PckygYqjIR7PI/iar9EWQuu4wNI3Pmj8m/f2FZwQxgT9pvVegTF1FJDuF4eviv38 JE9Fiqpnxcbddz2v2/6Op44MApbSy+SgDbU1eRas0dCXTe84i+szjyQKQZIoqUbWoP5L rxm1HYIk0MHITK9ZMrRIg94XmZvuSVw0gK94V0aS3VIwA3ALNvaCtZGwUuAnkLY7caAE vaH63DStMimUPMsGDNmXFkc5Y7Cz4gvbmlR+86Lp+ju+fY+upIkwsfTB7917hKa1mkRC jDixo3+8UPKsOq6FhHb1FmM5BVdEPXISA9/cGn29sSj2MfvgDpqwI8YRt2Cv48D7tgwu DnPQ== X-Forwarded-Encrypted: i=1; AKwUvBy9qUYkQkVywB7scVs6HYxSr/LBgBN7BjBHRj3FRIw5kbBdBj8nAkhJxF9XUzFzgB8d/+0hp1OUz0kiILY=@vger.kernel.org X-Gm-Message-State: AFq9FYLhTWk/w6Vpt6pFbpDZcpqenWlN+hbpsoXUolmRwkZMPzYeXxbt NFPPlyCXm9NpYaKnWmdIMWISCiyzISv3cKqwoAjoh9tD4k014mDla/Eq X-Gm-Gg: AYBFou2Hk2gaEam062X9XmeUVLxSr8aMagDEVbCMX7majthLHxh+gWxrDi7c1oDV9Ws IdfI2AwXk9i1H2ZADtjsz4JCr9vK2/5V0lv9dixlZro0hipDEEthEP8FSguT6A2NpKUstog9luK XlPnABp78cEb01+qg3ojRJFSTn5zP7Ca1JCE6X3V8pay8MrIU4sfPfjYPaJ6iaRSDL3Lk6T/NMi BXLDPyOSI3yBBYVALmM7RZ8kaD98sXmF8Ho5LAYQAScNzYPFnkQdyPQaMRRevpWRXmTgJwSaMQG szACdgfHmne49KkeThFBdRc4Fru+n0OdHNEvBGKWkFLuQ1IOaHTCu6bBTv9UoSXMGSugyr4b/Cv /5/MK1WsZ8jRwFI5e+Ps1+ZLadcZhkGcUCiAfs0b/cT+0D4Oe8VmVEBLgAswScL2PjGh+Icvk5j 0pNeyxZ8CWt6iMTJ2griwhUyFYWZ2p37ijt699Nh0jgt53RK/xKBr14dGrmTsmEhZEhB9x0NegI oenLTwqcMY/Vxcq4WmVk8JColdiDYXs2Di1uw2LKjbA382mqgyZEfncayJ43WMmZL8irinbqs2q X-Received: by 2002:a05:6000:2406:b0:488:868a:1b88 with SMTP id ffacd0b85a97d-488868a1bd3mr13642177f8f.5.1790602365500; Mon, 28 Sep 2026 06:32:45 -0700 (PDT) Received: from 127.mynet ([2a01:4b00:bd21:4f00:7cc6:d3ca:494:116c]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4887a349731sm28090164f8f.12.2026.09.28.06.32.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2026 06:32:44 -0700 (PDT) From: Pavel Begunkov To: linux-block@vger.kernel.org Cc: asml.silence@gmail.com, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-nvme@lists.infradead.org, linux-fsdevel@vger.kernel.org, io-uring@vger.kernel.org, Christoph Hellwig , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Keith Busch , Sagi Grimberg , Alexander Viro , Christian Brauner , Jan Kara , Andrew Morton , Jens Axboe , Nitesh Shetty , Kanchan Joshi , Anuj Gupta , Tushar Gohad , William Power , Phil Cayton , Matthew Brost , Alasdair Kergon , Mike Snitzer , Mikulas Patocka , Benjamin Marzinski , dm-devel@lists.linux.dev Subject: [PATCH v7 01/13] dma-buf: introduce initial file I/O infrastructure Date: Mon, 28 Sep 2026 14:32:17 +0100 Message-ID: X-Mailer: git-send-email 2.54.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The goal is to be able to natively use dma-buf in the read-write / IO path. This patch adds basic building blocks serving as a glue and API between drivers and upper layer subsystems providing the uAPI. Later patches implement it for NVMe raw block devices and expose it to the user space via io_uring. There are two main objects. struct dma_buf_io_ctx and struct dma_buf_io_map. The ctx is used during initial registration and serves as an interface between the upper layer user like io_uring and to the importer subsystem / driver. The map represents the actual dma map established for the target device[s] with dma_buf_map_attachment() and stored in a device specific format. The context is created via a new file operation ->init_dma_buf_io_ctx. The ctx-map separation exists to support map invalidation (see dma_buf_io_invalidate_mappings()). A ctx can create multiple maps during its lifetime, but there can only be no more than one (active) map attached to it. Invalidation drops the active map if present, and the next map will only be attempted to be created once there is a new request that wants to use the dma-buf IO ctx. The primary task of the dma_buf_io_map object is to count requests using it and to wait for their completion when we want to destroy the DMA map. [un]mapping and any work with dma addresses is delegated to the importer driver via an ops table stored in the ctx, see struct dma_buf_io_ops. Only the target driver / subsystem knows about devices it wants to use the dma-buf with, especially in case of multi-device filesystems or stacking in the future. Signed-off-by: Pavel Begunkov --- drivers/dma-buf/Makefile | 2 +- drivers/dma-buf/dma-buf-io.c | 230 +++++++++++++++++++++++++++++++++++ include/linux/dma-buf-io.h | 117 ++++++++++++++++++ include/linux/fs.h | 2 + 4 files changed, 350 insertions(+), 1 deletion(-) create mode 100644 drivers/dma-buf/dma-buf-io.c create mode 100644 include/linux/dma-buf-io.h diff --git a/drivers/dma-buf/Makefile b/drivers/dma-buf/Makefile index b25d7550bacf..523731b0f83e 100644 --- a/drivers/dma-buf/Makefile +++ b/drivers/dma-buf/Makefile @@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only obj-y :=3D dma-buf.o dma-fence.o dma-fence-array.o dma-fence-chain.o \ - dma-fence-unwrap.o dma-resv.o dma-buf-mapping.o + dma-fence-unwrap.o dma-resv.o dma-buf-mapping.o dma-buf-io.o obj-$(CONFIG_DMABUF_HEAPS) +=3D dma-heap.o obj-$(CONFIG_DMABUF_HEAPS) +=3D heaps/ obj-$(CONFIG_SYNC_FILE) +=3D sync_file.o diff --git a/drivers/dma-buf/dma-buf-io.c b/drivers/dma-buf/dma-buf-io.c new file mode 100644 index 000000000000..420379a8db81 --- /dev/null +++ b/drivers/dma-buf/dma-buf-io.c @@ -0,0 +1,230 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Common infrastructure for supporing dma-buf in the I/O path. + * + * Copyright (C) 2026 Pavel Begunkov + */ +#include +#include + +static void dma_buf_io_put_ctx(struct dma_buf_io_ctx *ctx) +{ + might_sleep(); + + if (!refcount_dec_and_test(&ctx->refs)) + return; + if (WARN_ON_ONCE(rcu_dereference_protected(ctx->map, true))) + return; + + ctx->dev_ops->release(ctx); + + dma_buf_put(ctx->dmabuf); + mutex_destroy(&ctx->map_mutex); + mutex_destroy(&ctx->map_create_mutex); + kfree(ctx); +} + +static void dma_buf_io_map_release_work(struct work_struct *work) +{ + struct dma_buf_io_map *map =3D container_of(work, struct dma_buf_io_map, + release_work); + struct dma_buf_io_ctx *ctx =3D map->ctx; + struct dma_buf *dmabuf =3D ctx->dmabuf; + + percpu_ref_exit(&map->refs); + + dma_resv_lock(dmabuf->resv, NULL); + ctx->dev_ops->unmap(ctx, map); + dma_resv_unlock(dmabuf->resv); + + atomic_dec(&ctx->all_maps); + wake_up(&ctx->maps_wq); + dma_buf_io_put_ctx(ctx); +} + +static void dma_buf_io_map_release_rcu(struct rcu_head *rcu) +{ + struct dma_buf_io_map *map =3D container_of(rcu, struct dma_buf_io_map, r= cu); + + INIT_WORK(&map->release_work, dma_buf_io_map_release_work); + queue_work(system_percpu_wq, &map->release_work); +} + +static void dma_buf_io_map_refs_release(struct percpu_ref *ref) +{ + struct dma_buf_io_map *map =3D container_of(ref, struct dma_buf_io_map, r= efs); + struct dma_buf_io_ctx *ctx =3D map->ctx; + + /* There are no more requests using the map. */ + atomic_dec(&ctx->active_maps); + wake_up(&ctx->maps_wq); + /* The map might still be visible via ctx->map */ + call_rcu(&map->rcu, dma_buf_io_map_release_rcu); +} + +static void dma_buf_io_wait_active_maps(struct dma_buf_io_ctx *ctx) +{ + wait_event(ctx->maps_wq, atomic_read(&ctx->active_maps) =3D=3D 0); +} + +static void dma_buf_io_wait_maps(struct dma_buf_io_ctx *ctx) +{ + wait_event(ctx->maps_wq, atomic_read(&ctx->all_maps) =3D=3D 0); +} + +int dma_buf_io_init_map(struct dma_buf_io_ctx *ctx, struct dma_buf_io_map = *map, + struct sg_table *sgt) +{ + unsigned seg_shift =3D ~0U; + struct scatterlist *sg; + unsigned long tmp; + int ret; + + for_each_sgtable_dma_sg(sgt, sg, tmp) + seg_shift =3D min(seg_shift, __ffs(sg_dma_len(sg))); + + ret =3D percpu_ref_init(&map->refs, dma_buf_io_map_refs_release, 0, + GFP_KERNEL); + if (ret) + return ret; + map->min_seg_shift =3D seg_shift; + map->ctx =3D ctx; + return 0; +} +EXPORT_SYMBOL_NS_GPL(dma_buf_io_init_map, "DMA_BUF"); + +struct dma_buf_io_map *dma_buf_io_create_map(struct dma_buf_io_ctx *ctx) +{ + struct dma_buf *dmabuf =3D ctx->dmabuf; + struct dma_buf_io_map *map; + long ret; + + guard(mutex)(&ctx->map_create_mutex); + + scoped_guard(mutex, &ctx->map_mutex) { + if (ctx->maps_killed) + return ERR_PTR(-ENOENT); + /* recheck under the lock in case it has already been re-created */ + map =3D __dma_buf_io_get_map(ctx); + if (map) + return map; + } + + dma_buf_io_wait_active_maps(ctx); + + ret =3D dma_resv_lock_interruptible(dmabuf->resv, NULL); + if (ret) + return ERR_PTR(ret); + + ret =3D dma_resv_wait_timeout(dmabuf->resv, DMA_RESV_USAGE_KERNEL, + true, MAX_SCHEDULE_TIMEOUT); + if (ret <=3D 0) { + if (!ret) + ret =3D -EAGAIN; + dma_resv_unlock(dmabuf->resv); + return ERR_PTR(ret); + } + + map =3D ctx->dev_ops->map(ctx); + if (!IS_ERR(map)) { + atomic_inc(&ctx->active_maps); + atomic_inc(&ctx->all_maps); + } + dma_resv_unlock(dmabuf->resv); + + if (IS_ERR(map)) + return map; + if (WARN_ON_ONCE(!map->min_seg_shift)) + return ERR_PTR(-EFAULT); + + refcount_inc(&ctx->refs); + /* get a reference for the caller */ + percpu_ref_get(&map->refs); + + scoped_guard(mutex, &ctx->map_mutex) + rcu_assign_pointer(ctx->map, map); + return map; +} + +static void dma_buf_io_kill_maps(struct dma_buf_io_ctx *ctx, bool final) +{ + struct dma_buf_io_map *map; + + scoped_guard(mutex, &ctx->map_mutex) { + if (final) + ctx->maps_killed =3D true; + + map =3D rcu_dereference_protected(ctx->map, + lockdep_is_held(&ctx->map_mutex)); + if (!map) + return; + rcu_assign_pointer(ctx->map, NULL); + percpu_ref_kill(&map->refs); + } +} + +void dma_buf_io_invalidate_mappings(struct dma_buf_io_ctx *ctx) +{ + dma_buf_io_kill_maps(ctx, false); + dma_buf_io_wait_active_maps(ctx); +} +EXPORT_SYMBOL_NS_GPL(dma_buf_io_invalidate_mappings, "DMA_BUF"); + +void dma_buf_io_ctx_release(struct dma_buf_io_ctx *ctx) +{ + /* Remove and wait for the last map, there should be no new ones. */ + dma_buf_io_kill_maps(ctx, true); + dma_buf_io_put_ctx(ctx); +} + +int dma_buf_io_ctx_create(struct file *file, + struct dma_buf *dmabuf, + enum dma_data_direction dir, + struct dma_buf_io_ctx **out_ctx) +{ + struct dma_buf_io_ctx *ctx; + int ret; + + if (!file->f_op->init_dma_buf_io_ctx) + return -EOPNOTSUPP; + + ctx =3D kmalloc_obj(*ctx); + if (!ctx) + return -ENOMEM; + + memset(ctx, 0, sizeof(*ctx)); + ctx->dir =3D dir; + ctx->dmabuf =3D dmabuf; + get_dma_buf(dmabuf); + mutex_init(&ctx->map_mutex); + mutex_init(&ctx->map_create_mutex); + atomic_set(&ctx->active_maps, 0); + atomic_set(&ctx->all_maps, 0); + refcount_set(&ctx->refs, 1); + init_waitqueue_head(&ctx->maps_wq); + + ret =3D file->f_op->init_dma_buf_io_ctx(file, ctx); + if (ret) { + kfree(ctx); + dma_buf_put(dmabuf); + return ret; + } + + if (WARN_ON_ONCE(!ctx->dev_ops || + !ctx->dev_ops->map || + !ctx->dev_ops->unmap || + !ctx->dev_ops->release)) + return -EINVAL; + + *out_ctx =3D ctx; + return 0; +} + +void dma_buf_io_detach(struct dma_buf_io_ctx *ctx) +{ + guard(mutex)(&ctx->map_create_mutex); + + dma_buf_io_kill_maps(ctx, true); + dma_buf_io_wait_maps(ctx); +} +EXPORT_SYMBOL_NS_GPL(dma_buf_io_detach, "DMA_BUF"); diff --git a/include/linux/dma-buf-io.h b/include/linux/dma-buf-io.h new file mode 100644 index 000000000000..9964526cc245 --- /dev/null +++ b/include/linux/dma-buf-io.h @@ -0,0 +1,117 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __DMA_BUF_IO_H__ +#define __DMA_BUF_IO_H__ + +#include + +struct dma_buf_io_ctx; +struct dma_buf_io_map; + +struct dma_buf_io_ops { + /* + * Create a new map for the given ctx. Called with the reservation + * lock held. + */ + struct dma_buf_io_map *(*map)(struct dma_buf_io_ctx *ctx); + + /* + * Clean up device specific parts of the @map. Called with the + * reservation lock held. + */ + void (*unmap)(struct dma_buf_io_ctx *ctx, struct dma_buf_io_map *map); + + /* + * The user tries to destroy the ctx. Release all device specific + * parts of the token. + */ + void (*release)(struct dma_buf_io_ctx *); +}; + +struct dma_buf_io_map { + /* + * Counts attached requests and other users. Device specific unmapping + * is deferred until all refs are dropped. + */ + struct percpu_ref refs; + /* + * Shift for the minimum segment size of the mapping. + */ + unsigned min_seg_shift; + + struct dma_buf_io_ctx *ctx; + union { + struct work_struct release_work; + struct rcu_head rcu; + }; +}; + +struct dma_buf_io_ctx { + struct dma_buf_io_map __rcu *map; + struct dma_buf *dmabuf; + enum dma_data_direction dir; + + /* synchronises map reassignment */ + struct mutex map_mutex; + struct mutex map_create_mutex; + refcount_t refs; + /* maps that may still be in use. */ + atomic_t active_maps; + atomic_t all_maps; + struct wait_queue_head maps_wq; + bool maps_killed; + + void *dev_priv; + const struct dma_buf_io_ops *dev_ops; +}; + +int dma_buf_io_ctx_create(struct file *file, + struct dma_buf *dmabuf, + enum dma_data_direction dir, + struct dma_buf_io_ctx **ctx); +void dma_buf_io_ctx_release(struct dma_buf_io_ctx *ctx); + +struct dma_buf_io_map *dma_buf_io_create_map(struct dma_buf_io_ctx *ctx); + +static inline struct dma_buf_io_map * +__dma_buf_io_get_map(struct dma_buf_io_ctx *ctx) +{ + struct dma_buf_io_map *map; + + guard(rcu)(); + + map =3D rcu_dereference(ctx->map); + if (unlikely(!map || !percpu_ref_tryget_live_rcu(&map->refs))) + return NULL; + + return map; +} + +static inline struct dma_buf_io_map * +dma_buf_io_get_map(struct dma_buf_io_ctx *ctx, bool nowait) +{ + struct dma_buf_io_map *map; + + map =3D __dma_buf_io_get_map(ctx); + if (likely(map)) + return map; + + if (nowait) + return ERR_PTR(-EAGAIN); + return dma_buf_io_create_map(ctx); +} + +static inline void dma_buf_io_map_drop(struct dma_buf_io_map *map) +{ + percpu_ref_put(&map->refs); +} + +/* + * Device API + */ + +void dma_buf_io_invalidate_mappings(struct dma_buf_io_ctx *ctx); +int dma_buf_io_init_map(struct dma_buf_io_ctx *ctx, struct dma_buf_io_map = *map, + struct sg_table *sgt); +void dma_buf_io_detach(struct dma_buf_io_ctx *ctx); + +#endif diff --git a/include/linux/fs.h b/include/linux/fs.h index c5d7301f16f9..fa8ee0943cd4 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -1914,6 +1914,7 @@ struct dir_context { #define COPY_FILE_SPLICE (1 << 0) =20 struct io_uring_cmd; +struct dma_buf_io_ctx; struct offset_ctx; =20 struct file_operations { @@ -1960,6 +1961,7 @@ struct file_operations { int (*uring_cmd_iopoll)(struct io_uring_cmd *, struct io_comp_batch *, unsigned int poll_flags); int (*mmap_prepare)(struct vm_area_desc *); + int (*init_dma_buf_io_ctx)(struct file *, struct dma_buf_io_ctx *); } __randomize_layout; =20 /* Supports async buffered reads */ --=20 2.54.0 From nobody Sat Oct 3 12:03:09 2026 Received: from mail-wr2-f35.google.com (mail-wr2-f35.google.com [74.125.225.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EBFA54BEE38 for ; Mon, 28 Sep 2026 13:32:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.225.99 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602373; cv=none; b=fiJd4XTv21GM96ywHVvy3k2UFF+orzV6l6raONdwWAMZSawrDopKKGdcau8JfeZVnJoerB70z4AWp4EmrOrrHROxdNnxrp2zk3ffd2xm9n6x6OO4PKFXVje/AEVbavnXa5RUvbldxF1/k2EzGJXWw7X29HpAfZcrc+SNoA7zr8Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602373; c=relaxed/simple; bh=r0p2adqwMj/5lT7C+TYVmZHcVYZ+gdU2HSJ4o7+sSAQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ENaZYMj1JG3VBREeQf9VPyV8v61P2jXZxODAHMEPIG6OCPYl+HW3Z0xnkWmVVAP9RiEcXhijPPOC0zCL0+BcIjo0tCiJUQ7iUxEKrNfz8VT0wNaMjWcDg01TBEtswy81N2H1WtGOSVnXJY8UVbhYNWWYfIXrVuQ31OOoghuDtJw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=EFxPyctB; arc=none smtp.client-ip=74.125.225.99 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="EFxPyctB" Received: by mail-wr2-f35.google.com with SMTP id ffacd0b85a97d-488780459e2so2990741f8f.2 for ; Mon, 28 Sep 2026 06:32:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790602367; x=1791207167; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=u2SC7dcI1Y+PPMkABrALMaBjWk253olXayI/6F0cq90=; b=EFxPyctB6X5CY4qSFxOSKFZlariS+3VnxLnN6w9RS8ECTZOb/SNS8v0OhrbJJPGa5L DS3+rrFGTsZvoF+Tz4tgMjnHrIuolgEL/2wMv74eJPeWf5R4kY6PpO7YrpQ1K/KZMjWl UisDGpg//bE5Y4ZifA5M0HM6M8+E5xWBLBhU/bOwulbguqtnYnOiRhUomP74wmEaDNO9 SSdJQGI+KuJ+3Ez2VHuhPityGsgIcQ+cKJEc+c8DEhU34DCGwI1crl2oK9gWNQaMOJgZ 4IKyGlsiMWA3UijWHm1/xtRm/eHz22nJCj6/3EVUV1hcYsAVI39WyOugsMWCdv0Uve96 xHlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790602367; x=1791207167; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=u2SC7dcI1Y+PPMkABrALMaBjWk253olXayI/6F0cq90=; b=aaLWTlY2DmrLmuYp5yNe441Ce+XURu0ni65thpO6bi5RqMGmso/tW1dE62wJ0I4tkJ TtMrzSBjauNLIblqqUR/lQesb+SeGHfIi2M1hoYyyD80ogeTaFkGItsVQSJeJqUEu+Uy BJK6jC0LyiNXyZAFq9sSfzxo7kyPyBktaTyZv1SNSAW7E3G+NxkQ3qay7vp8w7Fcljzq 1O1wkATxXh1DBLMf0SkWAoufgJPgKV7YKccMdvDh0KSqm3msWqgJeqWmAGTRVyY+l+Mg /hvVV221Luo+2OY5gW3Qw9GM5WqJjurp++FqWlv8IxL0DfxkDTT7Or3Dp/3sAuGUDRJX SsCQ== X-Forwarded-Encrypted: i=1; AKwUvBxjwdBp5HYYjaJsZvju1nnhbzMnJys4FtT5oOdSyqLG8bT/PlVW5c4EzJSOJ+3J/z4m/qzw0tKBPvf8hoU=@vger.kernel.org X-Gm-Message-State: AFq9FYLJn67ALL/9V8rnQ9yqZDCbIyQ9SRYsMhnKzlxV90JdiyLdSQxX volWnRCtbzN3y+hL9bW5SBif1TaQkacdy35ivNMr8Ks0iL8FownIliaO X-Gm-Gg: AYBFou0HkThga1ze6VIJLNabc8tJ4dQGoIoQdwHHMyKY+4i5yVH3tRxGdmG1QFs0ZZa YnZOmrLcj51tFPda4Fg+1ODH8tLstETWmbzduygTuGSJEs79SOIjo4PMxjWkb866EmXS7vV+U45 V9EXqxTEdjqkW8HMljTE+x2wfh4/7ssVqEuGn6Yo7htsgLq4TipRSVf3v/bd67eNR8SZrWLK1b/ CQEFPQBFkMiPwsHxD9htkUwi5Pbfs+fr8hYfcHzrGFNUteSvXnIia+AjFMy62lIpxHI0fRpx+ht bIO3/Ofx+d1nNXvzSvqyQmOSn+OjKs27ENjG6ShrAUQUWP8bR7I1ao2WOGhj/h49hCYSABD+Pxr LPdMylZImCfEGZZB5qULWnm2MGw6tUopDbKzf5Mdk7UFEfSUe5+pPwG+kx76JJ/A7RtOo2cw8BH O8d7Zdb6yE03zEAo2alkKme9qsiKFCsI0RJKjrFsdmGQxryn3jzhnhaNclTYBXxY802XSr8padx 7yGM3lfwe51YD7t3lDjb745A7DPQSZvGFp3ZUGVWUyYRHM/B497KVU65mGWEJWFEAjXQX7lHNSS S17eljdrdEY= X-Received: by 2002:a05:6000:2083:b0:488:5c28:94d0 with SMTP id ffacd0b85a97d-4887172596fmr24595420f8f.19.1790602366822; Mon, 28 Sep 2026 06:32:46 -0700 (PDT) Received: from 127.mynet ([2a01:4b00:bd21:4f00:7cc6:d3ca:494:116c]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4887a349731sm28090164f8f.12.2026.09.28.06.32.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2026 06:32:46 -0700 (PDT) From: Pavel Begunkov To: linux-block@vger.kernel.org Cc: asml.silence@gmail.com, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-nvme@lists.infradead.org, linux-fsdevel@vger.kernel.org, io-uring@vger.kernel.org, Christoph Hellwig , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Keith Busch , Sagi Grimberg , Alexander Viro , Christian Brauner , Jan Kara , Andrew Morton , Jens Axboe , Nitesh Shetty , Kanchan Joshi , Anuj Gupta , Tushar Gohad , William Power , Phil Cayton , Matthew Brost , Alasdair Kergon , Mike Snitzer , Mikulas Patocka , Benjamin Marzinski , dm-devel@lists.linux.dev Subject: [PATCH v7 02/13] iov_iter: add iterator type for dmabuf maps Date: Mon, 28 Sep 2026 14:32:18 +0100 Message-ID: <74a9d65f5c4f9acce5f57ad5921a9fadc6eda712.1790602223.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Introduce a new iterator type for dmabuf maps. The map in an opaque object with internals and format specific to the subsystem / driver, and only it can use that subsystem / driver for issuing IO. The task of the middle layers is to pass the map / iterator further down, maybe doing basic splitting and length checking. The iterator can only be used by operations of the file the associated map was created for. Suggested-by: Keith Busch Reviewed-by: Christoph Hellwig Signed-off-by: Pavel Begunkov --- include/linux/iov_iter.h | 2 ++ include/linux/uio.h | 11 +++++++++++ lib/iov_iter.c | 29 +++++++++++++++++++++++------ 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/include/linux/iov_iter.h b/include/linux/iov_iter.h index f9a17fbbd398..2f91884d733b 100644 --- a/include/linux/iov_iter.h +++ b/include/linux/iov_iter.h @@ -310,6 +310,8 @@ size_t iterate_and_advance2(struct iov_iter *iter, size= _t len, void *priv, return iterate_folioq(iter, len, priv, priv2, step); if (iov_iter_is_xarray(iter)) return iterate_xarray(iter, len, priv, priv2, step); + if (WARN_ON_ONCE(iov_iter_is_dmabuf_map(iter))) + return 0; return iterate_discard(iter, len, priv, priv2, step); } =20 diff --git a/include/linux/uio.h b/include/linux/uio.h index fe2e985d74d2..638c1116e912 100644 --- a/include/linux/uio.h +++ b/include/linux/uio.h @@ -12,6 +12,7 @@ =20 struct page; struct folio_queue; +struct dma_buf_io_map; =20 typedef unsigned int __bitwise iov_iter_extraction_t; =20 @@ -29,6 +30,7 @@ enum iter_type { ITER_FOLIOQ, ITER_XARRAY, ITER_DISCARD, + ITER_DMABUF_MAP, }; =20 #define ITER_SOURCE 1 // =3D=3D WRITE @@ -71,6 +73,7 @@ struct iov_iter { const struct folio_queue *folioq; struct xarray *xarray; void __user *ubuf; + struct dma_buf_io_map *dmabuf_map; }; size_t count; }; @@ -155,6 +158,11 @@ static inline bool iov_iter_is_xarray(const struct iov= _iter *i) return iov_iter_type(i) =3D=3D ITER_XARRAY; } =20 +static inline bool iov_iter_is_dmabuf_map(const struct iov_iter *i) +{ + return iov_iter_type(i) =3D=3D ITER_DMABUF_MAP; +} + static inline unsigned char iov_iter_rw(const struct iov_iter *i) { return i->data_source ? WRITE : READ; @@ -300,6 +308,9 @@ void iov_iter_folio_queue(struct iov_iter *i, unsigned = int direction, unsigned int first_slot, unsigned int offset, size_t count); void iov_iter_xarray(struct iov_iter *i, unsigned int direction, struct xa= rray *xarray, loff_t start, size_t count); +void iov_iter_dmabuf_map(struct iov_iter *i, unsigned int direction, + struct dma_buf_io_map *map, + loff_t off, size_t count); ssize_t iov_iter_get_pages2(struct iov_iter *i, struct page **pages, size_t maxsize, unsigned maxpages, size_t *start); ssize_t iov_iter_get_pages_alloc2(struct iov_iter *i, struct page ***pages, diff --git a/lib/iov_iter.c b/lib/iov_iter.c index 2072c04e99d0..6831a5d9396e 100644 --- a/lib/iov_iter.c +++ b/lib/iov_iter.c @@ -575,7 +575,8 @@ void iov_iter_advance(struct iov_iter *i, size_t size) { if (unlikely(i->count < size)) size =3D i->count; - if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i))) { + if (likely(iter_is_ubuf(i)) || unlikely(iov_iter_is_xarray(i)) || + unlikely(iov_iter_is_dmabuf_map(i))) { i->iov_offset +=3D size; i->count -=3D size; } else if (likely(iter_is_iovec(i) || iov_iter_is_kvec(i))) { @@ -631,7 +632,8 @@ void iov_iter_revert(struct iov_iter *i, size_t unroll) return; } unroll -=3D i->iov_offset; - if (iov_iter_is_xarray(i) || iter_is_ubuf(i)) { + if (iov_iter_is_xarray(i) || iter_is_ubuf(i) || + iov_iter_is_dmabuf_map(i)) { BUG(); /* We should never go beyond the start of the specified * range since we might then be straying into pages that * aren't pinned. @@ -775,6 +777,20 @@ void iov_iter_xarray(struct iov_iter *i, unsigned int = direction, } EXPORT_SYMBOL(iov_iter_xarray); =20 +void iov_iter_dmabuf_map(struct iov_iter *i, unsigned int direction, + struct dma_buf_io_map *map, + loff_t off, size_t count) +{ + WARN_ON(direction & ~(READ | WRITE)); + *i =3D (struct iov_iter){ + .iter_type =3D ITER_DMABUF_MAP, + .data_source =3D direction, + .dmabuf_map =3D map, + .count =3D count, + .iov_offset =3D off, + }; +} + /** * iov_iter_discard - Initialise an I/O iterator that discards data * @i: The iterator to initialise. @@ -856,7 +872,7 @@ unsigned long iov_iter_alignment(const struct iov_iter = *i) return iov_iter_alignment_bvec(i); =20 /* With both xarray and folioq types, we're dealing with whole folios. */ - if (iov_iter_is_folioq(i)) + if (iov_iter_is_folioq(i) || iov_iter_is_dmabuf_map(i)) return i->iov_offset | i->count; if (iov_iter_is_xarray(i)) return (i->xarray_start + i->iov_offset) | i->count; @@ -872,7 +888,7 @@ unsigned long iov_iter_gap_alignment(const struct iov_i= ter *i) size_t size =3D i->count; unsigned k; =20 - if (iter_is_ubuf(i)) + if (iter_is_ubuf(i) || iov_iter_is_dmabuf_map(i)) return 0; =20 if (WARN_ON(!iter_is_iovec(i))) @@ -1469,11 +1485,12 @@ EXPORT_SYMBOL_GPL(import_ubuf); void iov_iter_restore(struct iov_iter *i, struct iov_iter_state *state) { if (WARN_ON_ONCE(!iov_iter_is_bvec(i) && !iter_is_iovec(i) && - !iter_is_ubuf(i)) && !iov_iter_is_kvec(i)) + !iter_is_ubuf(i) && !iov_iter_is_kvec(i) && + !iov_iter_is_dmabuf_map(i))) return; i->iov_offset =3D state->iov_offset; i->count =3D state->count; - if (iter_is_ubuf(i)) + if (iter_is_ubuf(i) || iov_iter_is_dmabuf_map(i)) return; /* * For the *vec iters, nr_segs + iov is constant - if we increment --=20 2.54.0 From nobody Sat Oct 3 12:03:09 2026 Received: from mail-wr2-f12.google.com (mail-wr2-f12.google.com [74.125.225.76]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9D23B4C9E09 for ; Mon, 28 Sep 2026 13:32:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.225.76 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602374; cv=none; b=g2OXfnyexACp8bw5ale04eiQlU95rVeBXeX9H1j902cI1AUX+cPUyngDJgEKhYYBtHWNsxy6Vlpt3nXmYpj3mPbKxPvJI5ciH4gIjCbcZM5tnIGnwl/cdPaygmR6EEQdcqdarrGUJMrTbrfKa/BoafviMEX8tKHonGdzcGJmMj8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602374; c=relaxed/simple; bh=io+mbOBr0Rt7TH/wdx/lZ5oAtsvxfyuCrmFKA9OR3tU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nrStS1MrJIWNqV71V62RQu2Nv9EiJH4qvcIvykps3PclIwTra3kSIZTF2wMcZbKqePW7MZ8vywG/dSK81YPYCtvUToZ2rkx7COhuhMyqhBViDSbsOR5KpijiCenBkEgt4WiKPFKg1rNsotUPeDcO1fSx7WZoM0So7Z1irnOHC0Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Aw3XFRZT; arc=none smtp.client-ip=74.125.225.76 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Aw3XFRZT" Received: by mail-wr2-f12.google.com with SMTP id ffacd0b85a97d-4843f22dcb8so2313624f8f.0 for ; Mon, 28 Sep 2026 06:32:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790602368; x=1791207168; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=OcUXv3wgMOdU2Q5PfJeVzuf4KJvyhVvGu/xuChutEvA=; b=Aw3XFRZThrnFfZ4NNrryBnBx8rKYreyC+wG6xeLdRp72/nQBOP2LLNpSBikDWaAqUg B1Cc2nsr0YNiHZuzqepaE4qOdLBRkPtZ/q2lECdNi96tOaIOUhljX+piUy9R/ZDij2zv EJpQ0nTCs03EX6n0tFfjAO0LaxQYLe0Vbf2A4wlUxsMqgHOOfc4MHDOm82alsW56zirU u0vcOZs8uyUGHl3hWmI5ayUV05BUfIGbJdq+lnkQ/IBDDaFxYSaku1GxlrroHfTOPswe sg68AfqotYirUkk5Teg7eA4xEUmGb1HcmEqxmwwEtyYmw8nk+T6yLamjdbQ9pRm96Dkh snAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790602368; x=1791207168; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=OcUXv3wgMOdU2Q5PfJeVzuf4KJvyhVvGu/xuChutEvA=; b=f5IUu4AmUpWlYjXjyjCPV18y3bz8hsOUcaA3KQ5rmkAGxJWwlp7iHm/ntkNPwvwKnK BhkmvAhXdkv3aqcQe9z9gEXp8R4TgHW4iqkPVVGPp8i5rMRprjkXawKxtrUUZFtCUh40 0cE/L1q5zvVgdCccmc1CM4789Qb7xlV2Z3cjczgZ48h7vUERHYD3+tPInvfaNE/VMX6B ztjeJDDR5243/aLfcTFWT6MiKK+apwiBP6re586QLmhJvtJqCEM5flhsTrdEgCyDi1h0 My3Oa8VfrUNCWsTV1xW4/5sfzeqq/WaYSsHusITKsXYof57moUdoDNTT+9Z/EN/0wt3X wBHw== X-Forwarded-Encrypted: i=1; AKwUvBwxncSr8wRSKCC7AiOki4EQYicdk1eXrUGNkp8Fg/eGjtaJyujl+RpOKrnJKXTpuU/I7/D08ovEXwBEN4U=@vger.kernel.org X-Gm-Message-State: AFq9FYLCpJ7EK48kLpaGgaNn1Wk8r7FRLcBrdSzq4bHmPw+scWU3jY00 dP4ZPUoyjukcQaRiLx2p++VajEZfDZrwIkPK17drdWR7fRTCNjHaJhvW X-Gm-Gg: AYBFou2jfCiSew4QFAJhL/HfLL8Q06//ERjEUr3LYMV8+2FpE+cO6/CXD+9wkG/wX32 qW/mNeGvec/RwFqSmxIvjFftI0ow8gar0uzNZiKkLQOSpIQ+1niBIpjV1OvwcCcljCrXo7NOmLk ItnHBz2wpMoKzBFjwKoVFiP5rQ3WpbF4M2VItT8LmmI/d/2QNo3I8Jao8JeM9QqRFBYOXxzomg7 N7gfEw8OpR1O0w2hdV63Bh7iKbl36VAbksIlI69iySsYEZ860O3mGF5GxE9hm+wJDcXDEud8Z/k ttSLuhxE9ufcqeWQ9fDaB+K1G+/07byR7PTF8iEkEGrfqZMPS0SwqqavT8/G29jajmtb4wp1GLc jCqnGHMIWc8Gb5oYFL3dQwDOvvEy8JB4CcZkCHFiS0x8FVavhi3JGkicwNnyzstjqHfPmrK1S3n nKaGqwl1r2XKTFS6A0Jz/xJYw4TCwTtXue9YRNN9F9cxbtT4WWmrwmEMEeFuORD6f+qb1AMqr2r yM8PhXHk8hwqyy3u1jvL/2kJm/zczeVZ71SJ6Z9OrbdL17Z/vSa4S3X1A+tHQ== X-Received: by 2002:adf:e19c:0:b0:487:11c3:b004 with SMTP id ffacd0b85a97d-488719760c4mr23422853f8f.52.1790602368298; Mon, 28 Sep 2026 06:32:48 -0700 (PDT) Received: from 127.mynet ([2a01:4b00:bd21:4f00:7cc6:d3ca:494:116c]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4887a349731sm28090164f8f.12.2026.09.28.06.32.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2026 06:32:47 -0700 (PDT) From: Pavel Begunkov To: linux-block@vger.kernel.org Cc: asml.silence@gmail.com, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-nvme@lists.infradead.org, linux-fsdevel@vger.kernel.org, io-uring@vger.kernel.org, Christoph Hellwig , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Keith Busch , Sagi Grimberg , Alexander Viro , Christian Brauner , Jan Kara , Andrew Morton , Jens Axboe , Nitesh Shetty , Kanchan Joshi , Anuj Gupta , Tushar Gohad , William Power , Phil Cayton , Matthew Brost , Alasdair Kergon , Mike Snitzer , Mikulas Patocka , Benjamin Marzinski , dm-devel@lists.linux.dev Subject: [PATCH v7 03/13] block: always adjust bi_offset on bio_advance_iter Date: Mon, 28 Sep 2026 14:32:19 +0100 Message-ID: <39b4cb178513b33726c8d2ee0824f7324e84828e.1790602223.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Extend bio_no_advance_iter() iteration to also increment the offset. It's not currently needed because the currently listed request types don't have a buffer, but will be useful once we add bios backed by dma-buf, which don't have a bvec but work with a single range. Suggested-by: Christoph Hellwig Reviewed-by: Christoph Hellwig Signed-off-by: Pavel Begunkov --- drivers/md/dm-io-rewind.c | 6 ++++-- include/linux/bio.h | 12 ++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/drivers/md/dm-io-rewind.c b/drivers/md/dm-io-rewind.c index 04f3fc8aeb6f..b22719c6411c 100644 --- a/drivers/md/dm-io-rewind.c +++ b/drivers/md/dm-io-rewind.c @@ -113,10 +113,12 @@ static inline void dm_bio_rewind_iter(const struct bi= o *bio, iter->bi_sector -=3D bytes >> 9; =20 /* No advance means no rewind */ - if (bio_no_advance_iter(bio)) + if (bio_no_advance_iter(bio)) { iter->bi_size +=3D bytes; - else + iter->bi_offset -=3D bytes; + } else { dm_bvec_iter_rewind(bio->bi_io_vec, iter, bytes); + } } =20 /** diff --git a/include/linux/bio.h b/include/linux/bio.h index 75f8086351c0..10ece05cbf43 100644 --- a/include/linux/bio.h +++ b/include/linux/bio.h @@ -113,11 +113,13 @@ static inline void bio_advance_iter(const struct bio = *bio, { iter->bi_sector +=3D bytes >> 9; =20 - if (bio_no_advance_iter(bio)) + if (bio_no_advance_iter(bio)) { iter->bi_size -=3D bytes; - else + iter->bi_offset +=3D bytes; + } else { bvec_iter_advance(bio->bi_io_vec, iter, bytes); /* TODO: It is reasonable to complete bio with error here. */ + } } =20 /* @bytes should be less or equal to bvec[i->bi_idx].bv_len */ @@ -127,10 +129,12 @@ static inline void bio_advance_iter_single(const stru= ct bio *bio, { iter->bi_sector +=3D bytes >> 9; =20 - if (bio_no_advance_iter(bio)) + if (bio_no_advance_iter(bio)) { iter->bi_size -=3D bytes; - else + iter->bi_offset +=3D bytes; + } else { bvec_iter_advance_single(bio->bi_io_vec, iter, bytes); + } } =20 void __bio_advance(struct bio *, unsigned bytes); --=20 2.54.0 From nobody Sat Oct 3 12:03:09 2026 Received: from mail-wm2-f12.google.com (mail-wm2-f12.google.com [74.125.225.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E3EC34CA766 for ; Mon, 28 Sep 2026 13:32:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.225.140 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602376; cv=none; b=erJ+/D0klscj41zqdlLLPgfdaGTmx7IIWtQ42xFYlU+EbqHz5wClRA6n9UoNSnfm9q25Ve+bVyjUArEgAISMi+8KhERSVeuO0Czwr4Suthcw4niBhXefDdSqjXC0Wd+H4RF2XoGC6K5c6hwKaJG4lt592X0fQVHqv0tK0e0DNP0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602376; c=relaxed/simple; bh=j3pz2Fuj7nVIXtfL0+9vjgtsmZcjqUTN9QEUrQrBWDg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QgWYyYMifg+3qK4ZI3zvn/XKHzmdZzckitnqyGPpkGrNvS5r4E0/Aux3os6eX0IVyIyjTAweiHPQuQ26MqjIL0nEPbZbnsSJp80hCDVzFoKuOgbOTgN7+5KC5aFayJuwuJ9qzi4R2Kl34gKvi8sO6LoAlWGQZNjGIm3STSfkS5E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ag9IjeCu; arc=none smtp.client-ip=74.125.225.140 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ag9IjeCu" Received: by mail-wm2-f12.google.com with SMTP id 5b1f17b1804b1-49cd5462b69so17322425e9.1 for ; Mon, 28 Sep 2026 06:32:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790602370; x=1791207170; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=AhzjnL4sENHY8mgNL3frpr16ZkLf9hho77d9PMQT7Tw=; b=ag9IjeCubuprHnnqyP5893NVc+/PUQW74Zai/30aZ/9OUzXf1CefjGzQ9Byrx+5DDt mGquFhxdMshBwP2bZJ7VUX2kMkYhe84H6h0tWFh6py8I2tjfBRJMcHF6u4jwPvvHG83k teoZvGPZaAp0mbzSNePkHL+dBDtha/6JCH1Y3i/f+szI0Ta47dSSK2ftCkkG/vd5ylRv yP/Ju/rTpAS5j41EbjTv8jGGE77G4KLbodz/FpPJlH01KyLERp2XsxZc5UjQF1Sv9hxJ y/fVeqKG6iZrqiXEj09CeHdtEEdF9h66HB4mXDPYhNrnAUp5y9nHHx+E2ZPrNhVzqHX9 r9hg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790602370; x=1791207170; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=AhzjnL4sENHY8mgNL3frpr16ZkLf9hho77d9PMQT7Tw=; b=Kg0YUkuMVU1QGWjJ2BVQbiU1GXOmoSEQFBZSWQho+cyNx8Qkzh/pRyH27FaKnC9Ala qDl4qS6MrJiRSiDnkRiN9JJoxHxYnjNtEfaSN89mRRxX0p/TufVc3XI7rbCDXcSZDssz R3RE2+O+cAMsvbMFAOLz55fL+LbuQhbVs0ZH09R4lI3/He5Fnij9TltLw8BVIByzP/1u s5UUhFDdHHQ1AfCKZbP9rsqp4r+dMZuCcUZOW4bweQxOpMO4abaFBOkAirWB/kByMVpY YN6kN27dLovgnLkbVylqWzV5Ra1GpimOVxgo++7o7pZx6eo1wMhFU3QOokTXjG+XWwSV xh+Q== X-Forwarded-Encrypted: i=1; AKwUvBwvA6GzHV17MzmrLFAabUj3/7TGFhParavgfQsZALqPFXRsl4S43PFCtUPzLwCXe5HWuIT+wCZZ8UIAnzg=@vger.kernel.org X-Gm-Message-State: AFuF++moZHK1TiH/2rndC79nrD0jxUsigYHKWWW4sS58aU6mER98eNTn suyIaIBvEd05szmlphLSa9JqVk4IiBqXZCdcPe4LL+l7ABHs0IaKZsHF X-Gm-Gg: AYBFou0GBaG392PZVm11j+ptJUmzHBipKowAIjIvaVFM7orTypz0FxuPTxgHvMUAOoh ZnAqHIrYJab+++rLeugb3N3FUia2g7UtlIZyXAf1dOUDMmixJj5KfjHksmz/IquUp+c73gB24N5 R938vvppcfrOszaB2H7C60IFFkA0yIQOPId7HnuTMgS7jXKeX1tcm3BL8QxxA7ZAw7aRiIIsI9H gKihM8zd1wsJUD+ARsWTDififl7TVM3srf2N7r4JpcO21gXF1JxDvCcrIB7nhQU95+CAwAWW6FQ L+yCS+UQBSQsSKVPel5GhPCpgmBgtHAEi/Or1U9/74l1K+mHutzSOcILGI4cfH+tCFFB7sKKugk EDNB9Ht5r53j60Bf+Pl/C8Np7Kq6c0JgEEgEal2TrDGCnaKaavuT9FZoegdOLgu4O/fdqNFFov5 08Uqr7ffEo/P+lq5qJC/kbyoSlRcNWoJEAbhQf6uET15akkWnUmlHWtYf6kaiQZd9WlRMxMKsYW MSSR0UEtdvtWn5ZeulnIsRcaZItVUvgh6UelLY3NILCd7P9oqj98IdavHwilL9WTD+V4xiBZhvQ X-Received: by 2002:a05:600c:4709:b0:49f:fbad:29c1 with SMTP id 5b1f17b1804b1-49ffbad2d61mr102147255e9.33.1790602369885; Mon, 28 Sep 2026 06:32:49 -0700 (PDT) Received: from 127.mynet ([2a01:4b00:bd21:4f00:7cc6:d3ca:494:116c]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4887a349731sm28090164f8f.12.2026.09.28.06.32.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2026 06:32:49 -0700 (PDT) From: Pavel Begunkov To: linux-block@vger.kernel.org Cc: asml.silence@gmail.com, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-nvme@lists.infradead.org, linux-fsdevel@vger.kernel.org, io-uring@vger.kernel.org, Christoph Hellwig , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Keith Busch , Sagi Grimberg , Alexander Viro , Christian Brauner , Jan Kara , Andrew Morton , Jens Axboe , Nitesh Shetty , Kanchan Joshi , Anuj Gupta , Tushar Gohad , William Power , Phil Cayton , Matthew Brost , Alasdair Kergon , Mike Snitzer , Mikulas Patocka , Benjamin Marzinski , dm-devel@lists.linux.dev Subject: [PATCH v7 04/13] block: introduce dma map backed bio type Date: Mon, 28 Sep 2026 14:32:20 +0100 Message-ID: <6fe0d7c85c2f981c5caab3d1532b6c73c604cdb1.1790602223.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Premapped buffers don't require a generic bio_vec since these have already been dma mapped. Repurpose the bi_io_vec space to store dmabuf maps as they are mutually exclusive. The bio splitting differs from the normal path because it's already pre-mapped and for the block layer it's just an offset into the dma-buf. The actual segmentation is only available to the importer driver and not the block layer, however, it doesn't contain alignment gaps and we don't have to check it. For the same reason we can't precisely split by the number of segments, but we use ->min_seg_shift stored in the map to calculate the minimum number of bytes a request consisting of lim->max_segments full segments can cover and split by that. It's stricter and can add extra splitting. E.g. for a {4K, 4G} segmentation, the min segment size is 4K, and we'll split it into bios of (4K * lim->max_segments) bytes each, but it should be good enough for now to cover the most popular use cases. Suggested-by: Keith Busch Signed-off-by: Pavel Begunkov --- block/bio.c | 15 ++++++++++-- block/blk-merge.c | 50 +++++++++++++++++++++++++++++++++++++++ block/fops.c | 2 +- include/linux/bio.h | 9 +++---- include/linux/blk-mq.h | 7 ++++++ include/linux/blk_types.h | 14 ++++++++++- include/linux/bvec.h | 3 ++- 7 files changed, 91 insertions(+), 9 deletions(-) diff --git a/block/bio.c b/block/bio.c index b48091c7663f..1e0d9714c541 100644 --- a/block/bio.c +++ b/block/bio.c @@ -881,7 +881,11 @@ static int __bio_clone(struct bio *bio, struct bio *bi= o_src, gfp_t gfp) bio->bi_write_stream =3D bio_src->bi_write_stream; bio->bi_bvec_gap_bit =3D bio_src->bi_bvec_gap_bit; bio->bi_iter =3D bio_src->bi_iter; - bio->bi_io_vec =3D bio_src->bi_io_vec; + + if (op_is_dmabuf(bio->bi_opf)) + bio->bi_dmabuf_map =3D bio_src->bi_dmabuf_map; + else + bio->bi_io_vec =3D bio_src->bi_io_vec; =20 if (bio->bi_bdev) { if (bio->bi_bdev =3D=3D bio_src->bi_bdev && @@ -1204,16 +1208,23 @@ EXPORT_SYMBOL_GPL(__bio_release_pages); =20 bool bio_iov_iter_set(struct bio *bio, const struct iov_iter *iter) { - if (!iov_iter_is_bvec(iter)) + if (!iov_iter_is_bvec(iter) && !iov_iter_is_dmabuf_map(iter)) return false; =20 WARN_ON_ONCE(bio->bi_max_vecs); =20 + static_assert(offsetof(struct bio, bi_io_vec) =3D=3D + offsetof(struct bio, bi_dmabuf_map)); + static_assert(offsetof(struct iov_iter, bvec) =3D=3D + offsetof(struct iov_iter, dmabuf_map)); + bio->bi_io_vec =3D (struct bio_vec *)iter->bvec; bio->bi_iter.bi_idx =3D 0; bio->bi_iter.bi_offset =3D iter->iov_offset; bio->bi_iter.bi_size =3D iov_iter_count(iter); bio_set_flag(bio, BIO_CLONED); + if (iov_iter_is_dmabuf_map(iter)) + bio->bi_opf |=3D REQ_NOMERGE | REQ_DMABUF; return true; } =20 diff --git a/block/blk-merge.c b/block/blk-merge.c index 258a726071d1..18f014ff3314 100644 --- a/block/blk-merge.c +++ b/block/blk-merge.c @@ -9,6 +9,7 @@ #include #include #include +#include =20 #include =20 @@ -319,6 +320,41 @@ static inline unsigned int bvec_seg_gap(struct bio_vec= *bvprv, return bv->bv_offset | (bvprv->bv_offset + bvprv->bv_len); } =20 +static inline int bio_split_io_at_dmabuf(struct bio *bio, + const struct queue_limits *lim, unsigned *segs, + unsigned max_bytes, unsigned len_align_mask, + unsigned start_align_mask) +{ + unsigned bytes =3D min(bio->bi_iter.bi_size, max_bytes); + unsigned seg_shift =3D bio->bi_dmabuf_map->min_seg_shift; + unsigned offset =3D bio->bi_iter.bi_offset & ((1U << seg_shift) - 1); + u64 max_segs_bytes; + + /* + * dma-buf maps don't expose the underlying segmentation, but they're + * guaranteed to not have alignment gaps, we only need to check the + * start and length alignment. + */ + if ((bio->bi_iter.bi_offset & start_align_mask) || + (bio->bi_iter.bi_size & len_align_mask)) + return -EINVAL; + + /* Presented as a single contiguous range into the dma-buf */ + *segs =3D 1; + + /* + * Limit by the number of segments by using the minimal segment size. + * Any I/O consisting of N full segments should be able to cover at + * least N multiplied by the segment size. It's stricter than walking + * the segments and might cause extra splitting. + */ + max_segs_bytes =3D (u64)lim->max_segments << seg_shift; + bytes =3D min_t(u64, bytes, max_segs_bytes - offset); + if (bytes !=3D bio->bi_iter.bi_size) + return bytes; + return 0; +} + /** * bio_split_io_at - check if and where to split a bio * @bio: [in] bio to be split @@ -346,6 +382,19 @@ int bio_split_io_at(struct bio *bio, const struct queu= e_limits *lim, len_align_mask |=3D (bc->bc_key->crypto_cfg.data_unit_size - 1); } =20 + if (op_is_dmabuf(bio->bi_opf)) { + int ret; + + ret =3D bio_split_io_at_dmabuf(bio, lim, &nsegs, max_bytes, + len_align_mask, start_align_mask); + if (ret < 0) + return ret; + if (!ret) + goto out; + bytes =3D ret; + goto split; + } + bio_for_each_bvec(bv, bio, iter) { if (bv.bv_offset & start_align_mask || bv.bv_len & len_align_mask) @@ -376,6 +425,7 @@ int bio_split_io_at(struct bio *bio, const struct queue= _limits *lim, bvprvp =3D &bvprv; } =20 +out: *segs =3D nsegs; bio->bi_bvec_gap_bit =3D ffs(gaps); return 0; diff --git a/block/fops.c b/block/fops.c index 4d1ab0311b4d..641621db700f 100644 --- a/block/fops.c +++ b/block/fops.c @@ -363,7 +363,7 @@ static ssize_t __blkdev_direct_IO_async(struct kiocb *i= ocb, * Users don't rely on the iterator being in any particular * state for async I/O returning -EIOCBQUEUED, hence we can * avoid expensive iov_iter_advance(). Bypass - * bio_iov_iter_get_pages() and set the bvec directly. + * bio_iov_iter_get_pages() and set the bvec/dmabuf directly. */ if (!bio_iov_iter_set(bio, iter)) { ret =3D blkdev_iov_iter_get_pages(bio, iter, bdev); diff --git a/include/linux/bio.h b/include/linux/bio.h index 10ece05cbf43..47aef63dba3b 100644 --- a/include/linux/bio.h +++ b/include/linux/bio.h @@ -80,7 +80,8 @@ static inline bool bio_no_advance_iter(const struct bio *= bio) { return bio_op(bio) =3D=3D REQ_OP_DISCARD || bio_op(bio) =3D=3D REQ_OP_SECURE_ERASE || - bio_op(bio) =3D=3D REQ_OP_WRITE_ZEROES; + bio_op(bio) =3D=3D REQ_OP_WRITE_ZEROES || + op_is_dmabuf(bio->bi_opf); } =20 static inline void *bio_data(struct bio *bio) @@ -444,12 +445,12 @@ static inline void bio_wouldblock_error(struct bio *b= io) =20 /* * Calculate number of bvec segments that should be allocated to fit data - * pointed by @iter. If @iter is backed by bvec it's going to be reused - * instead of allocating a new one. + * pointed by @iter. If @iter is backed by a bvec or a dmabuf, the bvec ar= ray / + * the dma map are going to be reused, and so no extra allocation is requi= red. */ static inline int bio_iov_vecs_to_alloc(struct iov_iter *iter, int max_seg= s) { - if (iov_iter_is_bvec(iter)) + if (iov_iter_is_bvec(iter) || iov_iter_is_dmabuf_map(iter)) return 0; return iov_iter_npages(iter, max_segs); } diff --git a/include/linux/blk-mq.h b/include/linux/blk-mq.h index af878597afb8..7c7504c84e09 100644 --- a/include/linux/blk-mq.h +++ b/include/linux/blk-mq.h @@ -1017,6 +1017,13 @@ static inline void *blk_mq_rq_to_pdu(struct request = *rq) return rq + 1; } =20 +static inline bool blk_mq_rq_is_dmabuf(struct request *rq) +{ + if (!IS_ENABLED(CONFIG_DMA_SHARED_BUFFER)) + return false; + return rq->bio && op_is_dmabuf(rq->bio->bi_opf); +} + static inline struct blk_mq_hw_ctx *queue_hctx(struct request_queue *q, in= t id) { struct blk_mq_hw_ctx *hctx; diff --git a/include/linux/blk_types.h b/include/linux/blk_types.h index 98e21b4cbf32..0cc09b975d8f 100644 --- a/include/linux/blk_types.h +++ b/include/linux/blk_types.h @@ -233,7 +233,12 @@ struct bio { atomic_t __bi_remaining; =20 /* The actual vec list, preserved by bio_reset() */ - struct bio_vec *bi_io_vec; + union { + struct bio_vec *bi_io_vec; + /* Driver specific dma map, valid IFF REQ_DMABUF is set */ + struct dma_buf_io_map *bi_dmabuf_map; + }; + struct bvec_iter bi_iter; =20 union { @@ -402,6 +407,7 @@ enum req_flag_bits { __REQ_DRV, /* for driver use */ __REQ_FS_PRIVATE, /* for file system (submitter) use */ __REQ_ATOMIC, /* for atomic write operations */ + __REQ_DMABUF, /* Using premmaped dma buffers */ /* * Command specific flags, keep last: */ @@ -434,6 +440,7 @@ enum req_flag_bits { #define REQ_DRV (__force blk_opf_t)(1ULL << __REQ_DRV) #define REQ_FS_PRIVATE (__force blk_opf_t)(1ULL << __REQ_FS_PRIVATE) #define REQ_ATOMIC (__force blk_opf_t)(1ULL << __REQ_ATOMIC) +#define REQ_DMABUF (__force blk_opf_t)(1ULL << __REQ_DMABUF) =20 #define REQ_NOUNMAP (__force blk_opf_t)(1ULL << __REQ_NOUNMAP) =20 @@ -487,6 +494,11 @@ static inline bool op_is_discard(blk_opf_t op) return (op & REQ_OP_MASK) =3D=3D REQ_OP_DISCARD; } =20 +static inline bool op_is_dmabuf(blk_opf_t op) +{ + return op & REQ_DMABUF; +} + /* * Check if a bio or request operation is a zone management operation. */ diff --git a/include/linux/bvec.h b/include/linux/bvec.h index fc566ee1c1ff..b63914ff56e3 100644 --- a/include/linux/bvec.h +++ b/include/linux/bvec.h @@ -108,7 +108,8 @@ struct bvec_iter { unsigned int bi_idx; =20 /* - * Current offset in the bvec entry pointed to by `bi_idx`. + * Current offset in the bvec entry pointed to by `bi_idx` or into + * a dma-buf map. */ unsigned int bi_offset; } __packed __aligned(4); --=20 2.54.0 From nobody Sat Oct 3 12:03:09 2026 Received: from mail-wr2-f12.google.com (mail-wr2-f12.google.com [74.125.225.76]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 976684CC600 for ; Mon, 28 Sep 2026 13:32:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.225.76 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602377; cv=none; b=JF4saIygCH4+1+Yw9L5GDE2qOA93o0l81NVqxZscaGZ6tfdSW6ImPThhKbF8p++MyNm7B4giqwmo35xGRbXLHJ9hTOAN/Pc81iNbPC0MEFlMyL2USzUHsmem3Hvt89VZpN7bCVsNhesdna3raGmjBJz748u2XuT8YrYys/1FBmc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602377; c=relaxed/simple; bh=s/pMlWhML39X7WPnWJGpXvJM/zWMeJLTmEHcD+dWxB8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=RRNxE3c0XzbdA12OtSrEECFXrgGo9MJMrY2mmN3lj17ascXm8KMbqDYL266DJP5uTZURf5tFdZ1D+3kDKbrPr8ZfIp8/rW5Y1A4zvPfUJv5baWW2VxAt0vga0PW6X+gn8MlfhQu5lr3PyLllhX8oFajon3yREw8AhOqqS80nlJI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=RfkaR0Pm; arc=none smtp.client-ip=74.125.225.76 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="RfkaR0Pm" Received: by mail-wr2-f12.google.com with SMTP id ffacd0b85a97d-482f635552aso2403496f8f.2 for ; Mon, 28 Sep 2026 06:32:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790602372; x=1791207172; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=zNN8LK2yF6e2Er4Otjr5e2ZaUKzvWbebPH7D4oJybmc=; b=RfkaR0PmmRqpWxFhMtgRZdKnj7xeumGmGfDTxzfQ6oWWJsLbke7bhO4MBPzg+BSoLo 4hggbNPW70ddSMca21lxO/l0oyAi2RycEF0NAV5bQm80Z3aBiU7Vb8WA0ccxK5dE8Z6L ZXjzu6TBl38tpDGG0EQy5wyV/7F+kmOce+ZVUtW/gl5o+ZsDXVzrTSk2hNKcDR9jDeG+ HwgOkSN3yzuJ956pHO9pqRl7KZ77zPaGcoNzWgtpX6FZ+oiyCqVBYWn1mmGyUKi4bKr4 U5O3ppeWrwrCB2Z+3wRCBpCsGZTy4Mk1L1AiK4C8w4XRdpi4avlhnDngcWkm3ypM3gJR JFfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790602372; x=1791207172; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=zNN8LK2yF6e2Er4Otjr5e2ZaUKzvWbebPH7D4oJybmc=; b=wCOuReXrKoTboTO9Nm9NybbzUl4z719dn3fmi3KVV+EjgOMMQM3KAtcY7wKt4fryBG 0jjhd0uh6Bn3UbWoFLRdcVW80NdmiKp5NEd3uy+BrFORPEaXJ6MtT+YKPjmYHvMRpkFB hDQsO7O5gCzcG+sobm5FynNY0etoaGjVnDf/PncebKG/+4mMIrMlUfEqISM+tfz79G8u YIbDECKSysXlQkcSEU6QJ/oFQoF3NxffQJK5bh284pY/79y+ICImPzgT+ps0oISA7+E9 tsVIG1Zqa8RlKqB0jdPj8SkQl/DPj4FT2+elSgLJ9v+qrYETJiFUQD/mLFVHYFvKvzlp odEw== X-Forwarded-Encrypted: i=1; AKwUvBzrF55o83jVKzEbLXpmjOQBHjAEyS8fHUw8blyDMI551+nU7wUcQskXg+f/jQiFEEhP1zBTCPsUc4a4Pf8=@vger.kernel.org X-Gm-Message-State: AFq9FYKpvnfJxlgxNZdBv2o2iRtU7mlU/C88G9x7/52WB5lorDUT4fb3 8q91gdVlQ0pfVWoAOgy2pQWt5oicgyyWMXmXzia0qN1Zupbp1bRXdFe5 X-Gm-Gg: AYBFou37ifrEfzcMFDzYqbeYesU04npFs8kNE47ew8BMtq3ITtO2R0MUbaj04lh6gL3 kce7Ysmgn36F/8DIvN6wRZN0NrGJR4jsxd8KAcN6/FAQ6MCFUIxZGJ15ThhvS62S/Yswj2Jxu7k mSKOpoRqnGw+ubudpB1v9Nkr8u4sPvQcngVzVVelFCguXTxoTLOkBOjmiOGVTzoNjMhLXify2gs Y1bWmcBBb7/EiVX2l5UbOJvNME2N4ktGcr09/vW8A9MuRtcHkGpVtmA8B9+f4NXT6p4Sxox7kfB VJBAmjtOPWZQ8Ttcjyh4CZXy8etFAw7sZ0xZCprXZKZvpbUqsqzdXVXt5SKuVdzNOxT9weAzBSA 6M5tutWtO5qDKmn9KfJnBryawu1FXNXPLg8qYzux21kpFsCedXF35FsJmx+qbR9HRtW4kJ7b3V1 C/aEgfO1rRz4ex+s1bG/boEmm6jhTTmAvZIN42XBMlq7mQ3crMRSsi5eiZunITD0HHJSt7bJPi8 qS9a7a92JlVpfzow1Ex6J70ZdaF9o8UDPoVA4VszKl3THnPG6x8SbIVH4OPDFL0Tb8VURBIw6Wp X-Received: by 2002:a05:6000:4911:b0:486:f99f:8236 with SMTP id ffacd0b85a97d-48872ac74e0mr24072487f8f.44.1790602371877; Mon, 28 Sep 2026 06:32:51 -0700 (PDT) Received: from 127.mynet ([2a01:4b00:bd21:4f00:7cc6:d3ca:494:116c]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4887a349731sm28090164f8f.12.2026.09.28.06.32.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2026 06:32:50 -0700 (PDT) From: Pavel Begunkov To: linux-block@vger.kernel.org Cc: asml.silence@gmail.com, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-nvme@lists.infradead.org, linux-fsdevel@vger.kernel.org, io-uring@vger.kernel.org, Christoph Hellwig , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Keith Busch , Sagi Grimberg , Alexander Viro , Christian Brauner , Jan Kara , Andrew Morton , Jens Axboe , Nitesh Shetty , Kanchan Joshi , Anuj Gupta , Tushar Gohad , William Power , Phil Cayton , Matthew Brost , Alasdair Kergon , Mike Snitzer , Mikulas Patocka , Benjamin Marzinski , dm-devel@lists.linux.dev Subject: [PATCH v7 05/13] block: add dma-buf support for raw bdev Date: Mon, 28 Sep 2026 14:32:21 +0100 Message-ID: X-Mailer: git-send-email 2.54.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a simple proxy implementation of init_dma_buf_io_ctx() forwarding the call to a new struct block_device_operations operation. Also reject dma-buf backed iterators for buffered IO. Reviewed-by: Christoph Hellwig [pavel: reject dma-buf without O_DIRECT] Signed-off-by: Pavel Begunkov --- block/fops.c | 25 ++++++++++++++++++++++++- include/linux/blkdev.h | 2 ++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/block/fops.c b/block/fops.c index 641621db700f..d05a78d0919a 100644 --- a/block/fops.c +++ b/block/fops.c @@ -783,7 +783,8 @@ static ssize_t blkdev_write_iter(struct kiocb *iocb, st= ruct iov_iter *from) =20 if (iocb->ki_flags & IOCB_DIRECT) { ret =3D blkdev_direct_write(iocb, from); - if (ret >=3D 0 && iov_iter_count(from)) { + if (ret >=3D 0 && iov_iter_count(from) && + !iov_iter_is_dmabuf_map(from)) { ret =3D direct_write_fallback(iocb, from, ret, blkdev_buffered_write(iocb, from)); need_sync =3D true; @@ -796,6 +797,9 @@ static ssize_t blkdev_write_iter(struct kiocb *iocb, st= ruct iov_iter *from) need_sync =3D true; } } else { + if (unlikely(iov_iter_is_dmabuf_map(from))) + return -EOPNOTSUPP; + /* * Take i_rwsem and invalidate_lock to avoid racing with * set_blocksize changing i_blkbits/folio order and punching @@ -851,6 +855,11 @@ static ssize_t blkdev_read_iter(struct kiocb *iocb, st= ruct iov_iter *to) if (ret < 0 || !count) goto reexpand; } + if (unlikely(iov_iter_is_dmabuf_map(to))) { + if (!ret) + ret =3D -EOPNOTSUPP; + goto reexpand; + } =20 /* * Take i_rwsem and invalidate_lock to avoid racing with set_blocksize @@ -954,6 +963,19 @@ static int blkdev_mmap_prepare(struct vm_area_desc *de= sc) return generic_file_mmap_prepare(desc); } =20 +static int blkdev_init_dma_buf_io_ctx(struct file *file, + struct dma_buf_io_ctx *ctx) +{ + struct block_device *bdev =3D file_bdev(file); + struct gendisk *disk =3D bdev->bd_disk; + + if (!(file->f_flags & O_DIRECT)) + return -EINVAL; + if (!disk->fops->init_dma_buf_io_ctx) + return -EOPNOTSUPP; + return disk->fops->init_dma_buf_io_ctx(bdev, ctx); +} + const struct file_operations def_blk_fops =3D { .open =3D blkdev_open, .release =3D blkdev_release, @@ -972,6 +994,7 @@ const struct file_operations def_blk_fops =3D { .fallocate =3D blkdev_fallocate, .uring_cmd =3D blkdev_uring_cmd, .fop_flags =3D FOP_BUFFER_RASYNC | FOP_DONTCACHE, + .init_dma_buf_io_ctx =3D blkdev_init_dma_buf_io_ctx, }; =20 static __init int blkdev_init(void) diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h index d003a9d2d1f6..2d5c30823aab 100644 --- a/include/linux/blkdev.h +++ b/include/linux/blkdev.h @@ -1599,6 +1599,8 @@ struct block_device_operations { /* returns the length of the identifier or a negative errno: */ int (*get_unique_id)(struct gendisk *disk, u8 id[16], enum blk_unique_id id_type); + int (*init_dma_buf_io_ctx)(struct block_device *, + struct dma_buf_io_ctx *); struct module *owner; const struct pr_ops *pr_ops; =20 --=20 2.54.0 From nobody Sat Oct 3 12:03:09 2026 Received: from mail-wr2-f38.google.com (mail-wr2-f38.google.com [74.125.225.102]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 70EF04CDDD7 for ; Mon, 28 Sep 2026 13:32:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.225.102 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602383; cv=none; b=tLf1hDcnmlWD0HhAI9+CmS0J0miHaPFBhCreZWyeu+tkyX8C/cm5vWh7tLpbls8DLxRzcHu1CWlHeniBZFJG+tBt9YTL3aCbKHLWxPkJMNpMFZr+/ZgTpA/C6SXG0Qxjb8CunO4vIgHFx0eM7nXFCccrBhB6Gweq4j4utUJHuPs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602383; c=relaxed/simple; bh=04/EubsUzuOGVVBdV5+op4CBOIm0uE7Wxal5AldmltA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=c9cp+1shLQ+0LTaTILx06+GlVgtuNuwQXIQfPyz4bJZpAZ94V+RU4g4c3tKzQU4Kw98rMggaaPtks9CS2GsPjD8KiU5L0vDdbpz5tywYrqZi5mCrbJk7axCJAtCADmiZxerLMKxdC/hTa/4nbZfRqHk6jrh1Z7MtvDY5UY8K2TU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=asvJ6Ngl; arc=none smtp.client-ip=74.125.225.102 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="asvJ6Ngl" Received: by mail-wr2-f38.google.com with SMTP id ffacd0b85a97d-482f6350f89so1924307f8f.3 for ; Mon, 28 Sep 2026 06:32:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790602373; x=1791207173; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=cVsh0B/AKhKUiV66cIH1/p0W39OWw+1tNkq32iVCj/M=; b=asvJ6Nglq1nuLmjIhCS/wtp6gK3acxVKIXOG7Cfyt3b1B7f4oRb1ELgO4wH3ce1cW6 oSWZ5BTVP3jAusWKqB0uYApMQ/G5StH+RgHTuG6GtGS0s57V0wDTILCa/ZiPIGM/hro0 7tSqWgXsdPfFPppOawZ9fTmakyP8NstQLY7q4ScOsIWfnV0iwNhaU/mS3fiN3bjMkN29 OsC6aclhDGIqQ/gdRYoch6jdJuzCYqmx9z96dDZ8vg+bge0VrLGF8DlxxuW5fH9s24Bw 5CcWilAuJeWn90fvQ/xBXpYm5U9SEWatAKx5yWSes1PckLFpjU6cwJtb4Hw51AAMOQrL OCHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790602373; x=1791207173; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=cVsh0B/AKhKUiV66cIH1/p0W39OWw+1tNkq32iVCj/M=; b=vcCqnykv5S4Ea07VHzw/2AI8pXYhYB2vWAo54vyilD+mSep40suQjgu+Az8KOiLmbc RPaVH7SEiwNrxyWbnQQBiY4sqKXSVgRu6D7l3Pik3VzsDFLH7HiJPXs7NREYwu86LmY1 24lzEoqSSLHlkZPGnMcDs/yIeZqoXc3szH8Mz7jBk+ekQsfGh1pNVQ+s1IT3TNqqqPgX hWqwLO3UDLfJzyUXPpWvgtyOsG5O6a3OJvJkd2g9NTKmnNHZGE9DURizAwqnOkwuAQd0 HgZkIXnuNq/MgKCMNy/p7jDiRtIAQGW17pBu+zkjjnQ8iwtuSEt8pB77o7uF8tPWxuFB fnnA== X-Forwarded-Encrypted: i=1; AKwUvBy0HvGogPJe24galEJ3xaw7VA15UF8MnmzfvZJ+IBGsjlKryIcyY8sEsA0JpzgrKyxPw753RRLNMf+n/4c=@vger.kernel.org X-Gm-Message-State: AFq9FYI0g3CAhFNiWBNw9zIiwNSUKEnrzAF3kHQYLmQYlgzYxgIL9Qkb LTEo2Hoif9TIc3sUHHR5n0hSYLdfz23ErHXzJDilFXDv5dFQvefjq+eo X-Gm-Gg: AYBFou2tQ65LLijCJzIw4+E3499rSsvWlBu6UwzHSpNxNk0jLKGUwxRFE6zHw6795P8 L/VqCUp3rKZORXzMFRLrjWlhiu460cYb6U9FWat1/89wIZPeD5DGiCjT3vnrKuO0Z7hWLaAoJXm YRp264WNWLCsYGWozChtvdFSHeBHxp38W0ZNk9jKBCbyYtlaLyxcwgPE/A4p/hwiLMiBhud3ER+ BboXhd7sx6jrgblB/vsD492emNq8fzKktccaEZegrXARY8FnQtbFBj7eN3Lz203G+g6DM2o5vsK 8iwR5Zdubkv6DbhECnw2VnGQJ/BtCy9CwrW17Ko6hxgZ1Yp0YRXZ0jTiNfBFKUNJP0xv2fvCf+G Pr2HbuiJIm4KV2bvKIDAsHwf+99jJJQyxfuZoTj2F02/tlO52FTUNatLnRorTxgrmdJmV0WgoLd 2jHuNTpgJ0YZ85UVR8WZY6UnnpcXxnP82QPqUXwQApntdVd8tIsheAnlhASEb3svwzJlRskaCv7 65YlbvQh//2hS1AJ9gJbhGn/DJhsMn80B/Zm3J0EVe9Ydi3/ugCQGLHDxz2DW7/w6dROzJNbmav 0KNQ3Y1W6nY= X-Received: by 2002:a5d:59c4:0:b0:488:6da0:b83d with SMTP id ffacd0b85a97d-4887db39e6cmr20780234f8f.44.1790602373373; Mon, 28 Sep 2026 06:32:53 -0700 (PDT) Received: from 127.mynet ([2a01:4b00:bd21:4f00:7cc6:d3ca:494:116c]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4887a349731sm28090164f8f.12.2026.09.28.06.32.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2026 06:32:52 -0700 (PDT) From: Pavel Begunkov To: linux-block@vger.kernel.org Cc: asml.silence@gmail.com, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-nvme@lists.infradead.org, linux-fsdevel@vger.kernel.org, io-uring@vger.kernel.org, Christoph Hellwig , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Keith Busch , Sagi Grimberg , Alexander Viro , Christian Brauner , Jan Kara , Andrew Morton , Jens Axboe , Nitesh Shetty , Kanchan Joshi , Anuj Gupta , Tushar Gohad , William Power , Phil Cayton , Matthew Brost , Alasdair Kergon , Mike Snitzer , Mikulas Patocka , Benjamin Marzinski , dm-devel@lists.linux.dev Subject: [PATCH v7 06/13] nvme-pci: implement dma-buf backed requests Date: Mon, 28 Sep 2026 14:32:22 +0100 Message-ID: X-Mailer: git-send-email 2.54.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Enable BIO_DMABUF_MAP backed requests. On registration we map the dma-buf and store it as a prp list, which is then used to initialise requests. All attached contexts are stored in a new list dmabuf_ctxs, and additions/removals are synchronised with dmabuf_lock. Suggested-by: Keith Busch Signed-off-by: Pavel Begunkov --- drivers/nvme/host/core.c | 12 ++ drivers/nvme/host/nvme.h | 2 + drivers/nvme/host/pci.c | 308 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 322 insertions(+) diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c index beea23d04a70..d81462e864a4 100644 --- a/drivers/nvme/host/core.c +++ b/drivers/nvme/host/core.c @@ -2699,6 +2699,17 @@ static int nvme_report_zones(struct gendisk *disk, s= ector_t sector, #define nvme_report_zones NULL #endif /* CONFIG_BLK_DEV_ZONED */ =20 +static int nvme_init_dma_buf_io_ctx(struct block_device *bdev, + struct dma_buf_io_ctx *ctx) +{ + struct nvme_ns *ns =3D bdev->bd_disk->private_data; + struct nvme_ctrl *ctrl =3D ns->ctrl; + + if (!ctrl->ops->init_dma_buf_io_ctx) + return -EINVAL; + return ctrl->ops->init_dma_buf_io_ctx(ctrl, ctx); +} + const struct block_device_operations nvme_bdev_ops =3D { .owner =3D THIS_MODULE, .ioctl =3D nvme_ioctl, @@ -2709,6 +2720,7 @@ const struct block_device_operations nvme_bdev_ops = =3D { .get_unique_id =3D nvme_get_unique_id, .report_zones =3D nvme_report_zones, .pr_ops =3D &nvme_pr_ops, + .init_dma_buf_io_ctx =3D nvme_init_dma_buf_io_ctx, }; =20 static int nvme_wait_ready(struct nvme_ctrl *ctrl, u32 mask, u32 val, diff --git a/drivers/nvme/host/nvme.h b/drivers/nvme/host/nvme.h index 2cff9fcbf740..befb7faba9ca 100644 --- a/drivers/nvme/host/nvme.h +++ b/drivers/nvme/host/nvme.h @@ -667,6 +667,8 @@ struct nvme_ctrl_ops { int (*get_address)(struct nvme_ctrl *ctrl, char *buf, int size); void (*print_device_info)(struct nvme_ctrl *ctrl); bool (*supports_pci_p2pdma)(struct nvme_ctrl *ctrl); + int (*init_dma_buf_io_ctx)(struct nvme_ctrl *ctrl, + struct dma_buf_io_ctx *ctx); unsigned long (*get_virt_boundary)(struct nvme_ctrl *ctrl, bool is_admin); }; =20 diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c index a953c0697f99..e58bdd9a4098 100644 --- a/drivers/nvme/host/pci.c +++ b/drivers/nvme/host/pci.c @@ -27,6 +27,8 @@ #include #include #include +#include +#include =20 #include "trace.h" #include "nvme.h" @@ -318,6 +320,8 @@ struct nvme_dev { bool hmb; struct sg_table *hmb_sgt; mempool_t *dmavec_mempool; + struct list_head dmabuf_ctxs; + struct mutex dmabuf_lock; =20 /* shadow doorbell buffer support: */ __le32 *dbbuf_dbs; @@ -397,6 +401,13 @@ struct nvme_queue { struct completion delete_done; }; =20 +struct nvme_dmabuf_map { + struct dma_buf_io_map base; + struct sg_table *sgt; + unsigned nr_entries; + dma_addr_t dma_list[]; +}; + /* bits for iod->flags */ enum nvme_iod_flags { /* this command has been aborted by the timeout handler */ @@ -865,6 +876,120 @@ static void nvme_free_descriptors(struct request *req) } } =20 +static inline struct nvme_dmabuf_map * +to_nvme_dmabuf_map(struct dma_buf_io_map *map) +{ + return container_of(map, struct nvme_dmabuf_map, base); +} + +static void nvme_dmabuf_map_sync_for_cpu(struct nvme_dev *nvme_dev, + struct request *req) +{ + struct device *dev =3D nvme_dev->dev; + enum dma_data_direction dma_dir; + struct bio *bio =3D req->bio; + struct nvme_dmabuf_map *map =3D to_nvme_dmabuf_map(bio->bi_dmabuf_map); + + dma_dir =3D rq_data_dir(req) =3D=3D READ ? DMA_FROM_DEVICE : DMA_TO_DEVIC= E; + dma_sync_sgtable_for_cpu(dev, map->sgt, dma_dir); +} + +static void nvme_dmabuf_map_sync_for_device(struct nvme_dev *nvme_dev, + struct request *req) +{ + struct device *dev =3D nvme_dev->dev; + enum dma_data_direction dma_dir; + struct bio *bio =3D req->bio; + struct nvme_dmabuf_map *map =3D to_nvme_dmabuf_map(bio->bi_dmabuf_map); + + dma_dir =3D rq_data_dir(req) =3D=3D READ ? DMA_FROM_DEVICE : DMA_TO_DEVIC= E; + dma_sync_sgtable_for_device(dev, map->sgt, dma_dir); +} + +static void nvme_rq_clean_dmabuf_map(struct nvme_dev *dev, + struct request *req) +{ + struct nvme_iod *iod =3D blk_mq_rq_to_pdu(req); + + nvme_dmabuf_map_sync_for_cpu(dev, req); + + if (iod->nr_descriptors) + nvme_free_descriptors(req); +} + +static blk_status_t nvme_rq_setup_dmabuf_map(struct request *req, + struct nvme_queue *nvmeq) +{ + struct nvme_iod *iod =3D blk_mq_rq_to_pdu(req); + struct bio *bio =3D req->bio; + struct nvme_dmabuf_map *map =3D to_nvme_dmabuf_map(bio->bi_dmabuf_map); + unsigned bvec_done =3D bio->bi_iter.bi_offset; + unsigned map_idx =3D bvec_done / NVME_CTRL_PAGE_SIZE; + unsigned offset =3D bvec_done & (NVME_CTRL_PAGE_SIZE - 1); + int length =3D blk_rq_payload_bytes(req) - (NVME_CTRL_PAGE_SIZE - offset); + dma_addr_t *dma_list =3D map->dma_list; + u64 prp1_dma =3D dma_list[map_idx++] + offset; + u64 dma_addr, prp2_dma; + dma_addr_t prp_dma; + __le64 *prp_list; + unsigned i; + + nvme_dmabuf_map_sync_for_device(nvmeq->dev, req); + + if (length <=3D 0) { + prp2_dma =3D 0; + goto done; + } + + if (length <=3D NVME_CTRL_PAGE_SIZE) { + prp2_dma =3D dma_list[map_idx]; + goto done; + } + + if (DIV_ROUND_UP(length, NVME_CTRL_PAGE_SIZE) <=3D + NVME_SMALL_POOL_SIZE / sizeof(__le64)) + iod->flags |=3D IOD_SMALL_DESCRIPTOR; + + prp_list =3D dma_pool_alloc(nvme_dma_pool(nvmeq, iod), GFP_ATOMIC, + &prp_dma); + if (!prp_list) + return BLK_STS_RESOURCE; + + iod->descriptors[iod->nr_descriptors++] =3D prp_list; + prp2_dma =3D prp_dma; + i =3D 0; + for (;;) { + if (i =3D=3D NVME_CTRL_PAGE_SIZE >> 3) { + __le64 *old_prp_list =3D prp_list; + + prp_list =3D dma_pool_alloc(nvmeq->descriptor_pools.large, + GFP_ATOMIC, &prp_dma); + if (!prp_list) + goto free_prps; + iod->descriptors[iod->nr_descriptors++] =3D prp_list; + prp_list[0] =3D old_prp_list[i - 1]; + old_prp_list[i - 1] =3D cpu_to_le64(prp_dma); + i =3D 1; + } + + dma_addr =3D dma_list[map_idx++]; + prp_list[i++] =3D cpu_to_le64(dma_addr); + + length -=3D NVME_CTRL_PAGE_SIZE; + if (length <=3D 0) + break; + } +done: + iod->cmd.common.dptr.prp1 =3D cpu_to_le64(prp1_dma); + iod->cmd.common.dptr.prp2 =3D cpu_to_le64(prp2_dma); + return BLK_STS_OK; +free_prps: + iod->cmd.common.dptr.prp1 =3D cpu_to_le64(prp1_dma); + iod->cmd.common.dptr.prp2 =3D cpu_to_le64(prp2_dma); + nvme_free_descriptors(req); + return BLK_STS_RESOURCE; +} + static void nvme_free_prps(struct request *req, unsigned int attrs) { struct nvme_iod *iod =3D blk_mq_rq_to_pdu(req); @@ -943,6 +1068,11 @@ static void nvme_unmap_data(struct request *req) struct device *dma_dev =3D nvmeq->dev->dev; unsigned int attrs =3D 0; =20 + if (blk_mq_rq_is_dmabuf(req)) { + nvme_rq_clean_dmabuf_map(nvmeq->dev, req); + return; + } + if (iod->flags & IOD_SINGLE_SEGMENT) { static_assert(offsetof(union nvme_data_ptr, prp1) =3D=3D offsetof(union nvme_data_ptr, sgl.addr)); @@ -1257,6 +1387,9 @@ static blk_status_t nvme_map_data(struct request *req) struct blk_dma_iter iter; blk_status_t ret; =20 + if (blk_mq_rq_is_dmabuf(req)) + return nvme_rq_setup_dmabuf_map(req, nvmeq); + /* * Try to skip the DMA iterator for single segment requests, as that * significantly improves performances for small I/O sizes. @@ -2284,6 +2417,172 @@ static int nvme_create_queue(struct nvme_queue *nvm= eq, int qid, bool polled) return result; } =20 +#ifdef CONFIG_DMA_SHARED_BUFFER + +struct nvme_dma_buf_io_ctx { + struct dma_buf_attachment *attach; + struct dma_buf_io_ctx *ctx; + struct nvme_dev *dev; + struct list_head list; +}; + +static void nvme_dmabuf_invalidate_mappings(struct dma_buf_attachment *att= ach) +{ + struct dma_buf_io_ctx *ctx =3D attach->importer_priv; + + dma_buf_io_invalidate_mappings(ctx); +} + +static const struct dma_buf_attach_ops nvme_dmabuf_importer_ops =3D { + .invalidate_mappings =3D nvme_dmabuf_invalidate_mappings, + .allow_peer2peer =3D true, +}; + +static struct dma_buf_io_map *nvme_dma_buf_io_map(struct dma_buf_io_ctx *c= tx) +{ + unsigned nr_entries =3D ctx->dmabuf->size / NVME_CTRL_PAGE_SIZE; + struct nvme_dma_buf_io_ctx *nvme_ctx =3D ctx->dev_priv; + struct dma_buf_attachment *attach =3D nvme_ctx->attach; + unsigned long tmp, i =3D 0; + struct nvme_dmabuf_map *map; + struct scatterlist *sg; + struct sg_table *sgt; + int ret; + + dma_resv_assert_held(ctx->dmabuf->resv); + + map =3D kvmalloc_flex(*map, dma_list, nr_entries); + if (!map) + return ERR_PTR(-ENOMEM); + + sgt =3D dma_buf_map_attachment(attach, ctx->dir); + if (IS_ERR(sgt)) { + ret =3D PTR_ERR(sgt); + sgt =3D NULL; + goto err; + } + + for_each_sgtable_dma_sg(sgt, sg, tmp) { + dma_addr_t dma_addr =3D sg_dma_address(sg); + unsigned long sg_len =3D sg_dma_len(sg); + + if ((sg_len % NVME_CTRL_PAGE_SIZE) || + (dma_addr % NVME_CTRL_PAGE_SIZE)) { + ret =3D -EINVAL; + goto err; + } + while (sg_len) { + map->dma_list[i++] =3D dma_addr; + dma_addr +=3D NVME_CTRL_PAGE_SIZE; + sg_len -=3D NVME_CTRL_PAGE_SIZE; + } + } + + ret =3D dma_buf_io_init_map(ctx, &map->base, sgt); + if (ret) + goto err; + map->nr_entries =3D nr_entries; + map->sgt =3D sgt; + return &map->base; +err: + if (sgt) + dma_buf_unmap_attachment(attach, sgt, ctx->dir); + kfree(map); + return ERR_PTR(ret); +} + +static void nvme_dma_buf_io_unmap(struct dma_buf_io_ctx *ctx, + struct dma_buf_io_map *map_base) +{ + struct nvme_dma_buf_io_ctx *nvme_ctx =3D ctx->dev_priv; + struct nvme_dmabuf_map *map =3D to_nvme_dmabuf_map(map_base); + + dma_resv_assert_held(ctx->dmabuf->resv); + + dma_buf_unmap_attachment(nvme_ctx->attach, map->sgt, ctx->dir); + kvfree(map); +} + +static void nvme_dma_buf_io_release(struct dma_buf_io_ctx *ctx) +{ + struct nvme_dma_buf_io_ctx *nvme_ctx =3D ctx->dev_priv; + struct nvme_dev *dev =3D nvme_ctx->dev; + + mutex_lock(&dev->dmabuf_lock); + if (!list_empty(&nvme_ctx->list)) { + dma_buf_detach(ctx->dmabuf, nvme_ctx->attach); + list_del_init(&nvme_ctx->list); + } + mutex_unlock(&dev->dmabuf_lock); + + nvme_put_ctrl(&dev->ctrl); + kfree(nvme_ctx); +} + +const struct dma_buf_io_ops nvme_dma_buf_io_ops =3D { + .map =3D nvme_dma_buf_io_map, + .unmap =3D nvme_dma_buf_io_unmap, + .release =3D nvme_dma_buf_io_release, +}; + +static int __nvme_pci_init_dma_buf_io_ctx(struct nvme_ctrl *ctrl, + struct dma_buf_io_ctx *ctx) +{ + struct nvme_dev *dev =3D to_nvme_dev(ctrl); + struct nvme_dma_buf_io_ctx *nvme_ctx; + struct dma_buf_attachment *attach; + + nvme_ctx =3D kzalloc_obj(*nvme_ctx); + if (!nvme_ctx) + return -ENOMEM; + + attach =3D dma_buf_dynamic_attach(ctx->dmabuf, dev->dev, + &nvme_dmabuf_importer_ops, ctx); + if (IS_ERR(attach)) { + kfree(nvme_ctx); + return PTR_ERR(attach); + } + + nvme_get_ctrl(ctrl); + list_add(&nvme_ctx->list, &dev->dmabuf_ctxs); + nvme_ctx->attach =3D attach; + nvme_ctx->ctx =3D ctx; + nvme_ctx->dev =3D dev; + ctx->dev_priv =3D nvme_ctx; + ctx->dev_ops =3D &nvme_dma_buf_io_ops; + return 0; +} + +static int nvme_pci_init_dma_buf_io_ctx(struct nvme_ctrl *ctrl, + struct dma_buf_io_ctx *ctx) +{ + struct nvme_dev *dev =3D to_nvme_dev(ctrl); + int ret; + + mutex_lock(&dev->dmabuf_lock); + ret =3D __nvme_pci_init_dma_buf_io_ctx(ctrl, ctx); + mutex_unlock(&dev->dmabuf_lock); + return ret; +} + +static void nvme_pci_remove_dmabuf(struct nvme_dev *dev) +{ + struct nvme_dma_buf_io_ctx *ctx, *tmp; + + mutex_lock(&dev->dmabuf_lock); + list_for_each_entry_safe(ctx, tmp, &dev->dmabuf_ctxs, list) { + dma_buf_io_detach(ctx->ctx); + dma_buf_detach(ctx->ctx->dmabuf, ctx->attach); + list_del_init(&ctx->list); + } + mutex_unlock(&dev->dmabuf_lock); +} +#else +static void nvme_pci_remove_dmabuf(struct nvme_dev *dev) +{ +} +#endif + static const struct blk_mq_ops nvme_mq_admin_ops =3D { .queue_rq =3D nvme_queue_rq, .complete =3D nvme_pci_complete_rq, @@ -3357,6 +3656,9 @@ static void nvme_dev_disable(struct nvme_dev *dev, bo= ol shutdown) nvme_unquiesce_admin_queue(&dev->ctrl); } mutex_unlock(&dev->shutdown_lock); + + if (shutdown) + nvme_pci_remove_dmabuf(dev); } =20 static int nvme_disable_prepare_reset(struct nvme_dev *dev, bool shutdown) @@ -3579,6 +3881,9 @@ static const struct nvme_ctrl_ops nvme_pci_ctrl_ops = =3D { .print_device_info =3D nvme_pci_print_device_info, .supports_pci_p2pdma =3D nvme_pci_supports_pci_p2pdma, .get_virt_boundary =3D nvme_pci_get_virt_boundary, +#ifdef CONFIG_DMA_SHARED_BUFFER + .init_dma_buf_io_ctx =3D nvme_pci_init_dma_buf_io_ctx, +#endif }; =20 static int nvme_dev_map(struct nvme_dev *dev) @@ -3701,6 +4006,8 @@ static struct nvme_dev *nvme_pci_alloc_dev(struct pci= _dev *pdev, return ERR_PTR(-ENOMEM); INIT_WORK(&dev->ctrl.reset_work, nvme_reset_work); mutex_init(&dev->shutdown_lock); + INIT_LIST_HEAD(&dev->dmabuf_ctxs); + mutex_init(&dev->dmabuf_lock); =20 dev->nr_write_queues =3D write_queues; dev->nr_poll_queues =3D poll_queues; @@ -4344,5 +4651,6 @@ MODULE_AUTHOR("Matthew Wilcox = "); MODULE_LICENSE("GPL"); MODULE_VERSION("1.0"); MODULE_DESCRIPTION("NVMe host PCIe transport driver"); +MODULE_IMPORT_NS("DMA_BUF"); module_init(nvme_init); module_exit(nvme_exit); --=20 2.54.0 From nobody Sat Oct 3 12:03:09 2026 Received: from mail-wr2-f34.google.com (mail-wr2-f34.google.com [74.125.225.98]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1DB7D4C7555 for ; Mon, 28 Sep 2026 13:32:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.225.98 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602384; cv=none; b=dKIn9vijOXiTxvK4XMsBihxtGIWnAiMK/B30hUmeGtgWQbkBmp7d1sN4gVuvzZcH3OZ6kWJNDonSxuEa34zN7NYLJvaZwINNv3WIx6Jtf/F51RvdyXmO0PVn4jiiwzqopDdDjSxLmThJZGpG8NWgqGQXlubSulUAejJoLkCqd4c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602384; c=relaxed/simple; bh=wiOPOVzZkfrXRxbFz1UgU3+Xbr5CbqnKoLrDHMr2xo4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=osvPoayjQc61yX8ZQ5gS0dQxv/l2yHAoTud8I5op+PQP5FUBz2BxEjc2Yz/gfpEHEZsjmea+hRuHYWn8z1gEpwP62Ft0CxI8uw4QwxmdlEFMkMarcglX9oL1nZ1t00YRzcdlL0t8WVuf6Tu++IBJdC4BfRqUlxFum6Z+yoXIe3k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=A103FaSb; arc=none smtp.client-ip=74.125.225.98 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="A103FaSb" Received: by mail-wr2-f34.google.com with SMTP id ffacd0b85a97d-48884b0219bso1298250f8f.2 for ; Mon, 28 Sep 2026 06:32:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790602375; x=1791207175; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Z28dlsJTIpkqTAOZ5w69VF46XysVuY21MZeRZQmbEno=; b=A103FaSb31zfTZFdiyeg1rlVNhV/Y0mSzi1pmQMEVhQ8h5MmC+LzF6Loh5BR9R33Nu cJa0QpNtbiOq+814q1zyA2vrH/3JT8bXxDuJ0liEcR6uz3vAIPvzV3lKF6t+z+kRYepk AxFsfktwN0mRy1zjKsAIsUdKrBraHkOnIdqEokmBv4DzlbM6ctZiGT0tLs8xUp2jcIRq Tw4fTzJA9EB6nlXzd80bMpQk44iyuYjdQ2HrqXOQUH3yz85QDVbgMgwL7Z7waULl7lwz SFB7QL4L0BB0On9OuWfhYBapzspB5/8MCoMzobna0Wf3GetL1lEtETI5ccEXEybzOjE0 9ARg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790602375; x=1791207175; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=Z28dlsJTIpkqTAOZ5w69VF46XysVuY21MZeRZQmbEno=; b=QLKbxllmUKvZifdXbzTveHvfsmnBg2ftG+/SOxOsqSMrylw5r4NdOsHikRrxYlYnZH IqVP5LDjQxWpdr6wuquCoS+UAIZPa5beuzXZwJUv9vBoEIOu1v0xWytWd6ZaOuu4zc9H wFdwspvhCugjV94jVN8VZx/U1Sbe2aHQmoBJKwDjxYLII7x3nEs2VyHVueCMxiItF1s0 9rR/l25SkjzbfhO62SaYo4kSVs2g6ysIWYpI3I+Ru30Dr3oD6U8+K7DYstHifWLllHwK nTTLYXQZ7JOvHTurnq3La0gbUvkvboHhhMguacc7WSA/WaUUwzIcpPXB09Js9IcnFUcY 7syA== X-Forwarded-Encrypted: i=1; AKwUvBzZViZs1ORCILu90CGjGBuB5734zJ5MHTWf3RByqp6wGl9SHZ7dAvLua+xsIYxmIfyaWSDC25jcSkKfqyc=@vger.kernel.org X-Gm-Message-State: AFq9FYIJyVt3fEAaOB8LjRm6CZ2c8Q1iXmqGJFoG0eScMF/zwJRSnTJQ XAPHxW7AJ5s18RTHRmYNb9g7COH03aqdDAJ2Vi5Iidgl0YC35T2V7RCq X-Gm-Gg: AYBFou2Txw0q150tKlEF9xRG6shk903cip9nKoJquhYzRfWjAplp26uTU7/d1e5jrnV O8/68HlhoqflNRhKsUK+4N+mUXyGQieR9cKDGV2RTmqtsrwDSCbNE2cqOma6IStfldpnVYAlFC9 DlyIWFmDeXaq+Ec6U9Ou1wxQBQ0G4vPQFH/X8HNXbOhUwuTlrBJtBJw1NXKL//+tjkWGmZoopoi aTA9Is7UEO5lwSC/yyfstrwxnmX4QP9e/CCK9VqRAExTw4ny43CIb9Y/ZRWnYJzDUGk0Po1ca6e E3UYAOMmmcekCM6pMI8F/cEVwoaVK7iiOMxQwh1Gepeqakz6pvlIFFSHyaO0AmHhxerUK2eyK0i ed7y8chNA72XY/vC7m4FPTA/Y5zkOjKHZ0tLITBlY8dbGLmFOItOMy6IUTWl0tnKqIMMbzsDqi1 gJAlegnnu/q2dYjhtCimMhO7kMjXYvIl8Eej6d2d7fiLveH+iTdsfCcLTyTjPCstLLQclnXF7Ff HmypInp8bURl0dpvN4IypqjetS032ToWGQkV1HiXqNPuoPjjOv9LR0HmZRVsmRUc2qmhcj7EJUE X-Received: by 2002:a5d:5d87:0:b0:487:342:d147 with SMTP id ffacd0b85a97d-4887db39b7fmr20252053f8f.39.1790602374884; Mon, 28 Sep 2026 06:32:54 -0700 (PDT) Received: from 127.mynet ([2a01:4b00:bd21:4f00:7cc6:d3ca:494:116c]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4887a349731sm28090164f8f.12.2026.09.28.06.32.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2026 06:32:54 -0700 (PDT) From: Pavel Begunkov To: linux-block@vger.kernel.org Cc: asml.silence@gmail.com, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-nvme@lists.infradead.org, linux-fsdevel@vger.kernel.org, io-uring@vger.kernel.org, Christoph Hellwig , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Keith Busch , Sagi Grimberg , Alexander Viro , Christian Brauner , Jan Kara , Andrew Morton , Jens Axboe , Nitesh Shetty , Kanchan Joshi , Anuj Gupta , Tushar Gohad , William Power , Phil Cayton , Matthew Brost , Alasdair Kergon , Mike Snitzer , Mikulas Patocka , Benjamin Marzinski , dm-devel@lists.linux.dev Subject: [PATCH v7 07/13] nvme-pci: rename nvme_pci_sgl_set_data to nvme_pci_dma_iter_set_sgl Date: Mon, 28 Sep 2026 14:32:23 +0100 Message-ID: X-Mailer: git-send-email 2.54.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Anuj Gupta Rename the blk_dma_iter based nvme_pci_sgl_set_data() to nvme_pci_dma_iter_set_sgl(). Suggested-by: Christoph Hellwig Reviewed-by: Christoph Hellwig Signed-off-by: Anuj Gupta Signed-off-by: Pavel Begunkov --- drivers/nvme/host/pci.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c index e58bdd9a4098..bb8051c62926 100644 --- a/drivers/nvme/host/pci.c +++ b/drivers/nvme/host/pci.c @@ -1277,7 +1277,7 @@ static blk_status_t nvme_pci_setup_data_prp(struct re= quest *req, return BLK_STS_IOERR; } =20 -static void nvme_pci_sgl_set_data(struct nvme_sgl_desc *sge, +static void nvme_pci_dma_iter_set_sgl(struct nvme_sgl_desc *sge, struct blk_dma_iter *iter) { sge->addr =3D cpu_to_le64(iter->addr); @@ -1307,7 +1307,7 @@ static blk_status_t nvme_pci_setup_data_sgl(struct re= quest *req, iod->cmd.common.flags =3D NVME_CMD_SGL_METABUF; =20 if (entries =3D=3D 1 || blk_rq_dma_map_coalesce(&iod->dma_state)) { - nvme_pci_sgl_set_data(&iod->cmd.common.dptr.sgl, iter); + nvme_pci_dma_iter_set_sgl(&iod->cmd.common.dptr.sgl, iter); iod->total_len +=3D iter->len; return BLK_STS_OK; } @@ -1329,7 +1329,7 @@ static blk_status_t nvme_pci_setup_data_sgl(struct re= quest *req, iter->status =3D BLK_STS_IOERR; break; } - nvme_pci_sgl_set_data(&sg_list[mapped++], iter); + nvme_pci_dma_iter_set_sgl(&sg_list[mapped++], iter); iod->total_len +=3D iter->len; } while (blk_rq_dma_map_iter_next(req, nvmeq->dev->dev, iter)); =20 @@ -1492,13 +1492,13 @@ static blk_status_t nvme_pci_setup_meta_iter(struct= request *req) iod->cmd.common.metadata =3D cpu_to_le64(sgl_dma); if (entries =3D=3D 1) { iod->meta_total_len =3D iter.len; - nvme_pci_sgl_set_data(sg_list, &iter); + nvme_pci_dma_iter_set_sgl(sg_list, &iter); return BLK_STS_OK; } =20 sgl_dma +=3D sizeof(*sg_list); do { - nvme_pci_sgl_set_data(&sg_list[++i], &iter); + nvme_pci_dma_iter_set_sgl(&sg_list[++i], &iter); iod->meta_total_len +=3D iter.len; } while (blk_rq_integrity_dma_map_iter_next(req, dev->dev, &iter)); =20 --=20 2.54.0 From nobody Sat Oct 3 12:03:09 2026 Received: from mail-wr2-f35.google.com (mail-wr2-f35.google.com [74.125.225.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6896E3451A9 for ; Mon, 28 Sep 2026 13:32:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.225.99 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602385; cv=none; b=ETTLwMJA1rekuYyA9prspEl4qfewiwpH99uEsu6HvQoL+9WB6Xoyp8geHBLhdyNb4sQVd7baAJHcQaKYTlOvdFrVVbNqH5VB3j8ADmhpcGZv+DZrxE9LySOuPPP2IH+KDkBLx89hhAmvCObHod8LTAzHRzk7T/Btv0iKiUKRf0U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602385; c=relaxed/simple; bh=M6hLYVFKnXK9dPYxl8iRzXzPnel4ScDW0eLjXw//xzo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Alk7rLliZjshGr6qCyVjS992GfOWncISxQ+TTDr+HNIs1OLGuoRpUpcGVgFqSit2WWg6i8Bt5K7KNlCju6+za+Sg4i/EGYKtT8VBZAjZ4LtEueUHt7inf87JxS60rzwNa59zrryrEREx5BzHMZf12jI/9AjQFZx8a0ixoBHAGZo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=pfJTwi5J; arc=none smtp.client-ip=74.125.225.99 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="pfJTwi5J" Received: by mail-wr2-f35.google.com with SMTP id ffacd0b85a97d-48879d4fd8aso1754938f8f.2 for ; Mon, 28 Sep 2026 06:32:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790602377; x=1791207177; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=j1oj1dAJP0I40+o8SeAXsOmkZXs4Z4JZibo2wpFxfPw=; b=pfJTwi5Jb+sUYXxCKXsLpBOKROssfPu+SPm7Czp5pLeo/kFI5c/eDS6w4kDL9TzeZb g0r00s3kijsscpbbZmZHvNn8l4JoRss/Z2Cc4dYCtuZwbJ8NM9KEmnRFGcWK8kDduNh+ JN4AfvhMZQ0ovq7LF1G3ferf3wyOddQg5y1VwbAqp2sXJYojw5R06363LQkl8G4wo38i rulIiggA8v++DbAyT2tavxtXpppBOm+8lsPg4rZx4SiM2EewNPAW0VGz9y5QGZUVpmR+ CN6yO1R498UYvQ2DhUFX0kCIHf0oBRFs3AEAiVMlz1ueRfXKZ56HSsDkDv8wUGU/gVC+ i63g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790602377; x=1791207177; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=j1oj1dAJP0I40+o8SeAXsOmkZXs4Z4JZibo2wpFxfPw=; b=Y/TFpRQR+HI93GT348bpabHEGgIAuTqG6cibSpjEMszjipb7Jd2d2gpLR+K0F6P3Hw BYWqJ5Mi58RWP6wNtcvUdzJ9yzn2L/KLbJuS2VUAWCYHTANfu3d4q8m35U4ClhFudDzm tLgpulQdNFAO2RZPljSR8sif/V+y/fEWAfc4OdTaYp0Ghcqsz/MTH8LDsusq5WVatWgk sddyctWwHg6DME1M1YSzjoc8Fq07xrZx3AiGl/EufeWPSnBIBbjt+pJYOh7Mj1D6/GBK vqBjXzCilawJbjcuVhZ5Oa4IJIgxRgmvqkuONxc6ZUordc8NtL7r5qbC7iKZ5ConbCVd 2Yug== X-Forwarded-Encrypted: i=1; AKwUvBxNNZkbf7x5rl0GnB2qR2LMFe6nFMO+lg/5IZWC0X8sLxutJmjLRsCMFUo940inQoZTBwUe7rqJiqdnTMY=@vger.kernel.org X-Gm-Message-State: AFq9FYL2K70CLVty6y8Xco4rvswYrbCqlOdzXQX1QTn8zzy0nu1GAKpG 3ovXjd50YFxSaX2553MbdiEI8MxslE02tZ6vUnGEfX2U+P/Wx1zEjPRs X-Gm-Gg: AYBFou0/3yQvB0kXcupOQOgTrfMXMeM3ELkxmQFwCOhl6L5uR1dO+GHHPEEsSW75eM2 SPne8+15dMRn4lbsC8UCzGrXly8TUqkHrTwU5tQ4sw5rFKNhqYPnUcTKv/EQkbis8CG33+Zyetv kwzqlGoNq9PiAO/qixKJA/Oofg74MNqFypGXiICc5J0Z6jIBZ87q4UXWW1avNL8SMQsEr7hkmOV ACyDq7Dj4tlnXnpbdiwDLUPk7LTiBJTthtDrxImt2jg31f2RNFLshA+sAHlJ/Z8kbh3uemTQdqf c7sguTqixdMlUQtbM60IJgYiOTOYaQBdk5MDZjR0VrZMSC/7aJoIAxE6Z7lx3R9jzq5buqRE9NX gKuTEc7P0R/shE86QBcEJ8mEiH43DbmGXKE7NClzXylDeOnrZKn4mYqC778wVdG11qslXaZnpAt JKhNXO4cKtOB5eXwwywvxwd0dlrNFYGG/x+BM99ZXwvIZuMe8dvaCUPBlzNzudiM0lxWXAO42ZK 2WBwUObmaL3EQ9/d3kCRlhVyoAVKXuqJmy7qN1XP0lVvQOYrva0s+lRfhe98he8UXZPt46c3BtM X-Received: by 2002:a5d:5f53:0:b0:488:8b07:f3fc with SMTP id ffacd0b85a97d-48aeddea01dmr1376199f8f.17.1790602376733; Mon, 28 Sep 2026 06:32:56 -0700 (PDT) Received: from 127.mynet ([2a01:4b00:bd21:4f00:7cc6:d3ca:494:116c]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4887a349731sm28090164f8f.12.2026.09.28.06.32.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2026 06:32:55 -0700 (PDT) From: Pavel Begunkov To: linux-block@vger.kernel.org Cc: asml.silence@gmail.com, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-nvme@lists.infradead.org, linux-fsdevel@vger.kernel.org, io-uring@vger.kernel.org, Christoph Hellwig , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Keith Busch , Sagi Grimberg , Alexander Viro , Christian Brauner , Jan Kara , Andrew Morton , Jens Axboe , Nitesh Shetty , Kanchan Joshi , Anuj Gupta , Tushar Gohad , William Power , Phil Cayton , Matthew Brost , Alasdair Kergon , Mike Snitzer , Mikulas Patocka , Benjamin Marzinski , dm-devel@lists.linux.dev Subject: [PATCH v7 08/13] nvme-pci: add SGL support for the dmabuf path Date: Mon, 28 Sep 2026 14:32:24 +0100 Message-ID: <9d09ba51f1335a17576f3b92f0e93230c39751d2.1790602223.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Anuj Gupta Add SGL support in addition to PRP for dmabuf-backed requests, building the descriptor list from the mapping's sg_table. Reviewed-by: Christoph Hellwig Signed-off-by: Anuj Gupta Signed-off-by: Pavel Begunkov --- drivers/nvme/host/pci.c | 153 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 149 insertions(+), 4 deletions(-) diff --git a/drivers/nvme/host/pci.c b/drivers/nvme/host/pci.c index bb8051c62926..36d8987bb28d 100644 --- a/drivers/nvme/host/pci.c +++ b/drivers/nvme/host/pci.c @@ -1277,12 +1277,18 @@ static blk_status_t nvme_pci_setup_data_prp(struct = request *req, return BLK_STS_IOERR; } =20 +static void nvme_pci_sgl_set_data(struct nvme_sgl_desc *sge, + dma_addr_t addr, u32 len) +{ + sge->addr =3D cpu_to_le64(addr); + sge->length =3D cpu_to_le32(len); + sge->type =3D NVME_SGL_FMT_DATA_DESC << 4; +} + static void nvme_pci_dma_iter_set_sgl(struct nvme_sgl_desc *sge, struct blk_dma_iter *iter) { - sge->addr =3D cpu_to_le64(iter->addr); - sge->length =3D cpu_to_le32(iter->len); - sge->type =3D NVME_SGL_FMT_DATA_DESC << 4; + nvme_pci_sgl_set_data(sge, iter->addr, iter->len); } =20 static void nvme_pci_sgl_set_seg(struct nvme_sgl_desc *sge, @@ -1293,6 +1299,145 @@ static void nvme_pci_sgl_set_seg(struct nvme_sgl_de= sc *sge, sge->type =3D NVME_SGL_FMT_LAST_SEG_DESC << 4; } =20 +static unsigned int nvme_pci_dmabuf_sgl_nents(struct request *req) +{ + struct bio *bio =3D req->bio; + struct nvme_dmabuf_map *map =3D to_nvme_dmabuf_map(bio->bi_dmabuf_map); + struct scatterlist *sg; + unsigned long tmp; + size_t offset =3D bio->bi_iter.bi_offset; + size_t remaining =3D blk_rq_payload_bytes(req); + unsigned int nents =3D 0; + + for_each_sgtable_dma_sg(map->sgt, sg, tmp) { + size_t sg_len =3D sg_dma_len(sg); + + if (!remaining) + break; + if (offset >=3D sg_len) { + offset -=3D sg_len; + continue; + } + + sg_len -=3D offset; + offset =3D 0; + + do { + size_t chunk =3D min(remaining, sg_len); + + nents++; + sg_len -=3D chunk; + remaining -=3D chunk; + } while (sg_len && remaining); + } + + if (unlikely(remaining)) + return 0; + + return nents; +} + +static blk_status_t nvme_rq_setup_dmabuf_sgl(struct request *req, + struct nvme_queue *nvmeq, unsigned int entries) +{ + struct nvme_iod *iod =3D blk_mq_rq_to_pdu(req); + struct bio *bio =3D req->bio; + struct nvme_dmabuf_map *map =3D to_nvme_dmabuf_map(bio->bi_dmabuf_map); + size_t length =3D blk_rq_payload_bytes(req); + struct nvme_sgl_desc *sg_list =3D &iod->cmd.common.dptr.sgl; + bool pooled =3D entries > 1; + dma_addr_t sgl_dma =3D 0; + unsigned int mapped =3D 0; + unsigned long tmp; + struct scatterlist *sg; + size_t offset =3D bio->bi_iter.bi_offset; + size_t remaining =3D length; + + if (!entries) + return BLK_STS_IOERR; + + iod->cmd.common.flags =3D NVME_CMD_SGL_METABUF; + iod->total_len =3D length; + + nvme_dmabuf_map_sync_for_device(nvmeq->dev, req); + + /* entries =3D=3D 1 fits in the inline descriptor; more needs a pool. */ + if (pooled) { + if (entries <=3D NVME_SMALL_POOL_SIZE / sizeof(*sg_list)) + iod->flags |=3D IOD_SMALL_DESCRIPTOR; + + sg_list =3D dma_pool_alloc(nvme_dma_pool(nvmeq, iod), GFP_ATOMIC, + &sgl_dma); + if (!sg_list) + return BLK_STS_RESOURCE; + iod->descriptors[iod->nr_descriptors++] =3D sg_list; + } + + for_each_sgtable_dma_sg(map->sgt, sg, tmp) { + size_t sg_len =3D sg_dma_len(sg); + dma_addr_t addr =3D sg_dma_address(sg); + + if (!remaining) + break; + if (offset >=3D sg_len) { + offset -=3D sg_len; + continue; + } + + addr +=3D offset; + sg_len -=3D offset; + offset =3D 0; + + do { + u32 chunk =3D min_t(size_t, remaining, sg_len); + + if (WARN_ON_ONCE(mapped =3D=3D entries)) + goto err_free; + nvme_pci_sgl_set_data(&sg_list[mapped++], addr, chunk); + + addr +=3D chunk; + sg_len -=3D chunk; + remaining -=3D chunk; + } while (sg_len && remaining); + } + + if (unlikely(remaining)) + goto err_free; + + if (pooled) + nvme_pci_sgl_set_seg(&iod->cmd.common.dptr.sgl, sgl_dma, + mapped); + return BLK_STS_OK; +err_free: + if (pooled) { + iod->nr_descriptors--; + dma_pool_free(nvme_dma_pool(nvmeq, iod), sg_list, sgl_dma); + } + return BLK_STS_IOERR; +} + +static blk_status_t nvme_rq_setup_dmabuf(struct request *req, + struct nvme_queue *nvmeq, enum nvme_use_sgl use_sgl) +{ + unsigned int entries; + size_t avg_seg; + + if (use_sgl =3D=3D SGL_UNSUPPORTED) + return nvme_rq_setup_dmabuf_map(req, nvmeq); + + entries =3D nvme_pci_dmabuf_sgl_nents(req); + + if (use_sgl =3D=3D SGL_FORCED) + return nvme_rq_setup_dmabuf_sgl(req, nvmeq, entries); + + avg_seg =3D entries ? + DIV_ROUND_UP(blk_rq_payload_bytes(req), entries) : 0; + if (sgl_threshold && avg_seg >=3D sgl_threshold) + return nvme_rq_setup_dmabuf_sgl(req, nvmeq, entries); + + return nvme_rq_setup_dmabuf_map(req, nvmeq); +} + static blk_status_t nvme_pci_setup_data_sgl(struct request *req, struct blk_dma_iter *iter) { @@ -1388,7 +1533,7 @@ static blk_status_t nvme_map_data(struct request *req) blk_status_t ret; =20 if (blk_mq_rq_is_dmabuf(req)) - return nvme_rq_setup_dmabuf_map(req, nvmeq); + return nvme_rq_setup_dmabuf(req, nvmeq, use_sgl); =20 /* * Try to skip the DMA iterator for single segment requests, as that --=20 2.54.0 From nobody Sat Oct 3 12:03:09 2026 Received: from mail-wr2-f33.google.com (mail-wr2-f33.google.com [74.125.225.97]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 971AB4C6525 for ; Mon, 28 Sep 2026 13:33:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.225.97 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602387; cv=none; b=APx9vgHKBF6WQ5+TzhcyKIl9RTCwx5sGrbNmLh94Ty6l+VxxyXyypio4q2ZiW6fpvciQcdP6Q8MxVKgd77wdUhBu0XzHQdQ1IdL7cmWcWR+I2966WKjBco0kEkHMP4mbpdfpyD/xtTXO/dkgX5lVp9FPy0FPcNc7T9dUjoj/JM8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602387; c=relaxed/simple; bh=Z4vOxks9qTIFK+TCf+DcsECaWrDahRJWHvnK/Oy0YsE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=UtEGoNndgjQ30LIWXl2rh0WZHC+DY1UYkPYh1LWlkVFno9LuALSi5jPOIc6mUFiQqno0iExZIkxUJjMCiPIpcl7yCa+8gkT+r2W9TTHydFQfjYygdBDUqA0ba2O3LttLATCIVevUX5WvTQF3s2bLQrf35lWZku887OCP/dGh+d8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=UY3RrCwh; arc=none smtp.client-ip=74.125.225.97 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="UY3RrCwh" Received: by mail-wr2-f33.google.com with SMTP id ffacd0b85a97d-482f6351831so1773696f8f.1 for ; Mon, 28 Sep 2026 06:33:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790602379; x=1791207179; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=/RT09IM6iAPFk2VI7P8d4NpW1v04uxG67FoGdSGxGhI=; b=UY3RrCwhYnPhXr8AkzcnPeNEkbrBMyA04UBKWmnTDBNGELNHpkWH7w5fS5Txp1Huc8 hC1amO1hXCloFTtcV/9kThFy3Bah+Qxa+4sLYJxvNT912FKfzrPTDu2CsboIvPFtheT6 XTMjl+eaf1MgkmjSuG01yKrsGrVGSpCEQvbjClfptMxRdTrYa+me8F24MROwh0iMp8QJ cbBfbnKppGaxfMKLM67XEjEjnwUjshA35fxgjnfoDMIzSi+OejtxQ1xTgEOL42yMhm6G z37g0kLzqiJ59alYwznS+6643oEu1CyPgllnqMcNN5uzzh30t3+1QVR4upyxq6tlX/3J d5kA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790602379; x=1791207179; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=/RT09IM6iAPFk2VI7P8d4NpW1v04uxG67FoGdSGxGhI=; b=eJxsgJM80a6QkkgzhHHnnAfqq3NNL8I/szMQJGoQ8AYrE9SNoFn0eP+E3gR3uWAD3/ 8Z706CBDSEkdPBsPTAyyoVugMwP883AFQMcV5+dQIQxRsebgpR3RZZnZasgDxsc/D4Dg QRiYhiGg1iypMh+EivSKDGGcpC8SeXU35wAYvT4hrf4HYzkhOPXlQUJ9MkeEoKgapA4w KP420CYq84RchSYUw7UW2neEUelikeJExqlGimguYhCKofFukvOym6W1f8FGo13YdICz dcCsKkyMyOvxuQQEt+bZs6oqDde5ql6q5SSk6FelnlChGYLBVF+Yxri7a4AlFhD+g+kE QKRg== X-Forwarded-Encrypted: i=1; AKwUvBwzKQzCY1wOKXXO+LrIeMJpY1ODkiueqRRjWMmdPzHrFqfRNf8CIvJN6DkAjAFUAD9kr3gxhNLKgAKrTxo=@vger.kernel.org X-Gm-Message-State: AFq9FYIVL2831zxeLi6Emms/JV0dNCtMowy1sxdK2ZIcYHjxGxp8wAly YusbRxARy78WG0gAm7v18SsvnO9/lRPimtjOOrS4MR0w7jCysBH1muPe X-Gm-Gg: AYBFou3IheM6vK8GRv5+oY0AxgTJX0l8qNW/iISz02QW6TCNKBlSRFPjIxcJUi9zYBx KF0S8hbYWM6NAEV6ISVeyDe2CujcwEtudwlq+nRZ1sauZNCimypDqNO5CQwZOEXiObrTAm8mdT1 wkvo5PKcuga3n/+tKhoMosmlhXUZj56tuMWyzWlsoEA773tvfeer8HL45n5BJpQ5XJUsINoqaBr 6Q44oiSgIHHf8dF7HzwqFDQt0hpTeMXDGFsJotY2esy9nWbHMFYZA57FJ8vqxMSrjuVU15wh4es EBHeIUUyW5kjpjnzv8sWBWU7bK1VRWkmmzio5IBpVbeTdawfa7htmj2UbvLYEwMcrTBu3bBbm2Y iMA9LMzv4vRQJ/PQZ0Hjc43YnSrUpw8BShxUEQl5T0DP2FzmeWlW5FDjokCPzu7l1iT2mzcwnkb nVkAuaxflTF8ureTFLb0aKQR8RKuW5j3LdyDO4qmThu/uuYWmUaagskiFY44pecLL5J4ezYSito QxfTf2dCJ3PNjDQx6q4vC1KJqPEB9+wEFPZrMdD1AvVUYsOscUTwjGZUqpF5f3wDohRbI1Wb8sA X-Received: by 2002:a05:6000:1887:b0:486:f4ae:72a2 with SMTP id ffacd0b85a97d-4887170dd0dmr23900440f8f.1.1790602378210; Mon, 28 Sep 2026 06:32:58 -0700 (PDT) Received: from 127.mynet ([2a01:4b00:bd21:4f00:7cc6:d3ca:494:116c]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4887a349731sm28090164f8f.12.2026.09.28.06.32.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2026 06:32:57 -0700 (PDT) From: Pavel Begunkov To: linux-block@vger.kernel.org Cc: asml.silence@gmail.com, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-nvme@lists.infradead.org, linux-fsdevel@vger.kernel.org, io-uring@vger.kernel.org, Christoph Hellwig , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Keith Busch , Sagi Grimberg , Alexander Viro , Christian Brauner , Jan Kara , Andrew Morton , Jens Axboe , Nitesh Shetty , Kanchan Joshi , Anuj Gupta , Tushar Gohad , William Power , Phil Cayton , Matthew Brost , Alasdair Kergon , Mike Snitzer , Mikulas Patocka , Benjamin Marzinski , dm-devel@lists.linux.dev Subject: [PATCH v7 09/13] io_uring/rsrc: introduce buf registration structure Date: Mon, 28 Sep 2026 14:32:25 +0100 Message-ID: X-Mailer: git-send-email 2.54.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" In preparation to following changes, instead of passing an iovec for buffer registration introduce a new structure. It'll be moved to uapi later, but for now it's initialised early from a user provided iovec. Signed-off-by: Pavel Begunkov --- io_uring/rsrc.c | 47 ++++++++++++++++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c index 51b46e624ddd..da767b34eb55 100644 --- a/io_uring/rsrc.c +++ b/io_uring/rsrc.c @@ -27,8 +27,13 @@ struct io_rsrc_update { u32 offset; }; =20 +struct io_uring_regbuf_desc { + __u64 uaddr; + __u64 size; +}; + static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, - struct iovec *iov); + struct io_uring_regbuf_desc *desc); =20 static int hpage_acct_ref(struct io_ring_ctx *ctx, struct page *hpage, bool *acct_new) @@ -81,6 +86,15 @@ static bool hpage_acct_unref(struct io_ring_ctx *ctx, st= ruct page *hpage) =20 #define IO_CACHED_BVECS_SEGS 32 =20 +static void io_iov_to_regbuf_desc(const struct iovec *iov, + struct io_uring_regbuf_desc *desc) +{ + *desc =3D (struct io_uring_regbuf_desc) { + .uaddr =3D (u64)(uintptr_t)iov->iov_base, + .size =3D iov->iov_len, + }; +} + int __io_account_mem(struct user_struct *user, unsigned long nr_pages) { unsigned long page_limit, cur_pages, new_pages; @@ -381,6 +395,7 @@ static int __io_sqe_buffers_update(struct io_ring_ctx *= ctx, return -EINVAL; =20 for (done =3D 0; done < nr_args; done++) { + struct io_uring_regbuf_desc desc; struct io_rsrc_node *node; u64 tag =3D 0; =20 @@ -394,7 +409,9 @@ static int __io_sqe_buffers_update(struct io_ring_ctx *= ctx, err =3D -EFAULT; break; } - node =3D io_sqe_buffer_register(ctx, iov); + + io_iov_to_regbuf_desc(iov, &desc); + node =3D io_sqe_buffer_register(ctx, &desc); if (IS_ERR(node)) { err =3D PTR_ERR(node); break; @@ -853,26 +870,26 @@ bool io_check_coalesce_buffer(struct page **page_arra= y, int nr_pages, } =20 static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, - struct iovec *iov) + struct io_uring_regbuf_desc *desc) { + unsigned long uaddr =3D (unsigned long)desc->uaddr; + size_t size =3D desc->size; struct io_mapped_ubuf *imu =3D NULL; struct page **pages =3D NULL; struct io_rsrc_node *node; unsigned long off; - size_t size; int ret, nr_pages, i; struct io_imu_folio_data data; bool coalesced =3D false; =20 - if (!iov->iov_base) { - if (iov->iov_len) + if (!uaddr) { + if (size) return ERR_PTR(-EFAULT); /* remove the buffer without installing a new one */ return NULL; } =20 - ret =3D io_validate_user_buf_range((unsigned long)iov->iov_base, - iov->iov_len); + ret =3D io_validate_user_buf_range(uaddr, size); if (ret) return ERR_PTR(ret); =20 @@ -881,8 +898,7 @@ static struct io_rsrc_node *io_sqe_buffer_register(stru= ct io_ring_ctx *ctx, return ERR_PTR(-ENOMEM); =20 ret =3D -ENOMEM; - pages =3D io_pin_pages((unsigned long) iov->iov_base, iov->iov_len, - &nr_pages); + pages =3D io_pin_pages(uaddr, size, &nr_pages); if (IS_ERR(pages)) { ret =3D PTR_ERR(pages); pages =3D NULL; @@ -904,10 +920,9 @@ static struct io_rsrc_node *io_sqe_buffer_register(str= uct io_ring_ctx *ctx, if (ret) goto done; =20 - size =3D iov->iov_len; /* store original address for later verification */ - imu->ubuf =3D (unsigned long) iov->iov_base; - imu->len =3D iov->iov_len; + imu->ubuf =3D uaddr; + imu->len =3D size; imu->folio_shift =3D PAGE_SHIFT; imu->release =3D io_release_ubuf; imu->priv =3D imu; @@ -917,7 +932,7 @@ static struct io_rsrc_node *io_sqe_buffer_register(stru= ct io_ring_ctx *ctx, imu->folio_shift =3D data.folio_shift; refcount_set(&imu->refs, 1); =20 - off =3D (unsigned long)iov->iov_base & ~PAGE_MASK; + off =3D uaddr & ~PAGE_MASK; if (coalesced) off +=3D data.first_folio_page_idx << PAGE_SHIFT; =20 @@ -969,6 +984,7 @@ int io_sqe_buffers_register(struct io_ring_ctx *ctx, vo= id __user *arg, memset(iov, 0, sizeof(*iov)); =20 for (i =3D 0; i < nr_args; i++) { + struct io_uring_regbuf_desc desc; struct io_rsrc_node *node; u64 tag =3D 0; =20 @@ -992,7 +1008,8 @@ int io_sqe_buffers_register(struct io_ring_ctx *ctx, v= oid __user *arg, } } =20 - node =3D io_sqe_buffer_register(ctx, iov); + io_iov_to_regbuf_desc(iov, &desc); + node =3D io_sqe_buffer_register(ctx, &desc); if (IS_ERR(node)) { ret =3D PTR_ERR(node); break; --=20 2.54.0 From nobody Sat Oct 3 12:03:09 2026 Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E12BF4D1796 for ; Mon, 28 Sep 2026 13:33:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602394; cv=none; b=XdGBHYX+GdSfjWhk4yZKfVpBgHI9ukbpfm8tZae6rJceLzygHvp8+uzB6BaVg+ICewpbkEq2dFSzKAAHly3k18rDVue0j8Y5Gqw+Gbr8u6WUxoQfKmK73ko5uVGroxNgRXiXCvjdjI/1nMh9STGEM62Y/V4ArR9fjSTha0vSCQE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602394; c=relaxed/simple; bh=FAuNY7f+2IN9h+0fEp3doBi1UJ2umEK7JqOTw5NHGgE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=u05BaJ3N95vsFn126d5m+E57cU5gjMVFwxnBZyHxopOYgDQxdgOET8dwi6w0k9GiPs22Q2YSsa6gmmNIBJ4slpCAOiVizTQG/OJGtRFf3IeI7pvx3AxB9spTjAbWtlLGTOLYK/JFEEDOy25NqaRmeC0E/OZljVulc0wuWzBoERM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=ELjG5KMd; arc=none smtp.client-ip=209.85.221.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="ELjG5KMd" Received: by mail-wr1-f43.google.com with SMTP id ffacd0b85a97d-488615e6cfcso2031836f8f.0 for ; Mon, 28 Sep 2026 06:33:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790602380; x=1791207180; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=FutSyws4EmX9/QkptnDh8TTYgLLGT7M1LNenj3p2Roc=; b=ELjG5KMdpzizAOeqRHzM/Q8UczuJHXyOSElj/rhyHpfYCY46eTyyb7Dqf+NFwtouKp W6glY0ru8WccCvJIxYkhln308Wi14oXXMLUXRN4cQo9PYf0EwUTuheRIiCiUZBp06R/F HLAyngEGfh0LcUc09PCDEEouvWbF/F9I+4CKRYk6WBzwJc10Zlbnj02APjsgMq65SCIy vJQHlsERuN1mJF6B2iKmhKoF8+sFhDpjpB4I+09X8kCrlHnWZcs6bNblPMq1PTrsioF7 lrIUuNqZaOYoqX0vfrB9jjJMnWrvmu0R2/tlhKXjasrbhhjppFzMrq2Zqb9V0QEQRQdv Hh8g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790602380; x=1791207180; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=FutSyws4EmX9/QkptnDh8TTYgLLGT7M1LNenj3p2Roc=; b=D9DsnMqPcH0lQGI1n176MUfFMddfMY+98OhF3+PnDdkmdw6FJc7kpuVsMyGP1dC2iu 5x3klxDBkFAe/3XBORWiTROtYruHhFOrydAduU047E40qxamFuTtC1oWjn2Q/69fVQ9u zljM6lU3QcYs2zE7VhJ3SoLR2wJ6T7G7v+15k6ZXrqM63AcyhV/Q1xD1KTXBZ6XSjx4W muGk68QZosZwJGynaAzz62jQGLemBsRvItciidggfUBVMsq+Ht4lx2LE/yVdEnjNR4zC 0T2zNHKecBh6t0liiQ1+Y8IpHeQsIeGrRig8/9+T4eJ+gGEmYskvnrmbZTjjqnbiXWiD BYxA== X-Forwarded-Encrypted: i=1; AKwUvBw7nWH1+byQ0uauWS5m0hpHtt5T+IYXm1Lw6pI9Nfk4RB0/hkDKJpRsTxY6IClQEg19KpgeFASDpV/ogkE=@vger.kernel.org X-Gm-Message-State: AFq9FYJTgxKHMlnx9VRFJvJyPIyZacdXMT1QqwnG7p/IUdW8UbthChXh NRRbKL+KkGRlHsCa5zLXyGanCwm2HKcfCuxmwE88nceDQe7MqiyRXh0p X-Gm-Gg: AYBFou12bhQlpZft7i0aVFZ0MM3fS7zKqCKBOH3qV3WlXTs7g/XEq9v47Dz+7OK4KW4 KGsKsL3W6GungTFlwdIDcfi2T4ZgQibadYTln7oLnq/snRYTLSyfgeMs0jTePkNUceLwvpdpOBY XgZ8SRkxroiDNgg/KW62EJykJWeFbWCXMoS5Hfoxyr1EOh0HzwVUuyHHMOBDmn1YvF6m4dvtiN0 wU4LEoA3Mqbdmoe5n8Go/o0n4ousXjTKyiLE9BRu33IRhU125mFuk1+wfHQ3TRPJFwithjb+JQI zDJpEXyGQCBEhitxDWc/7XI4EVHIcF84EQhZ5PNmVDUsPBIqzLaihfgWjmlCFRv+tvGg5Cu7Fw0 tQndOzSc7hpdmx/pfPcLab+lAQG7tUkt+2HYBfTmHvktg3nNITAiUkkqgpCerpWGbAyR5mclfmQ 0fPPz18lktW2r5HYLKXMTWqNaj906CTR/nOVDazvNcKd8atfrWMgN1JR1GrAvBKmXiMn1gHDH+I wqtvP+vksUoOLX/xuVfGX/RrG7wKhcK5gICRmCXKwGcWlzd1zBGX+UxHVO5ymDgLy9fyQip4qV1 X-Received: by 2002:a05:6000:2c04:b0:488:8b97:3cca with SMTP id ffacd0b85a97d-4888b973fd1mr9122372f8f.11.1790602379646; Mon, 28 Sep 2026 06:32:59 -0700 (PDT) Received: from 127.mynet ([2a01:4b00:bd21:4f00:7cc6:d3ca:494:116c]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4887a349731sm28090164f8f.12.2026.09.28.06.32.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2026 06:32:58 -0700 (PDT) From: Pavel Begunkov To: linux-block@vger.kernel.org Cc: asml.silence@gmail.com, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-nvme@lists.infradead.org, linux-fsdevel@vger.kernel.org, io-uring@vger.kernel.org, Christoph Hellwig , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Keith Busch , Sagi Grimberg , Alexander Viro , Christian Brauner , Jan Kara , Andrew Morton , Jens Axboe , Nitesh Shetty , Kanchan Joshi , Anuj Gupta , Tushar Gohad , William Power , Phil Cayton , Matthew Brost , Alasdair Kergon , Mike Snitzer , Mikulas Patocka , Benjamin Marzinski , dm-devel@lists.linux.dev Subject: [PATCH v7 10/13] io_uring/rsrc: extend buffer update Date: Mon, 28 Sep 2026 14:32:26 +0100 Message-ID: <32bc70d8c93bbbd2926b5597c436d9adb691aff8.1790602223.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We need to pass more information to buffer registration than we can fit into a single struct iovec. This patch allows users to optionally pass struct io_uring_regbuf_desc. Apart from having more space for future use cases, it also introduces registration types. Currently, the type can be either of IO_REGBUF_TYPE_UADDR, which mirrors the iovec path, or IO_REGBUF_TYPE_EMPTY for leaving a buffer table slot empty. The next patch introduces a dmabuf backed type, and can be useful for other extensions like splicing a list of user addresses (i.e. iovec[]), interoperability with zcrx, kernel allocated memory like was brough up by Cristoph. Note, the type only represents a registration option, which is distinct from how io_uring internally stores it. The flags field is not used yet but always useful to have, e.g. we can encode read-only / write-only restrictions using it. Signed-off-by: Pavel Begunkov --- include/uapi/linux/io_uring.h | 27 +++++++++++++- io_uring/rsrc.c | 69 ++++++++++++++++++++++------------- 2 files changed, 69 insertions(+), 27 deletions(-) diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h index 909fb7aea638..98b259901185 100644 --- a/include/uapi/linux/io_uring.h +++ b/include/uapi/linux/io_uring.h @@ -790,13 +790,38 @@ struct io_uring_rsrc_update { =20 struct io_uring_rsrc_update2 { __u32 offset; - __u32 resv; + __u32 flags; __aligned_u64 data; __aligned_u64 tags; __u32 nr; __u32 resv2; }; =20 +/* struct io_uring_rsrc_update2::flags */ +enum io_uring_rsrc_reg_flags { + /* + * Use the extended descriptor format for buffer updates, + * see struct io_uring_regbuf_desc + */ + IORING_RSRC_UPDATE_EXTENDED =3D 1U << 1, +}; + +/* Buffer registration type, passed in struct io_uring_regbuf_desc::type */ +enum io_uring_regbuf_type { + IO_REGBUF_TYPE_EMPTY, + IO_REGBUF_TYPE_UADDR, + + __IO_REGBUF_TYPE_MAX, +}; + +struct io_uring_regbuf_desc { + __u32 type; /* enum io_uring_regbuf_type */ + __u32 flags; + __u64 size; + __u64 uaddr; + __u64 __resv[7]; +}; + /* Skip updating fd indexes set to this value in the fd table */ #define IORING_REGISTER_FILES_SKIP (-2) =20 diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c index da767b34eb55..83a7ad9f5bc7 100644 --- a/io_uring/rsrc.c +++ b/io_uring/rsrc.c @@ -27,11 +27,6 @@ struct io_rsrc_update { u32 offset; }; =20 -struct io_uring_regbuf_desc { - __u64 uaddr; - __u64 size; -}; - static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, struct io_uring_regbuf_desc *desc); =20 @@ -90,9 +85,12 @@ static void io_iov_to_regbuf_desc(const struct iovec *io= v, struct io_uring_regbuf_desc *desc) { *desc =3D (struct io_uring_regbuf_desc) { + .type =3D IO_REGBUF_TYPE_UADDR, .uaddr =3D (u64)(uintptr_t)iov->iov_base, .size =3D iov->iov_len, }; + if (!desc->uaddr) + desc->type =3D IO_REGBUF_TYPE_EMPTY; } =20 int __io_account_mem(struct user_struct *user, unsigned long nr_pages) @@ -323,6 +321,8 @@ static int __io_sqe_files_update(struct io_ring_ctx *ct= x, return -ENXIO; if (up->offset + nr_args > ctx->file_table.data.nr) return -EINVAL; + if (up->flags) + return -EINVAL; =20 for (done =3D 0; done < nr_args; done++) { u64 tag =3D 0; @@ -382,9 +382,8 @@ static int __io_sqe_buffers_update(struct io_ring_ctx *= ctx, struct io_uring_rsrc_update2 *up, unsigned int nr_args) { + bool extended =3D up->flags & IORING_RSRC_UPDATE_EXTENDED; u64 __user *tags =3D u64_to_user_ptr(up->tags); - struct iovec fast_iov, *iov; - struct iovec __user *uvec; u64 user_data =3D up->data; __u32 done; int i, err; @@ -393,29 +392,49 @@ static int __io_sqe_buffers_update(struct io_ring_ctx= *ctx, return -ENXIO; if (up->offset + nr_args > ctx->buf_table.nr) return -EINVAL; + if (up->flags & ~IORING_RSRC_UPDATE_EXTENDED) + return -EINVAL; =20 for (done =3D 0; done < nr_args; done++) { struct io_uring_regbuf_desc desc; struct io_rsrc_node *node; u64 tag =3D 0; =20 - uvec =3D u64_to_user_ptr(user_data); - iov =3D iovec_from_user(uvec, 1, 1, &fast_iov, io_is_compat(ctx)); - if (IS_ERR(iov)) { - err =3D PTR_ERR(iov); - break; - } if (tags && copy_from_user(&tag, &tags[done], sizeof(tag))) { err =3D -EFAULT; break; } =20 - io_iov_to_regbuf_desc(iov, &desc); + if (extended) { + if (copy_from_user(&desc, u64_to_user_ptr(user_data), + sizeof(desc))) { + err =3D -EFAULT; + break; + } + user_data +=3D sizeof(desc); + } else { + struct iovec __user *uvec =3D u64_to_user_ptr(user_data); + struct iovec fast_iov, *iov; + + if (io_is_compat(ctx)) + user_data +=3D sizeof(struct compat_iovec); + else + user_data +=3D sizeof(struct iovec); + + iov =3D iovec_from_user(uvec, 1, 1, &fast_iov, io_is_compat(ctx)); + if (IS_ERR(iov)) { + err =3D PTR_ERR(iov); + break; + } + io_iov_to_regbuf_desc(iov, &desc); + } + node =3D io_sqe_buffer_register(ctx, &desc); if (IS_ERR(node)) { err =3D PTR_ERR(node); break; } + if (tag) { if (!node) { err =3D -EINVAL; @@ -426,10 +445,6 @@ static int __io_sqe_buffers_update(struct io_ring_ctx = *ctx, i =3D array_index_nospec(up->offset + done, ctx->buf_table.nr); io_reset_rsrc_node(ctx, &ctx->buf_table, i); ctx->buf_table.nodes[i] =3D node; - if (io_is_compat(ctx)) - user_data +=3D sizeof(struct compat_iovec); - else - user_data +=3D sizeof(struct iovec); } return done ? done : err; } @@ -464,7 +479,7 @@ int io_register_files_update(struct io_ring_ctx *ctx, v= oid __user *arg, memset(&up, 0, sizeof(up)); if (copy_from_user(&up, arg, sizeof(struct io_uring_rsrc_update))) return -EFAULT; - if (up.resv || up.resv2) + if (up.resv2) return -EINVAL; return __io_register_rsrc_update(ctx, IORING_RSRC_FILE, &up, nr_args); } @@ -478,7 +493,7 @@ int io_register_rsrc_update(struct io_ring_ctx *ctx, vo= id __user *arg, return -EINVAL; if (copy_from_user(&up, arg, sizeof(up))) return -EFAULT; - if (!up.nr || up.resv || up.resv2) + if (!up.nr || up.resv2) return -EINVAL; return __io_register_rsrc_update(ctx, type, &up, up.nr); } @@ -578,12 +593,9 @@ int io_files_update(struct io_kiocb *req, unsigned int= issue_flags) struct io_uring_rsrc_update2 up2; int ret; =20 + memset(&up2, 0, sizeof(up2)); up2.offset =3D up->offset; up2.data =3D up->arg; - up2.nr =3D 0; - up2.tags =3D 0; - up2.resv =3D 0; - up2.resv2 =3D 0; =20 if (up->offset =3D=3D IORING_FILE_INDEX_ALLOC) { ret =3D io_files_update_with_index_alloc(req, issue_flags); @@ -882,8 +894,13 @@ static struct io_rsrc_node *io_sqe_buffer_register(str= uct io_ring_ctx *ctx, struct io_imu_folio_data data; bool coalesced =3D false; =20 - if (!uaddr) { - if (size) + if (desc->type >=3D __IO_REGBUF_TYPE_MAX) + return ERR_PTR(-EINVAL); + if (!mem_is_zero(&desc->__resv, sizeof(desc->__resv)) || desc->flags) + return ERR_PTR(-EINVAL); + + if (desc->type =3D=3D IO_REGBUF_TYPE_EMPTY) { + if (uaddr || size) return ERR_PTR(-EFAULT); /* remove the buffer without installing a new one */ return NULL; --=20 2.54.0 From nobody Sat Oct 3 12:03:09 2026 Received: from mail-wm2-f13.google.com (mail-wm2-f13.google.com [74.125.225.141]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 953834D0CEE for ; Mon, 28 Sep 2026 13:33:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.225.141 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602390; cv=none; b=rXmmLTl9cTZ6ebfPy0guJB/EMg2S19Gyu2boZ8mI0Mm132X8SwN45Le14m8N1ziMj0yVAK90RSst72tbhX4lWu4SQI0I6vR12gYZWQEAFENlepc+ry+xQ85FTLOIlZXfd60ibpjzKTGp7F59QQU8GhXS+AuQUToUsLPfHYEmdkE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602390; c=relaxed/simple; bh=dp8A9m21F4Z6vnwVaB36/IYEFqc7xZs52MFL03Y1vS8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=LQ6fdJirUk8fzIYKcDc9284D3e4Uikp7oSoy0qdlOK/8s59fGHQtLXRAqE+1OyaVmZ99ilFi9eHV7UVGwUzpKhpfnESPol7JRCMUQrRi2Y6nuvyZHC9MWgkFvmv5WuJ1lmcV5vVOFIZUF90YMVn8H6b4BSidveys79U/Y8EldAw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=n0Vea775; arc=none smtp.client-ip=74.125.225.141 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="n0Vea775" Received: by mail-wm2-f13.google.com with SMTP id 5b1f17b1804b1-49fe8bf173aso15962925e9.3 for ; Mon, 28 Sep 2026 06:33:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790602382; x=1791207182; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=raEDt673oWsC7HjzDuokLb4yrdGb5LOQuk6WNjaexbY=; b=n0Vea775koJUlSUtF5+MNAJGT1aIZ2lp/MSZJl83hA2FW9hBK4FWdZHONhBVtNhryr j63EZ2n7a7ro0iSl7+VsBVaiLUwPniO15bfIH7dhdUd8pHcLQViQh3xyM5n+KMsTVAgZ MUjIUrThxu6h5r/3C0RvK4lr/TP/WwXPCJM7pc+8SXbjDAV5JL83DOXh0DmyVJRExuUl 0+gVrhXT4/xfoanMnKt7zpikee0YYBJrAot+UKHgG3vGVGsplXPPHBFyV8x8Fv/VCY6H TzDfRVOU7HyRc9NbZsQ/TU9+/E1QT4d4YNlVmI1ce1q0q/vKSLbzf2KWIAt26nDT6pq3 CJWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790602382; x=1791207182; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=raEDt673oWsC7HjzDuokLb4yrdGb5LOQuk6WNjaexbY=; b=NoCwrVSZf3kvDP3eQ2DWcmsDdZBSjOhBpTqpyK0ycC3RZ56AmFjUIDn+fkeoaRFgCd f/l5snpsYFNnTRPdNU04eOyZeZs63w9cuVDJuQ3ytHWJlqX0JcNGNU6FFmOsWE8LiBc6 7ZwnkW9012z4aBfEuMceOK6zXOWzKB6lINWZVOXFmvjghZejschSmBpRscaCu48VufEw Mx+GUZzRkLPmD1cfN5pk279tZ8UkBLTsKX0+sc/jucwFDIODdO/NqgrlGa2yUZwngonC QAlOfrvqk7GquWdfkVW7mZ7FymE3FYVGF0vLKI5jHPjmkSCmsz9AN/TSPh3gdiT4SkMF ci6Q== X-Forwarded-Encrypted: i=1; AKwUvBzNoelrxxUakWI6Hpi64E3m/GHISjgO5G24KHEygwD3+UYuZqMLd+N30fZWA9rWaMsN0nrO7k56l/wdBQg=@vger.kernel.org X-Gm-Message-State: AFuF++laDMqcY60uZ/C+EMKiJwSBBeoIsslH7gD1p1FgGga/f8aG4ADs sxADvvrG0vpsPzl1wliYjoqRijnEnG0y9JX15LZ40KVEAtBeX56m1uHt X-Gm-Gg: AYBFou1ik5NWww2jydbzp2LvjPwoEm9CQ819jVDhsYdn6pVSEIW3utRi3BGIA9lZvz3 HCRu+YsV4+NnelivTxvsEHjahvSZLd5tbZqCJL/1eJD+Dt9sKUdz+sDo9uS+YALa43gXyKPD4wF Avr0R5aF2TmAYp97ZwJbuQZiE0RjSiTv+ZCEw86dl4hV3N9doY5Sm3Bt2yDngkwbqR4hcXJJbJl aJLYO6PeTzxwCkPUNsg6AQEKccVoyaK38ueb/rKhxv4ViFFtcWNXsMNyIKSlv5N0ycrFMk33lE9 X+LMzOk+kcsFzOuOJWhnCKBGrPMHW7kwt/P3ByiL2z/t7ETqkNFiXgcDl0+m295TIIehAWfxhSg SAa+MX2dZ1SM/Td3aVynhXqzvI2ovYLMI6SERf+MYZJpjaa/roZBe9x33HUbw9g0SJl7C4l0vBA Zm92h2BVsgVnuussqu/kMViVGm2NLxk76cjsKwMGAupuwOFH1piLd7jiAfyC2Gli0noPSCZ0cqt hUhLaCxeFnQt52fSOH/Z7MYBE21+k6+y+tXRkyBj6yPUYzm8DlRw/I4TZqibvh/6W5wrohYB0Jr X-Received: by 2002:a05:600c:c3cb:20b0:49f:fc13:189d with SMTP id 5b1f17b1804b1-49ffc131a3bmr73381205e9.1.1790602381298; Mon, 28 Sep 2026 06:33:01 -0700 (PDT) Received: from 127.mynet ([2a01:4b00:bd21:4f00:7cc6:d3ca:494:116c]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4887a349731sm28090164f8f.12.2026.09.28.06.32.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2026 06:33:00 -0700 (PDT) From: Pavel Begunkov To: linux-block@vger.kernel.org Cc: asml.silence@gmail.com, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-nvme@lists.infradead.org, linux-fsdevel@vger.kernel.org, io-uring@vger.kernel.org, Christoph Hellwig , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Keith Busch , Sagi Grimberg , Alexander Viro , Christian Brauner , Jan Kara , Andrew Morton , Jens Axboe , Nitesh Shetty , Kanchan Joshi , Anuj Gupta , Tushar Gohad , William Power , Phil Cayton , Matthew Brost , Alasdair Kergon , Mike Snitzer , Mikulas Patocka , Benjamin Marzinski , dm-devel@lists.linux.dev Subject: [PATCH v7 11/13] io_uring/rsrc: add uncloneable regbuf flag Date: Mon, 28 Sep 2026 14:32:27 +0100 Message-ID: <6bb81fd0941222a30477dc849684d47333d1e4ce.1790602223.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" It's hard to implement cloning if the internal structure needs to be mutable and/or relies on other ring resources. In preparation to such buffer types, add a flag indicating that the buffer can't be cloned. It might be possible to add cloning in the future for them, but that would likely need reallocating the structure and reacquiring resources in case by case manner. Signed-off-by: Pavel Begunkov --- io_uring/rsrc.c | 5 +++++ io_uring/rsrc.h | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c index 83a7ad9f5bc7..65d6cf7201ce 100644 --- a/io_uring/rsrc.c +++ b/io_uring/rsrc.c @@ -1434,6 +1434,11 @@ static int io_clone_buffers(struct io_ring_ctx *ctx,= struct io_ring_ctx *src_ctx if (!src_node) { dst_node =3D NULL; } else { + if (src_node->buf->flags & IO_REGBUF_F_UNCLONEABLE) { + io_rsrc_data_free(ctx, &data); + return -EOPNOTSUPP; + } + dst_node =3D io_rsrc_node_alloc(ctx, IORING_RSRC_BUFFER); if (!dst_node) { io_rsrc_data_free(ctx, &data); diff --git a/io_uring/rsrc.h b/io_uring/rsrc.h index 9ef88383b363..73d7b1ebf0b6 100644 --- a/io_uring/rsrc.h +++ b/io_uring/rsrc.h @@ -26,7 +26,8 @@ struct io_rsrc_node { }; =20 enum { - IO_REGBUF_F_KBUF =3D 1, + IO_REGBUF_F_KBUF =3D 1 << 0, + IO_REGBUF_F_UNCLONEABLE =3D 1 << 1, }; =20 struct io_mapped_ubuf { --=20 2.54.0 From nobody Sat Oct 3 12:03:09 2026 Received: from mail-wr2-f34.google.com (mail-wr2-f34.google.com [74.125.225.98]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D5F414C9E0A for ; Mon, 28 Sep 2026 13:33:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.225.98 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602393; cv=none; b=uKzsiol1RVmTCNkbm1Q3DJO42Tt8D2qq8wcMu/9oxOnuwhtUvAxI40L2rcFcjzbZ7a0ix1QJPWuq7vrIAhF0kWrybvjK7Lkz2urjpR6HYMByQbgoxX0z6jAa51kCWWoUYmTa8KJNY+eBXEU1WQP9QJ6AkzDqMFqWMMbP2FEdnAY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602393; c=relaxed/simple; bh=IlrTb51Yp7Rjs5yQkZp4t/TrynsY1IZo7uIZ92+N310=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=c6ETtEyfuuWmeHqlLTY/0h13oKS+75oo5MWk7cNt4EPNsJrTgvxrox7YhdhDFofkNmWBNiiySKU8ElG9vuia9/bAL1Dj+EQINEGoATpfCBFsyzU1wuPlUzPMqDozztBM040nhObFg6UgzVyweZO5ot1LgJaq3ngSh51cLdEV5ok= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=HlWcxP56; arc=none smtp.client-ip=74.125.225.98 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="HlWcxP56" Received: by mail-wr2-f34.google.com with SMTP id ffacd0b85a97d-48884b926f0so1856550f8f.3 for ; Mon, 28 Sep 2026 06:33:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790602383; x=1791207183; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=vb+nXysx6Pid1tQCSKOtIWL9IN5+XlO/q6rrI+ZzfOE=; b=HlWcxP569zau9ROT+qN+SxznnaiYSV9KrVWazV0861xi53VRCILlOmXz5swGHJFDYI sr812SmszIoHGeRW93B6BeXrBPpqzJaczA/Num1FJ4xLXKGLFnqPI1Iex3Kld6ebToDb CuKRyO3ER+8mTNfkwi5baXDAyaTpSXduiIg0QOcfBPFW75QynblkyNOotCAFRCAcgwFT Gfui6bzkuxEattU7HPfagJf/E5ehELiGqP5AXNeaIVRUzy88LptUC3FLt87MaSAvyEHK UPL9I4kTU3OiYlKnDLUdwa380sqFPSzL8dd0c5KNuQtRj0fSvIMKhEpKaQi3OSNv0wGZ y9Gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790602383; x=1791207183; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=vb+nXysx6Pid1tQCSKOtIWL9IN5+XlO/q6rrI+ZzfOE=; b=HqX4yvZamjkgtvVjkZqfktzwoACDcDtpxBCzKLRbFQuJoA3Yk2wDQ7RMT7lQ/yPsBR qBLZns12p8t05YAkyA34o6eegXU3jANJiva+vJgxYVOTFleN4ZNqDVk8BSABz5jxyMD8 TfXSb+eSiSSx2YiWwmicHxBO66co4w53cFlgaZ2cb68OHbtND1JQx7tiGrI6XWoVHQWK NrFYBH63cz/Eghv4SUInV8ga6141pfzpS/xwZ93VszAGoxa0ZI3ADnl7VBA9EcAbZRz+ PIFUqDoqID3eT7g/1ITggz6A3+s5BMs7320cKT22ZzN0jyAHbICb+sHTRa+N8DY/nxGO KybQ== X-Forwarded-Encrypted: i=1; AKwUvBwLx/To2sTdHFhkum8XWk241AUp0l/Xa3QJBTW1wniivq4E8ot2zfilJaqwp0VbwFyOyRqYPMHmSypvqP4=@vger.kernel.org X-Gm-Message-State: AFq9FYJd3Pg8klkZ5GT5TYPF0r3+6V1Vlon7zqtnZ/eMJvbatm9Wc6iK jjK3+RAbf2ra8pt3Bed2uPtYrv5HMYLINxklKD+9uHd0/iWKHpQbDG+/d9G8BZr8 X-Gm-Gg: AYBFou06c+McqBrQANuTeQtL0cmBGAnMKNTPLDgedhvtKAf0RsECzVRdmoKg6znFiiA tLgxZ/W0Ufy37sc1ir3LjLT0ro6Cc/qTucJ9t6zIyNym9hOgDPMn0NLAzfFpN5wJQ48kSX3uDYH Ck0S15Sz7WA/7Gs8AIXFdf9iZPy7Fg+CjnPFgosPV969Y5kaIRkDgxeweQU6c+Jlwvr5vaYJUCl ax3BQ37zFVfzfTBKR6CgNzJrVeXAOz3tzSqqUw1hli36CJtk0iKxGBq9CDhFS/LQuh4woiX8Mpk 6BInn79DDGJ5F+7MjuNXBvaycn+EVqNO4ZxOVdHsQRB6mDLcLRrKet8nKcAWVBcMyZhZdZYjt7g at/6WD+y9UPri+AeEBkUsgOAA8XppVUf8j4t5w438HD3Gy6oVFkEgjKVOWh5ym8OzVg3KnLm/xD 5Gfpkgx7k1qdDVrZdnNltyZ3JObecdqc1TqKxHxneze7WrqrClW3fpOFV8npV34U2yg8gPIrWc5 Wmk4URKh2As7NucR3eIxkTWRRf9Cjwq0UMuCTBH+9MTVPe18pQRbJ6Ec0+PWZFTY7hBI/2/ITLG X-Received: by 2002:a05:6000:2893:b0:487:10a9:57cb with SMTP id ffacd0b85a97d-488719756c5mr21965332f8f.49.1790602382736; Mon, 28 Sep 2026 06:33:02 -0700 (PDT) Received: from 127.mynet ([2a01:4b00:bd21:4f00:7cc6:d3ca:494:116c]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4887a349731sm28090164f8f.12.2026.09.28.06.33.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2026 06:33:02 -0700 (PDT) From: Pavel Begunkov To: linux-block@vger.kernel.org Cc: asml.silence@gmail.com, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-nvme@lists.infradead.org, linux-fsdevel@vger.kernel.org, io-uring@vger.kernel.org, Christoph Hellwig , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Keith Busch , Sagi Grimberg , Alexander Viro , Christian Brauner , Jan Kara , Andrew Morton , Jens Axboe , Nitesh Shetty , Kanchan Joshi , Anuj Gupta , Tushar Gohad , William Power , Phil Cayton , Matthew Brost , Alasdair Kergon , Mike Snitzer , Mikulas Patocka , Benjamin Marzinski , dm-devel@lists.linux.dev Subject: [PATCH v7 12/13] io_uring/rsrc: add regbuf import flags Date: Mon, 28 Sep 2026 14:32:28 +0100 Message-ID: <692972b0ea345774fda580a53e2b1bbe5f771aa3.1790602223.git.asml.silence@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We'll have special registered buffer types that can't be used with all opcodes and need special handling. Add separate flags to control registered buffer import, which will be used to specify what kind of buffers the caller can handle. Signed-off-by: Pavel Begunkov --- io_uring/rsrc.c | 8 ++++---- io_uring/rsrc.h | 24 ++++++++++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c index 65d6cf7201ce..6f6860d6ca03 100644 --- a/io_uring/rsrc.c +++ b/io_uring/rsrc.c @@ -1298,9 +1298,9 @@ inline struct io_rsrc_node *io_find_buf_node(struct i= o_kiocb *req, return NULL; } =20 -int io_import_reg_buf(struct io_kiocb *req, struct iov_iter *iter, +int __io_import_reg_buf(struct io_kiocb *req, struct iov_iter *iter, u64 buf_addr, size_t len, int ddir, - unsigned issue_flags) + unsigned issue_flags, unsigned import_flags) { struct io_rsrc_node *node; =20 @@ -1709,9 +1709,9 @@ static int io_kern_bvec_size(struct iovec *iov, unsig= ned nr_iovs, return 0; } =20 -int io_import_reg_vec(int ddir, struct iov_iter *iter, +int __io_import_reg_vec(int ddir, struct iov_iter *iter, struct io_kiocb *req, struct iou_vec *vec, - unsigned nr_iovs, unsigned issue_flags) + unsigned nr_iovs, unsigned issue_flags, unsigned import_flags) { struct io_rsrc_node *node; struct io_mapped_ubuf *imu; diff --git a/io_uring/rsrc.h b/io_uring/rsrc.h index 73d7b1ebf0b6..351995b61e68 100644 --- a/io_uring/rsrc.h +++ b/io_uring/rsrc.h @@ -62,12 +62,28 @@ int io_rsrc_data_alloc(struct io_rsrc_data *data, unsig= ned nr); =20 struct io_rsrc_node *io_find_buf_node(struct io_kiocb *req, unsigned issue_flags); -int io_import_reg_buf(struct io_kiocb *req, struct iov_iter *iter, +int __io_import_reg_buf(struct io_kiocb *req, struct iov_iter *iter, u64 buf_addr, size_t len, int ddir, - unsigned issue_flags); -int io_import_reg_vec(int ddir, struct iov_iter *iter, + unsigned issue_flags, unsigned import_flags); +int __io_import_reg_vec(int ddir, struct iov_iter *iter, struct io_kiocb *req, struct iou_vec *vec, - unsigned nr_iovs, unsigned issue_flags); + unsigned nr_iovs, unsigned issue_flags, + unsigned import_flags); + +static inline int io_import_reg_buf(struct io_kiocb *req, struct iov_iter = *iter, + u64 buf_addr, size_t len, int ddir, + unsigned issue_flags) +{ + return __io_import_reg_buf(req, iter, buf_addr, len, ddir, issue_flags, 0= ); +} + +static inline int io_import_reg_vec(int ddir, struct iov_iter *iter, + struct io_kiocb *req, struct iou_vec *vec, + unsigned nr_iovs, unsigned issue_flags) +{ + return __io_import_reg_vec(ddir, iter, req, vec, nr_iovs, issue_flags, 0); +} + int io_prep_reg_iovec(struct io_kiocb *req, struct iou_vec *iv, const struct iovec __user *uvec, size_t uvec_segs); =20 --=20 2.54.0 From nobody Sat Oct 3 12:03:09 2026 Received: from mail-wr2-f12.google.com (mail-wr2-f12.google.com [74.125.225.76]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D89584D1787 for ; Mon, 28 Sep 2026 13:33:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.225.76 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602392; cv=none; b=aBi5fpVZTxjX3PlAjjaZUnKs6TjL29bbBy2dPFrj+sFDFliq6WytmQGWU3G4ceLgi5j4nC6sqygUuBEzJzWDVC2XBCrbwJgyOy+0h8vRVWAC3Yel9sVK7St5COl/8vT/74WRe4M/tcHOPVfgFJsLPo8VogBKnYLSvvsmPdTg8lI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790602392; c=relaxed/simple; bh=+TFUZF24zR90EzHuFN3/Z4BUjLDYP5TSG6Dr2rhm9r0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NSdFYNdGYX1dQObbx/sNuqsnLQ+cemyuazJCnsm6ZooKq2fwCLgdi7uTbpMwAOi0bOd111/R7/KfRdn3XcuDPTbf1z6enNV9mMgNQ5RBkJ//UN1L2tPMQeeN2X2VVzOXBNzS0oy8GXfsEFXx4eWfRUGTgvQQ6H1w0xG8A7nlTEc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=Yt7Xr6TA; arc=none smtp.client-ip=74.125.225.76 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Yt7Xr6TA" Received: by mail-wr2-f12.google.com with SMTP id ffacd0b85a97d-482f635552aso2403770f8f.2 for ; Mon, 28 Sep 2026 06:33:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1790602384; x=1791207184; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=S4++bv7VkupLTBlegq9bDur2NZ+d8zmcops2rZn7Th0=; b=Yt7Xr6TAM88UoZqbLa6e1T/A3BVhyLkeYj7LN38sD3jiFjyLMppbXMPNKj/gLa7M5L JUC+n7NSUOy7HiovAEhHwFYP1j+ADv45TlV8DTynQG1iDjYC4CLy2j9XBX/1Z9RkSXxG 210LQxXVfcEU2D420jLQLBXIL30Kvy3vs2B8yvAujkua5qkHyIaofHqkhORxNYS4ijQZ TC+Wrv9k0pmubltDZ38Quw/zI/r1fJz/N6FTt9HtCQM6Xr4Zj5K3Dkzl/XgH5aX6DQVZ xMqdLGl14CZdBd7eW20VG5O4IUK51AsEvFTojg+g+/kL+pnzFnSepy5z8/VML3ZijIO7 mPDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790602384; x=1791207184; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=S4++bv7VkupLTBlegq9bDur2NZ+d8zmcops2rZn7Th0=; b=VesFPQ30qd7mAllu+J2O7U6RH+8QLwFFQPffr/dJ6Vhm8QsGeuq0sQCcGiEPvi/guc XVJ1gcBOr7QkUbD4OTartGlE/fcXS8aBkyYeJeltyQ7BibAJBL1DjKOEivfuECc0qEpu ixMRg6CXtcBvkmUN8HdOZyJ+EYhNqxUkzjYX8bGXrcXLm3TVwg9INx7/k4lxBYL0FhN/ Uk0MFSUUWY5mCfQdNYH3MfIO3uthlVRsH/j047e186jV11RyuPwfXAbDXpgsUDLJlsmB uSleQNu8mxWBP/UW/N8o0IbPGOnsf+LYsg7EUP6pd7p4ZcVpeMg90Cr5L676c0/sr6Gl Vm0Q== X-Forwarded-Encrypted: i=1; AKwUvBwKhD8vokTUM08WLl9Rf61GNVpxB0jnUMpEhNppD4Cm0Mr6o30zRdY2BfTF1syy4KMshlU/tmhmpWfQfxw=@vger.kernel.org X-Gm-Message-State: AFq9FYLa2PBwlVQEmVRqnvnqfbQ2RHMRNcTwEECgot0p5QqY3ZvJL5HF ERicz9iHEe8TRosnWWj9m5xdw4AANhWZCoqoSQbrFgrZX55f4P0IKnzd X-Gm-Gg: AYBFou0ykzoJIEZU1fUvbSj/HOsX0wxgC06sxK/r64GCi+z0Ct1ZiDE/+7QpZLyoiV0 4yRqGMvBKe6o8VTzXBffdQ7KF64gzRkMqp0ePPD0scFSuBfiHW2tV87PlgQaCm7Lls0x7MgS7k+ hvVjRfnGy43mrbqHS6xE87MBr2U89h6TFAdwse8ceIJTohxTCLH7WQJwKMTLrJgYyVk3fYnDBLh KjNNIos3fY3ODeB7IDIPgGAFLa6Xx1glZ3irPY64DJ3IB1CC5QQ60ZJR+QKi43WYx8/WY83nYTC 54HcmNGRqBl1ZMbydxhF/b0L4N+z4SocLvmeIgB04iE5nJsgCkcJ2Yt7I6hqLxskjZA0CCFO7hP t04PgnSK080oh7LmLbgeHBEwHePyh7kNo7NAJciS8Ov+uP0SzWqbNQsJUPcdynEj3AHKQBSZqny 8pT1O2+p9r6+j9Vh7vUnkVL85hp1Izh5rcRWoLWfyL35qFp2/2fEkQ0UF4DK2aWwq9I4+8K3+ar FJR8Pz6o2b4bBU/pWEyWdo4WYfY25EqQoINme7qKL7uroy+SYniqNwDHa+v3220UNSbyIJV9Noz X-Received: by 2002:a05:6000:2608:b0:488:84ba:ae2e with SMTP id ffacd0b85a97d-48884baaf4emr12983113f8f.0.1790602384178; Mon, 28 Sep 2026 06:33:04 -0700 (PDT) Received: from 127.mynet ([2a01:4b00:bd21:4f00:7cc6:d3ca:494:116c]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4887a349731sm28090164f8f.12.2026.09.28.06.33.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Sep 2026 06:33:03 -0700 (PDT) From: Pavel Begunkov To: linux-block@vger.kernel.org Cc: asml.silence@gmail.com, linux-kernel@vger.kernel.org, linux-media@vger.kernel.org, dri-devel@lists.freedesktop.org, linaro-mm-sig@lists.linaro.org, linux-nvme@lists.infradead.org, linux-fsdevel@vger.kernel.org, io-uring@vger.kernel.org, Christoph Hellwig , Sumit Semwal , =?UTF-8?q?Christian=20K=C3=B6nig?= , Keith Busch , Sagi Grimberg , Alexander Viro , Christian Brauner , Jan Kara , Andrew Morton , Jens Axboe , Nitesh Shetty , Kanchan Joshi , Anuj Gupta , Tushar Gohad , William Power , Phil Cayton , Matthew Brost , Alasdair Kergon , Mike Snitzer , Mikulas Patocka , Benjamin Marzinski , dm-devel@lists.linux.dev Subject: [PATCH v7 13/13] io_uring/rsrc: add dmabuf backed registered buffers Date: Mon, 28 Sep 2026 14:32:29 +0100 Message-ID: X-Mailer: git-send-email 2.54.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Implement dmabuf backed registered buffers. To register them, the user should specify IO_REGBUF_TYPE_DMABUF for the regitration and pass the desired dmabuf fd and a file for which it should be registered. From there, it can be used with io_uring read/write requests IORING_OP_{READ,WRITE}_FIXED) as normal. The requests should be issued against the file specified during registration, and otherwise they'll be failed. The user should also be prepared to handle spurious -EAGAIN by reissuing the request. Internally, dmabuf registered buffers is an optin feature for io_uring request opcodes and they should pass a special flag on import to use it. Suggested-by: David Wei Suggested-by: Vishal Verma Suggested-by: Tushar Gohad Signed-off-by: Pavel Begunkov --- include/linux/io_uring_types.h | 5 + include/uapi/linux/io_uring.h | 6 +- io_uring/io_uring.c | 3 +- io_uring/rsrc.c | 162 ++++++++++++++++++++++++++++++++- io_uring/rsrc.h | 18 ++++ io_uring/rw.c | 6 +- 6 files changed, 192 insertions(+), 8 deletions(-) diff --git a/include/linux/io_uring_types.h b/include/linux/io_uring_types.h index 90fea94ad202..3d41ca622114 100644 --- a/include/linux/io_uring_types.h +++ b/include/linux/io_uring_types.h @@ -11,6 +11,7 @@ =20 struct iou_loop_params; struct io_uring_bpf_ops; +struct dma_buf_io_map; =20 enum { /* @@ -610,6 +611,7 @@ enum { REQ_F_IMPORT_BUFFER_BIT, REQ_F_SQE_COPIED_BIT, REQ_F_IOPOLL_BIT, + REQ_F_DROP_DMABUF_BIT, =20 /* not a real bit, just to check we're not overflowing the space */ __REQ_F_LAST_BIT, @@ -705,6 +707,8 @@ enum { REQ_F_SQE_COPIED =3D IO_REQ_FLAG(REQ_F_SQE_COPIED_BIT), /* request must be iopolled to completion (set in ->issue()) */ REQ_F_IOPOLL =3D IO_REQ_FLAG(REQ_F_IOPOLL_BIT), + /* there is a dma map attached to request that needs to be dropped */ + REQ_F_DROP_DMABUF =3D IO_REQ_FLAG(REQ_F_DROP_DMABUF_BIT), }; =20 struct io_tw_req { @@ -827,6 +831,7 @@ struct io_kiocb { /* custom credentials, valid IFF REQ_F_CREDS is set */ const struct cred *creds; struct io_wq_work work; + struct dma_buf_io_map *dmabuf_map; =20 struct io_big_cqe { u64 extra1; diff --git a/include/uapi/linux/io_uring.h b/include/uapi/linux/io_uring.h index 98b259901185..c39297e8beb6 100644 --- a/include/uapi/linux/io_uring.h +++ b/include/uapi/linux/io_uring.h @@ -810,6 +810,7 @@ enum io_uring_rsrc_reg_flags { enum io_uring_regbuf_type { IO_REGBUF_TYPE_EMPTY, IO_REGBUF_TYPE_UADDR, + IO_REGBUF_TYPE_DMABUF, =20 __IO_REGBUF_TYPE_MAX, }; @@ -819,7 +820,10 @@ struct io_uring_regbuf_desc { __u32 flags; __u64 size; __u64 uaddr; - __u64 __resv[7]; + + __s32 dmabuf_fd; + __s32 target_fd; + __u64 __resv[6]; }; =20 /* Skip updating fd indexes set to this value in the fd table */ diff --git a/io_uring/io_uring.c b/io_uring/io_uring.c index a67b2adeda36..267b2a43131a 100644 --- a/io_uring/io_uring.c +++ b/io_uring/io_uring.c @@ -109,7 +109,7 @@ =20 #define IO_REQ_CLEAN_SLOW_FLAGS (REQ_F_REFCOUNT | IO_REQ_LINK_FLAGS | \ REQ_F_REISSUE | REQ_F_POLLED | \ - IO_REQ_CLEAN_FLAGS) + IO_REQ_CLEAN_FLAGS | REQ_F_DROP_DMABUF) =20 #define IO_TCTX_REFS_CACHE_NR (1U << 10) =20 @@ -1134,6 +1134,7 @@ static void io_free_batch_list(struct io_ring_ctx *ct= x, io_queue_next(req); if (unlikely(req->flags & IO_REQ_CLEAN_FLAGS)) io_clean_op(req); + io_req_drop_dmabuf(req); } io_put_file(req); io_req_put_rsrc_nodes(req); diff --git a/io_uring/rsrc.c b/io_uring/rsrc.c index 6f6860d6ca03..c22bc374b1cd 100644 --- a/io_uring/rsrc.c +++ b/io_uring/rsrc.c @@ -10,6 +10,7 @@ #include #include #include +#include =20 #include =20 @@ -881,6 +882,94 @@ bool io_check_coalesce_buffer(struct page **page_array= , int nr_pages, return true; } =20 +struct io_regbuf_dma { + struct dma_buf_io_ctx *ctx; + struct file *target_file; +}; + +static void io_release_reg_dmabuf(void *priv) +{ + struct io_regbuf_dma *db =3D priv; + + fput(db->target_file); + dma_buf_io_ctx_release(db->ctx); + kfree(db); +} + +static struct io_rsrc_node *io_register_dmabuf(struct io_ring_ctx *ctx, + struct io_uring_regbuf_desc *desc) +{ + struct io_rsrc_node *node =3D NULL; + struct io_mapped_ubuf *imu =3D NULL; + struct io_regbuf_dma *regbuf =3D NULL; + struct file *target_file =3D NULL; + struct dma_buf *dmabuf =3D NULL; + int ret; + + if (!IS_ENABLED(CONFIG_DMA_SHARED_BUFFER)) + return ERR_PTR(-EOPNOTSUPP); + if (ctx->flags & IORING_SETUP_IOPOLL) + return ERR_PTR(-EOPNOTSUPP); + if (desc->uaddr || desc->size) + return ERR_PTR(-EINVAL); + + ret =3D -ENOMEM; + node =3D io_rsrc_node_alloc(ctx, IORING_RSRC_BUFFER); + if (!node) + return ERR_PTR(-ENOMEM); + imu =3D io_alloc_imu(ctx, 0); + if (!imu) + goto err; + regbuf =3D kzalloc(sizeof(*regbuf), GFP_KERNEL); + if (!regbuf) + goto err; + + ret =3D -EBADF; + target_file =3D fget(desc->target_fd); + if (!target_file) + goto err; + + dmabuf =3D dma_buf_get(desc->dmabuf_fd); + if (IS_ERR(dmabuf)) { + ret =3D PTR_ERR(dmabuf); + dmabuf =3D NULL; + goto err; + } + ret =3D io_validate_user_buf_range(0, dmabuf->size); + if (ret) + goto err; + + ret =3D dma_buf_io_ctx_create(target_file, dmabuf, DMA_BIDIRECTIONAL, + ®buf->ctx); + if (ret) + goto err; + + regbuf->target_file =3D target_file; + imu->nr_bvecs =3D 0; + imu->ubuf =3D 0; + imu->len =3D dmabuf->size; + imu->folio_shift =3D 0; + imu->release =3D io_release_reg_dmabuf; + imu->priv =3D regbuf; + imu->flags =3D IO_REGBUF_F_DMABUF; + imu->dir =3D IO_BUF_DEST | IO_BUF_SOURCE; + refcount_set(&imu->refs, 1); + node->buf =3D imu; + dma_buf_put(dmabuf); + return node; +err: + kfree(regbuf); + if (imu) + io_free_imu(ctx, imu); + if (node) + io_cache_free(&ctx->node_cache, node); + if (target_file) + fput(target_file); + if (dmabuf) + dma_buf_put(dmabuf); + return ERR_PTR(ret); +} + static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, struct io_uring_regbuf_desc *desc) { @@ -899,6 +988,12 @@ static struct io_rsrc_node *io_sqe_buffer_register(str= uct io_ring_ctx *ctx, if (!mem_is_zero(&desc->__resv, sizeof(desc->__resv)) || desc->flags) return ERR_PTR(-EINVAL); =20 + if (desc->type =3D=3D IO_REGBUF_TYPE_DMABUF) + return io_register_dmabuf(ctx, desc); + + if (desc->dmabuf_fd || desc->target_fd) + return ERR_PTR(-EINVAL); + if (desc->type =3D=3D IO_REGBUF_TYPE_EMPTY) { if (uaddr || size) return ERR_PTR(-EFAULT); @@ -1223,9 +1318,59 @@ static int io_import_kbuf(int ddir, struct iov_iter = *iter, return 0; } =20 -static int io_import_fixed(int ddir, struct iov_iter *iter, +void io_drop_dmabuf_node(struct io_kiocb *req) +{ + struct io_mapped_ubuf *imu; + + if (!IS_ENABLED(CONFIG_DMA_SHARED_BUFFER)) + return; + if (WARN_ON_ONCE(req->buf_node->type !=3D IORING_RSRC_BUFFER)) + return; + imu =3D req->buf_node->buf; + if (WARN_ON_ONCE(!(imu->flags & IO_REGBUF_F_DMABUF))) + return; + dma_buf_io_map_drop(req->dmabuf_map); + req->flags &=3D ~REQ_F_DROP_DMABUF; +} + +static int io_import_dmabuf(struct io_kiocb *req, + int ddir, struct iov_iter *iter, + struct io_mapped_ubuf *imu, + size_t len, size_t offset, + unsigned issue_flags) +{ + bool nowait =3D issue_flags & IO_URING_F_NONBLOCK; + struct io_regbuf_dma *db =3D imu->priv; + struct dma_buf_io_map *map; + + if (!IS_ENABLED(CONFIG_DMA_SHARED_BUFFER)) + return -EOPNOTSUPP; + if (!len) + return -EFAULT; + if (req->file !=3D db->target_file) + return -EBADF; + + if (req->flags & REQ_F_DROP_DMABUF) { + map =3D req->dmabuf_map; + goto init_iter; + } + + map =3D dma_buf_io_get_map(db->ctx, nowait); + if (unlikely(IS_ERR(map))) + return PTR_ERR(map); + req->dmabuf_map =3D map; + req->flags |=3D REQ_F_DROP_DMABUF; +init_iter: + iov_iter_dmabuf_map(iter, ddir, map, offset, len); + return 0; +} + +static int io_import_fixed(struct io_kiocb *req, + int ddir, struct iov_iter *iter, struct io_mapped_ubuf *imu, - u64 buf_addr, size_t len) + u64 buf_addr, size_t len, + unsigned issue_flags, + unsigned import_flags) { const struct bio_vec *bvec; size_t folio_mask; @@ -1245,6 +1390,12 @@ static int io_import_fixed(int ddir, struct iov_iter= *iter, =20 offset =3D buf_addr - imu->ubuf; =20 + if (imu->flags & IO_REGBUF_F_DMABUF) { + if (!(import_flags & IO_REGBUF_IMPORT_ALLOW_DMABUF)) + return -EFAULT; + return io_import_dmabuf(req, ddir, iter, imu, len, offset, + issue_flags); + } if (imu->flags & IO_REGBUF_F_KBUF) return io_import_kbuf(ddir, iter, imu, len, offset); =20 @@ -1307,7 +1458,8 @@ int __io_import_reg_buf(struct io_kiocb *req, struct = iov_iter *iter, node =3D io_find_buf_node(req, issue_flags); if (!node) return -EFAULT; - return io_import_fixed(ddir, iter, node->buf, buf_addr, len); + return io_import_fixed(req, ddir, iter, node->buf, buf_addr, len, + issue_flags, import_flags); } =20 static int io_buffer_acct_cloned_hpages(struct io_ring_ctx *ctx, @@ -1729,7 +1881,9 @@ int __io_import_reg_vec(int ddir, struct iov_iter *it= er, iovec_off =3D vec->nr - nr_iovs; iov =3D vec->iovec + iovec_off; =20 - if (imu->flags & IO_REGBUF_F_KBUF) { + if (imu->flags & IO_REGBUF_F_DMABUF) { + return -EOPNOTSUPP; + } else if (imu->flags & IO_REGBUF_F_KBUF) { int ret =3D io_kern_bvec_size(iov, nr_iovs, imu, &nr_segs); =20 if (unlikely(ret)) diff --git a/io_uring/rsrc.h b/io_uring/rsrc.h index 351995b61e68..8e36a954169e 100644 --- a/io_uring/rsrc.h +++ b/io_uring/rsrc.h @@ -28,6 +28,11 @@ struct io_rsrc_node { enum { IO_REGBUF_F_KBUF =3D 1 << 0, IO_REGBUF_F_UNCLONEABLE =3D 1 << 1, + IO_REGBUF_F_DMABUF =3D 1 << 3, +}; + +enum { + IO_REGBUF_IMPORT_ALLOW_DMABUF =3D 1 << 1, }; =20 struct io_mapped_ubuf { @@ -170,4 +175,17 @@ static inline void io_alloc_cache_vec_kasan(struct iou= _vec *iv) io_vec_free(iv); } =20 +void io_drop_dmabuf_node(struct io_kiocb *req); + +static inline void io_req_drop_dmabuf(struct io_kiocb *req) +{ + if (!IS_ENABLED(CONFIG_DMA_SHARED_BUFFER)) + return; + if (!(req->flags & REQ_F_DROP_DMABUF)) + return; + if (WARN_ON_ONCE(!(req->flags & REQ_F_BUF_NODE))) + return; + io_drop_dmabuf_node(req); +} + #endif diff --git a/io_uring/rw.c b/io_uring/rw.c index 755166e90746..9867ae2905e3 100644 --- a/io_uring/rw.c +++ b/io_uring/rw.c @@ -367,8 +367,8 @@ static int io_init_rw_fixed(struct io_kiocb *req, unsig= ned int issue_flags, if (io->bytes_done) return 0; =20 - ret =3D io_import_reg_buf(req, &io->iter, rw->addr, rw->len, ddir, - issue_flags); + ret =3D __io_import_reg_buf(req, &io->iter, rw->addr, rw->len, ddir, + issue_flags, IO_REGBUF_IMPORT_ALLOW_DMABUF); iov_iter_save_state(&io->iter, &io->iter_state); return ret; } @@ -583,6 +583,8 @@ static void io_complete_rw(struct kiocb *kiocb, long re= s) struct io_rw *rw =3D container_of(kiocb, struct io_rw, kiocb); struct io_kiocb *req =3D cmd_to_io_kiocb(rw); =20 + io_req_drop_dmabuf(req); + /* ring owner may block in freeze_super() before task_work runs */ if (kiocb->ki_flags & IOCB_WRITE) io_req_end_write(req); --=20 2.54.0