Skip to contentSkip to conversation

swap: Check nrexceptional of swap cache before being freed

PATCH In review Huang Ying 3 participants 6 messages Jan 20, 2021 – Jan 22, 2021

Subsystems (2)
lore
Share & tools
lore

Huang Ying

author
#0 lore raw

PATCH 1/1 swap: Check nrexceptional of swap cache before being freed

To catch the error in updating the swap cache shadow entries or their count.

Cc Minchan Kim <[email protected]>
Cc Joonsoo Kim <[email protected]>,
Cc Johannes Weiner <[email protected]>,
Cc Vlastimil Babka <[email protected]>, Hugh Dickins <[email protected]>,
Cc Mel Gorman <[email protected]>,
Cc Michal Hocko <[email protected]>,
Cc Dan Williams <[email protected]>,
Cc Christoph Hellwig <[email protected]>, Ilya Dryomov <[email protected]>,
---
 mm/swap_state.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
mm/swap_state.c +6 -1
index d0d417efeecc..240a4f97594a 100644
@@ -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;
 }
Signature · 1 line
2.29.2
#1 lore raw
Quoted 1 line · Huang Ying

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?

Quoted 33 lines
Cc Minchan Kim <[email protected]>
Cc Joonsoo Kim <[email protected]>,
Cc Johannes Weiner <[email protected]>,
Cc Vlastimil Babka <[email protected]>, Hugh Dickins <[email protected]>,
Cc Mel Gorman <[email protected]>,
Cc Michal Hocko <[email protected]>,
Cc Dan Williams <[email protected]>,
Cc Christoph Hellwig <[email protected]>, Ilya Dryomov <[email protected]>,
---
 mm/swap_state.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
mm/swap_state.c +6 -1
index d0d417efeecc..240a4f97594a 100644
@@ -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;
 }
Signature · 1 line
2.29.2
Signature · 2 lines
Michal Hocko
SUSE Labs
#2 lore raw
Quoted 4 lines · Michal Hocko
Quoted 1 line · Huang Ying

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.

Quoted 2 lines

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

Quoted 33 lines
Quoted 33 lines
Cc Minchan Kim <[email protected]>
Cc Joonsoo Kim <[email protected]>,
Cc Johannes Weiner <[email protected]>,
Cc Vlastimil Babka <[email protected]>, Hugh Dickins <[email protected]>,
Cc Mel Gorman <[email protected]>,
Cc Michal Hocko <[email protected]>,
Cc Dan Williams <[email protected]>,
Cc Christoph Hellwig <[email protected]>, Ilya Dryomov <[email protected]>,
---
 mm/swap_state.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
mm/swap_state.c +6 -1
index d0d417efeecc..240a4f97594a 100644
@@ -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;
 }
Signature · 1 line
2.29.2
#3 lore raw
Quoted 11 lines · Ying
Quoted 4 lines · Michal Hocko
Quoted 1 line · Huang Ying

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.

Quoted 6 lines
Quoted 2 lines

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().

Yes a warning is much more appropriate approach. The question is whether a test like this is really necessary. But I will leave that to others to decide. It was really the bug on that hit my eyes.

Signature · 2 lines
Michal Hocko
SUSE Labs
#4 lore raw
In reply to Huang Ying
Quoted 1 line · Huang Ying

To catch the error in updating the swap cache shadow entries or their count.

I just resent a patch that removes nrexceptional tracking.

Can you use !mapping_empty() instead?

Quoted 16 lines

void exit_swap_address_space(unsigned int type)
{

mm/swap_state.c +6 -1
-	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;
 }
Signature · 1 line
2.29.2
#5 lore raw
Quoted 6 lines · Matthew Wilcox
Quoted 1 line · Huang Ying

To catch the error in updating the swap cache shadow entries or their count.

I just resent a patch that removes nrexceptional tracking.

Can you use !mapping_empty() instead?

Sure. Will use that in the next version.

Best Regards,
Huang, Ying

Quoted 16 lines
Quoted 16 lines

void exit_swap_address_space(unsigned int type)
{

mm/swap_state.c +6 -1
-	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;
 }
Signature · 1 line
2.29.2