Re: [PATCH] swap: Check nrexceptional of swap cache before being freed

Michal Hocko
2021-01-20T07:46:58Z

On Wed 20-01-21 15:27:11, Huang Ying wrote:
> To catch the error in updating the swap cache shadow entries or their count.

What is the error? Can it happens in the real life? Why do we need this
patch? Is crashing the kernel the right way to handle the situation?

> Signed-off-by: "Huang, Ying" <ying.huang@intel.com>
> Cc: Minchan Kim <minchan@kernel.org>
> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>,
> Cc: Johannes Weiner <hannes@cmpxchg.org>,
> Cc: Vlastimil Babka <vbabka@suse.cz>, Hugh Dickins <hughd@google.com>,
> Cc: Mel Gorman <mgorman@techsingularity.net>,
> Cc: Michal Hocko <mhocko@kernel.org>,
> Cc: Dan Williams <dan.j.williams@intel.com>,
> Cc: Christoph Hellwig <hch@lst.de>, Ilya Dryomov <idryomov@gmail.com>,
> ---
> mm/swap_state.c | 7 ++++++-
> 1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/mm/swap_state.c b/mm/swap_state.c
> index d0d417efeecc..240a4f97594a 100644
> --- a/mm/swap_state.c
> +++ b/mm/swap_state.c
> @@ -703,7 +703,12 @@ int init_swap_address_space(unsigned int type, unsigned long nr_pages)
>
> void exit_swap_address_space(unsigned int type)
> {
> - kvfree(swapper_spaces[type]);
> + int i;
> + struct address_space *spaces = swapper_spaces[type];
> +
> + for (i = 0; i < nr_swapper_spaces[type]; i++)
> + VM_BUG_ON(spaces[i].nrexceptional);
> + kvfree(spaces);
> nr_swapper_spaces[type] = 0;
> swapper_spaces[type] = NULL;
> }
> --
> 2.29.2

--
Michal Hocko
SUSE Labs