Re: [PATCH] [v2] nvdimm-btt: fix several memleaks

From: Ira Weiny
Date: Fri Dec 15 2023 - 11:47:00 EST


Ira Weiny wrote:
> Dinghao Liu wrote:

[snip]

> >
> > -static int btt_maplocks_init(struct arena_info *arena)
> > +static int btt_maplocks_init(struct device *dev, struct arena_info *arena)
> > {
> > u32 i;
> >
> > - arena->map_locks = kcalloc(arena->nfree, sizeof(struct aligned_lock),
> > + arena->map_locks = devm_kcalloc(dev, arena->nfree, sizeof(struct aligned_lock),
> > GFP_KERNEL);
> > if (!arena->map_locks)
> > return -ENOMEM;
> > @@ -805,9 +805,6 @@ static void free_arenas(struct btt *btt)
> >
> > list_for_each_entry_safe(arena, next, &btt->arena_list, list) {
> > list_del(&arena->list);
> > - kfree(arena->rtt);
> > - kfree(arena->map_locks);
> > - kfree(arena->freelist);
>
> This does not quite work.
>
> free_arenas() is used in the error paths of create_arenas() and
> discover_arenas(). In those cases devm_kfree() is probably a better way
> to clean up this.
>
> However...
>
> > debugfs_remove_recursive(arena->debugfs_dir);
> > kfree(arena);
>
> Why can't arena be allocated with devm_*?
>
> We need to change this up a bit more to handle the error path vs regular
> device shutdown free (automatic devm frees).

We might want to look at using no_free_ptr() in this code. See this
patch[1] for an example of how to inhibit the cleanup and pass the pointer
on when the function succeeds.

[1] https://lore.kernel.org/all/170261791914.1714654.6447680285357545638.stgit@xxxxxxxxxxxxxxxxxxxxxxxxx/

Ira