Re: [PATCH] rust: drm: gem: shmem: do not leak pages when sg_table() fails

From: Sami Tolvanen

Date: Tue Oct 20 2026 - 16:16:59 EST


On Tue, Oct 20, 2026 at 5:31 AM Gary Guo <gary@xxxxxxxxxxx> wrote:
>
> On Mon Oct 19, 2026 at 9:21 PM BST, Sami Tolvanen via B4 Relay wrote:
> > From: Sami Tolvanen <samitolvanen@xxxxxxxxxx>
> >
> > `SGTableMap::new()` takes a pages reference and maps the table before
> > `Devres::new()` allocates. If the allocation fails, dropping the map
> > frees the table but keeps the reference, and the next `sg_table()`
> > takes another one. `drm_gem_shmem_release()` puts only one, so the
> > pages leak.
>
> Sorry, could you explain it in more detail?
>
> Creating a SGTableMap and dropping it doesn't restore the state? If so, it
> sounds like an API design issue.

Correct, SGTableMap::new() increments pages_use_count, but
__drm_gem_shmem_free_sgt_locked() doesn't decrement it in
SGTableMap::drop(). The count is only decremented when the object is
freed, in drm_gem_shmem_release(). This works unless Devres::new()
fails here and sg_table() is called again. I'm not sure if there's
some background that led to this design, but I do agree that it
probably could be more intuitive.

Sami