All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Pavel Begunkov" <asml.silence@gmail.com>
Cc: dri-devel@lists.freedesktop.org, media-ci@linuxtv.org
Subject: Re: [PATCH v10 01/13] dma-buf: introduce initial file I/O infrastructure
Date: Tue, 13 Oct 2026 21:06:32 +0000	[thread overview]
Message-ID: <sashiko-outbox-170510@kernel.org> (raw)
In-Reply-To: <1caebf340d205f6d4c5137e7a8a548ea3cf3b87d.1791924581.git.asml.silence@gmail.com>

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] dma-buf: memory and reference leak in dma_buf_io_ctx_create missing dev_ops
- [High] dma-buf: map leaked on invalid min_seg_shift in __dma_buf_io_create_map
- [High] dma-buf: deadlock risk by blocking on I/O inside dma_resv lock
- [Medium] dma-buf: resource leak in dma_buf_io_ctx_release() on map check failure
--

commit 420452d61f9f92ad48f07cea0dcd04c4e4a65bac
Author: Pavel Begunkov <asml.silence@gmail.com>
Subject: dma-buf: introduce initial file I/O infrastructure

This commit introduces the initial file I/O infrastructure for natively using
dma-buf in the read-write path. It adds building blocks like dma_buf_io_ctx
and dma_buf_io_map to serve as an interface between upper layer subsystems
and the importer driver. Operations like mapping and unmapping are delegated
to the importer driver via dma_buf_io_ops.

> diff --git a/drivers/dma-buf/dma-buf-io.c b/drivers/dma-buf/dma-buf-io.c
> new file mode 100644
> --- /dev/null
> +++ b/drivers/dma-buf/dma-buf-io.c

[ ... ]

> +static void dma_buf_io_kill_map(struct dma_buf_io_ctx *ctx)
> +{
> +	struct dma_buf_io_map *map;
> +
> +	dma_resv_assert_held(ctx->dmabuf->resv);
> +
> +	map = rcu_dereference_protected(ctx->map,
> +				dma_resv_held(ctx->dmabuf->resv));
> +	if (!map)
> +		return;
> +
> +	rcu_assign_pointer(ctx->map, NULL);
> +	percpu_ref_kill(&map->refs);
> +	/* make sure the map is not visible via ctx->map */
> +	synchronize_rcu_expedited();
> +	wait_for_completion(&map->drained);

[Severity: High]
Does this block indefinitely on hardware I/O completion while holding the
dmabuf->resv lock?

dma_buf_io_kill_map() is called from the invalidate_mappings callback. If the
exporter calls invalidate_mappings while holding the dma_resv lock during memory
reclaim, waiting synchronously for all in-flight I/O requests to drop their
references could cause an ABBA deadlock. Memory reclaim might try to acquire
the same dma_resv lock.

Additionally, dynamic importer rules dictate that invalidate_mappings must not
block indefinitely, but should instead detach or set fences so the exporter can
wait on them.

> +	dma_buf_io_map_release(map);
> +}

[ ... ]

> +static struct dma_buf_io_map *__dma_buf_io_create_map(struct dma_buf_io_ctx *ctx)
> +{
> +	struct dma_buf *dmabuf = ctx->dmabuf;
> +	struct dma_buf_io_map *map;
> +	long ret;
> +
> +	dma_resv_assert_held(dmabuf->resv);
> +
> +	if (ctx->is_dead)
> +		return ERR_PTR(-ENOENT);
> +
> +	map = __dma_buf_io_get_map(ctx);
> +	if (map)
> +		return map;
> +
> +	map = ctx->dev_ops->map(ctx);
> +	if (IS_ERR(map))
> +		return map;
> +
> +	if (WARN_ON_ONCE(!map->min_seg_shift))
> +		return ERR_PTR(-EFAULT);

[Severity: High]
Does this error path leak the map structure?

When map->min_seg_shift is zero, the function returns ERR_PTR directly. It
looks like this skips invoking dma_buf_io_map_release(map) or
ctx->dev_ops->unmap(ctx, map), which might permanently leak the map structure,
its initialized percpu_ref, and any associated hardware DMA mappings.

> +
> +	ret = dma_resv_wait_timeout(dmabuf->resv, DMA_RESV_USAGE_KERNEL,
> +				    true, MAX_SCHEDULE_TIMEOUT);

[Severity: High]
Can this wait cause a deadlock by blocking other lockers like shrinkers?

Since __dma_buf_io_create_map() holds the dma_resv lock, synchronously waiting
on dma_resv_wait_timeout() with MAX_SCHEDULE_TIMEOUT might block any other
dma_resv locker while waiting for hardware fences to signal.

> +	if (ret <= 0) {
> +		if (!ret)
> +			ret = -EAGAIN;
> +		dma_buf_io_map_release(map);
> +		return ERR_PTR(ret);
> +	}
> +
> +	/* get a reference for the caller */
> +	percpu_ref_get(&map->refs);
> +	rcu_assign_pointer(ctx->map, map);
> +	return map;
> +}

[ ... ]

> +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 = kzalloc_obj(*ctx);
> +	if (!ctx)
> +		return -ENOMEM;
> +	ctx->dir = dir;
> +	ctx->dmabuf = dmabuf;
> +	get_dma_buf(dmabuf);
> +
> +	ret = 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;

[Severity: High]
Does returning directly here leak the context and the dma_buf reference?

After allocating the context and getting a reference to dmabuf, if any of the
required device operations are missing, the function returns -EINVAL directly.
This appears to leave the allocated ctx unfreed and the dmabuf reference
unreleased, without calling any cleanup like kfree() or dma_buf_put(), nor any
driver-specific release.

> +
> +	*out_ctx = ctx;
> +	return 0;
> +}
> +
> +void dma_buf_io_ctx_release(struct dma_buf_io_ctx *ctx)
> +{
> +	dma_buf_io_ctx_shutdown(ctx);
> +
> +	if (WARN_ON_ONCE(rcu_dereference_protected(ctx->map, true)))
> +		return;

[Severity: Medium]
Does returning early from the destructor leak the context and pin the dma_buf?

If the ctx->map is still populated during release, returning early here skips
the final ctx->dev_ops->release(ctx), dma_buf_put(ctx->dmabuf), and
kfree(ctx). Teardown functions shouldn't silently leak resources when they
encounter an invalid state.

> +
> +	ctx->dev_ops->release(ctx);
> +	dma_buf_put(ctx->dmabuf);
> +	kfree(ctx);
> +}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1791924581.git.asml.silence@gmail.com?part=1

  reply	other threads:[~2026-10-13 21:06 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-13 20:54 [PATCH v10 00/13] Add dmabuf read/write via io_uring Pavel Begunkov
2026-10-13 20:54 ` [PATCH v10 01/13] dma-buf: introduce initial file I/O infrastructure Pavel Begunkov
2026-10-13 21:06   ` sashiko-bot [this message]
2026-10-13 20:54 ` [PATCH v10 02/13] iov_iter: add iterator type for dmabuf maps Pavel Begunkov
2026-10-13 21:13   ` sashiko-bot
2026-10-13 20:54 ` [PATCH v10 03/13] block: always adjust bi_offset on bio_advance_iter Pavel Begunkov
2026-10-13 20:54 ` [PATCH v10 04/13] block: introduce dma map backed bio type Pavel Begunkov
2026-10-13 20:54 ` [PATCH v10 05/13] block: add dma-buf support for raw bdev Pavel Begunkov
2026-10-13 20:54 ` [PATCH v10 06/13] nvme-pci: rename nvme_pci_sgl_set_data to nvme_pci_dma_iter_set_sgl Pavel Begunkov
2026-10-13 20:54 ` [PATCH v10 07/13] nvme-pci: implement dma-buf backed requests Pavel Begunkov
2026-10-13 21:23   ` sashiko-bot
2026-10-13 20:54 ` [PATCH v10 08/13] nvme-pci: add SGL support for the dmabuf path Pavel Begunkov
2026-10-13 20:54 ` [PATCH v10 09/13] io_uring/rsrc: introduce buf registration structure Pavel Begunkov
2026-10-13 20:54 ` [PATCH v10 10/13] io_uring/rsrc: extend buffer update Pavel Begunkov
2026-10-13 20:54 ` [PATCH v10 11/13] io_uring/rsrc: add uncloneable regbuf flag Pavel Begunkov
2026-10-13 20:54 ` [PATCH v10 12/13] io_uring/rsrc: add regbuf import flags Pavel Begunkov
2026-10-13 20:54 ` [PATCH v10 13/13] io_uring/rsrc: add dmabuf backed registered buffers Pavel Begunkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-170510@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=asml.silence@gmail.com \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=media-ci@linuxtv.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.