All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Nuno Sá" <noname.nuno@gmail.com>
To: Paul Cercueil <paul@crapouillou.net>
Cc: "Greg Kroah-Hartman" <gregkh@linuxfoundation.org>,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Nuno Sá" <nuno.sa@analog.com>,
	stable@vger.kernel.org
Subject: Re: [PATCH] usb: gadget: f_fs: Fix DMA fence leak
Date: Tue, 9 Jun 2026 17:02:32 +0100	[thread overview]
Message-ID: <aig48OoC1vkdRMYL@nsa> (raw)
In-Reply-To: <20260609152905.729328-1-paul@crapouillou.net>

On Tue, Jun 09, 2026 at 05:29:05PM +0200, Paul Cercueil wrote:
> In ffs_dmabuf_transfer(), a ffs_dma_fence object is kmalloc'd, with the
> underlying dma_fence later initialized by dma_fence_init(), which sets
> its kref counter to 1. Then, dma_resv_add_fence() gets a second
> reference, and a pointer to the ffs_dma_fence is passed as the
> usb_request's "context" field.
> 
> The dma-resv mechanism will manage the second reference, but the first
> reference is never properly released; the ffs_dmabuf_cleanup() function
> decreases the reference count, but only to balance with the reference
> grab in ffs_dmabuf_signal_done().
> 
> The code will then slowly leak memory as more ffs_dma_fence objects are
> created without being ever freed.
> 
> Address this issue by transferring ownership of the fence to the DMA
> reservation object, by calling dma_fence_put() right after
> dma_resv_add_fence(). The ffs_dma_fence then gets properly discarded
> after being signalled.
> 
> Fixes: 7b07a2a7ca02 ("usb: gadget: functionfs: Add DMABUF import interface")
> Cc: stable@vger.kernel.org
> Signed-off-by: Paul Cercueil <paul@crapouillou.net>
> ---

Tested-by: Nuno Sá <nuno.sa@analog.com>
Reviewed-by: Nuno Sá <nuno.sa@analog.com>

>  drivers/usb/gadget/function/f_fs.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c
> index 75912ce6ab55..7cc446502980 100644
> --- a/drivers/usb/gadget/function/f_fs.c
> +++ b/drivers/usb/gadget/function/f_fs.c
> @@ -1704,6 +1704,7 @@ static int ffs_dmabuf_transfer(struct file *file,
>  	resv_dir = epfile->in ? DMA_RESV_USAGE_READ : DMA_RESV_USAGE_WRITE;
>  
>  	dma_resv_add_fence(dmabuf->resv, &fence->base, resv_dir);
> +	dma_fence_put(&fence->base);
>  	dma_resv_unlock(dmabuf->resv);
>  
>  	/* Now that the dma_fence is in place, queue the transfer. */
> -- 
> 2.53.0
> 

      reply	other threads:[~2026-06-09 16:01 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-09 15:29 [PATCH] usb: gadget: f_fs: Fix DMA fence leak Paul Cercueil
2026-06-09 16:02 ` Nuno Sá [this message]

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=aig48OoC1vkdRMYL@nsa \
    --to=noname.nuno@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=paul@crapouillou.net \
    --cc=stable@vger.kernel.org \
    /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.