Re: [PATCH -mm] mm, swap: Fix swap space leak in error path of swap_free_entries()

From: Tim Chen
Date: Tue Apr 25 2017 - 18:05:48 EST


On Tue, 2017-04-25 at 14:37 -0700, Andrew Morton wrote:
> On Fri, 21 Apr 2017 20:47:39 +0800 "Huang, Ying" <ying.huang@xxxxxxxxx> wrote:
>
> >
> > From: Huang Ying <ying.huang@xxxxxxxxx>
> >
> > In swapcache_free_entries(), if swap_info_get_cont() return NULL,
> > something wrong occurs for the swap entry.ÂÂBut we should still
> > continue to free the following swap entries in the array instead of
> > skip them to avoid swap space leak.ÂÂThis is just problem in error
> > path, where system may be in an inconsistent state, but it is still
> > good to fix it.
> >
> > ...
> >
> > --- a/mm/swapfile.c
> > +++ b/mm/swapfile.c
> > @@ -1079,8 +1079,6 @@ void swapcache_free_entries(swp_entry_t *entries, int n)
> > Â p = swap_info_get_cont(entries[i], prev);
> > Â if (p)
> > Â swap_entry_free(p, entries[i]);
> > - else
> > - break;
> > Â prev = p;
> So now prev==NULL.ÂÂWill this code get the locking correct in
> swap_info_get_cont()?ÂÂI think so, but please double-check.
>

There are 4 possible cases, and I checked that the logic
in swap_info_get_cont do the expected:

entries[i]
valid? prev  Expected swap_info_get_cont behavior
---------------------------------------------------------------------
NO NULL Return NULL p, Do nothing on lock/unlock
NO NON-NULL Return NULL p, Unlock prevÂ
YES NULL Return non-NULL p, lock p
YES NON-NULL Return non-NULL p, (p != prev) unlock prev and lock pÂ
 Â(p == prev) do nothing on lock/unlock

Thanks.

Tim

>