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

From: Huang, Ying
Date: Wed Jan 20 2021 - 02:57:00 EST


Michal Hocko <mhocko@xxxxxxxx> writes:

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

There's no error in the current code. But we will change the related
code in the future. So this checking will help us to prevent error in
the future. I will change the patch description to make it more clear.

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

Emm... The mistake to update swap shadow entries will hurt performance,
but will not trigger functionality bug. So it may be better to use
VM_WARN_ON_ONCE().

Best Regards,
Huang, Ying


>> Signed-off-by: "Huang, Ying" <ying.huang@xxxxxxxxx>
>> Cc: Minchan Kim <minchan@xxxxxxxxxx>
>> Cc: Joonsoo Kim <iamjoonsoo.kim@xxxxxxx>,
>> Cc: Johannes Weiner <hannes@xxxxxxxxxxx>,
>> Cc: Vlastimil Babka <vbabka@xxxxxxx>, Hugh Dickins <hughd@xxxxxxxxxx>,
>> Cc: Mel Gorman <mgorman@xxxxxxxxxxxxxxxxxxx>,
>> Cc: Michal Hocko <mhocko@xxxxxxxxxx>,
>> Cc: Dan Williams <dan.j.williams@xxxxxxxxx>,
>> Cc: Christoph Hellwig <hch@xxxxxx>, Ilya Dryomov <idryomov@xxxxxxxxx>,
>> ---
>> 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