Re: [PATCH] usb: gadget: f_fs: Fix DMA fence leak

From: Nuno Sá

Date: Tue Jun 09 2026 - 12:17:48 EST


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@xxxxxxxxxxxxxxx
> Signed-off-by: Paul Cercueil <paul@xxxxxxxxxxxxxxx>
> ---

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

> 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
>