Skip to contentSkip to conversation

mm: fix special swap entry handling on copy mm

PATCH In review [email protected] 3 participants 5 messages Aug 12, 2013 – Aug 13, 2013

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: fix special swap entry handling on copy mm

From: Jerome Glisse <[email protected]>

Prior to this copy_one_pte will never reach the special swap file handling code because swap_duplicate will return invalid value.

Note this is not fatal so nothing bad ever happen because of that. Reason is that copy_pte_range would break of its loop and call add_swap_count_continuation which would see its a special swap file and return 0 triggering copy_pte_range to try again. Because we try again there is a huge chance that the temporarily special migration pte is now again valid and pointing to a new valid page.

This patch just split handling of special swap entry from regular one inside copy_one_pte.

(Note i spotted that while reading code i haven't tested my theory.)

---
 mm/memory.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
mm/memory.c +13 -13
index 1ce2e2a..9f907dd 100644
@@ -833,20 +833,20 @@ copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
 		if (!pte_file(pte)) {
 			swp_entry_t entry = pte_to_swp_entry(pte);
 
-			if (swap_duplicate(entry) < 0)
-				return entry.val;
-
-			/* make sure dst_mm is on swapoff's mmlist. */
-			if (unlikely(list_empty(&dst_mm->mmlist))) {
-				spin_lock(&mmlist_lock);
-				if (list_empty(&dst_mm->mmlist))
-					list_add(&dst_mm->mmlist,
-						 &src_mm->mmlist);
-				spin_unlock(&mmlist_lock);
-			}
-			if (likely(!non_swap_entry(entry)))
+			if (likely(!non_swap_entry(entry))) {
+				if (swap_duplicate(entry) < 0)
+					return entry.val;
+
+				/* make sure dst_mm is on swapoff's mmlist. */
+				if (unlikely(list_empty(&dst_mm->mmlist))) {
+					spin_lock(&mmlist_lock);
+					if (list_empty(&dst_mm->mmlist))
+						list_add(&dst_mm->mmlist,
+							 &src_mm->mmlist);
+					spin_unlock(&mmlist_lock);
+				}
 				rss[MM_SWAPENTS]++;
-			else if (is_migration_entry(entry)) {
+			} else if (is_migration_entry(entry)) {
 				page = migration_entry_to_page(entry);
 
 				if (PageAnon(page))
Signature · 7 lines
1.8.3.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw

Hi Jerome,

Quoted 12 lines · On Mon, Aug 12, 2013 at 11:43:24AM -0400, [email protected] wrote:

From: Jerome Glisse <[email protected]>

Prior to this copy_one_pte will never reach the special swap file handling code because swap_duplicate will return invalid value.

Note this is not fatal so nothing bad ever happen because of that. Reason is that copy_pte_range would break of its loop and call add_swap_count_continuation which would see its a special swap file and return 0 triggering copy_pte_range to try again. Because we try again there is a huge chance that the temporarily special migration pte is now again valid and pointing to a new valid page.

Quoted 6 lines

This patch just split handling of special swap entry from regular one inside copy_one_pte.

(Note i spotted that while reading code i haven't tested my theory.)

non_swap_entry() means not only migration entry, but also hwpoison entry, so it seems to me that simply moving the swap_duplicate() into the if(!non_swap_entry) block can change the behavior for hwpoison entry. Would it be nice to add check for such a case?

Thanks,
Naoya Horiguchi

Quoted 51 lines
---
 mm/memory.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
mm/memory.c +13 -13
index 1ce2e2a..9f907dd 100644
@@ -833,20 +833,20 @@ copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
 		if (!pte_file(pte)) {
 			swp_entry_t entry = pte_to_swp_entry(pte);
 
-			if (swap_duplicate(entry) < 0)
-				return entry.val;
-
-			/* make sure dst_mm is on swapoff's mmlist. */
-			if (unlikely(list_empty(&dst_mm->mmlist))) {
-				spin_lock(&mmlist_lock);
-				if (list_empty(&dst_mm->mmlist))
-					list_add(&dst_mm->mmlist,
-						 &src_mm->mmlist);
-				spin_unlock(&mmlist_lock);
-			}
-			if (likely(!non_swap_entry(entry)))
+			if (likely(!non_swap_entry(entry))) {
+				if (swap_duplicate(entry) < 0)
+					return entry.val;
+
+				/* make sure dst_mm is on swapoff's mmlist. */
+				if (unlikely(list_empty(&dst_mm->mmlist))) {
+					spin_lock(&mmlist_lock);
+					if (list_empty(&dst_mm->mmlist))
+						list_add(&dst_mm->mmlist,
+							 &src_mm->mmlist);
+					spin_unlock(&mmlist_lock);
+				}
 				rss[MM_SWAPENTS]++;
-			else if (is_migration_entry(entry)) {
+			} else if (is_migration_entry(entry)) {
 				page = migration_entry_to_page(entry);
 
 				if (PageAnon(page))
Signature · 7 lines
1.8.3.1

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#2 lore raw
Quoted 22 lines · Naoya Horiguchi

Hi Jerome,

Quoted 12 lines · On Mon, Aug 12, 2013 at 11:43:24AM -0400, [email protected] wrote:

From: Jerome Glisse <[email protected]>

Prior to this copy_one_pte will never reach the special swap file handling code because swap_duplicate will return invalid value.

Note this is not fatal so nothing bad ever happen because of that. Reason is that copy_pte_range would break of its loop and call add_swap_count_continuation which would see its a special swap file and return 0 triggering copy_pte_range to try again. Because we try again there is a huge chance that the temporarily special migration pte is now again valid and pointing to a new valid page.

Quoted 6 lines

This patch just split handling of special swap entry from regular one inside copy_one_pte.

(Note i spotted that while reading code i haven't tested my theory.)

Quoted 7 lines

non_swap_entry() means not only migration entry, but also hwpoison entry, so it seems to me that simply moving the swap_duplicate() into the if(!non_swap_entry) block can change the behavior for hwpoison entry. Would it be nice to add check for such a case?

Thanks,
Naoya Horiguchi

Well if my reading of the code is right for hwpoison entry current code will loop indefinitly inside the kernel on fork if one entry is set to hwpoison.

My patch does not handle hwpoison because it seems useless as there is nothing to do for hwpoison pte beside giving setting the new pte to hwpoison to. So the fork child will also have a pte with hwpoison. My patch do just that.

So change in behavior is current kernel loop indefinitly in kernel with hwpoison pte on fork, vs child get hwpoison pte with my patch. Meaning that both child and father can live as long as they dont access the hwpoisoned ptes.

Cheers,
Jerome

Quoted 46 lines
Quoted 46 lines
---
 mm/memory.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
mm/memory.c +13 -13
index 1ce2e2a..9f907dd 100644
@@ -833,20 +833,20 @@ copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
 		if (!pte_file(pte)) {
 			swp_entry_t entry = pte_to_swp_entry(pte);
 
-			if (swap_duplicate(entry) < 0)
-				return entry.val;
-
-			/* make sure dst_mm is on swapoff's mmlist. */
-			if (unlikely(list_empty(&dst_mm->mmlist))) {
-				spin_lock(&mmlist_lock);
-				if (list_empty(&dst_mm->mmlist))
-					list_add(&dst_mm->mmlist,
-						 &src_mm->mmlist);
-				spin_unlock(&mmlist_lock);
-			}
-			if (likely(!non_swap_entry(entry)))
+			if (likely(!non_swap_entry(entry))) {
+				if (swap_duplicate(entry) < 0)
+					return entry.val;
+
+				/* make sure dst_mm is on swapoff's mmlist. */
+				if (unlikely(list_empty(&dst_mm->mmlist))) {
+					spin_lock(&mmlist_lock);
+					if (list_empty(&dst_mm->mmlist))
+						list_add(&dst_mm->mmlist,
+							 &src_mm->mmlist);
+					spin_unlock(&mmlist_lock);
+				}
 				rss[MM_SWAPENTS]++;
-			else if (is_migration_entry(entry)) {
+			} else if (is_migration_entry(entry)) {
 				page = migration_entry_to_page(entry);
 
 				if (PageAnon(page))
Signature · 1 line
1.8.3.1
Quoted 6 lines
Quoted 6 lines

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw
Quoted 28 lines · Jerome Glisse
Quoted 27 lines · Naoya Horiguchi

Hi Jerome,

Quoted 12 lines · On Mon, Aug 12, 2013 at 11:43:24AM -0400, [email protected] wrote:

From: Jerome Glisse <[email protected]>

Prior to this copy_one_pte will never reach the special swap file handling code because swap_duplicate will return invalid value.

Note this is not fatal so nothing bad ever happen because of that. Reason is that copy_pte_range would break of its loop and call add_swap_count_continuation which would see its a special swap file and return 0 triggering copy_pte_range to try again. Because we try again there is a huge chance that the temporarily special migration pte is now again valid and pointing to a new valid page.

Quoted 6 lines

This patch just split handling of special swap entry from regular one inside copy_one_pte.

(Note i spotted that while reading code i haven't tested my theory.)

non_swap_entry() means not only migration entry, but also hwpoison entry, so it seems to me that simply moving the swap_duplicate() into the if(!non_swap_entry) block can change the behavior for hwpoison entry. Would it be nice to add check for such a case?

Quoted 5 lines
Quoted 2 lines

Thanks,
Naoya Horiguchi

Well if my reading of the code is right for hwpoison entry current code will loop indefinitly inside the kernel on fork if one entry is set to hwpoison.

(Sorry if I missed something, but) __swap_duplicate always returns -EINVAL if non_swap_entry is true and then swap_duplicate returns 0. So copy_one_pte() doesn't return at if (swap_duplicate(entry) < 0) block for non_swap_entry. So ...

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Prior to this copy_one_pte will never reach the special swap file handling code because swap_duplicate will return invalid value.

this seems not correct to me.
Could you explain more about this point?
(Maybe I don't understand the terminology "special swap file"...)

Quoted 3 lines

My patch does not handle hwpoison because it seems useless as there is nothing to do for hwpoison pte beside giving setting the new pte to hwpoison to. So the fork child will also have a pte with hwpoison. My patch do just that.

Yes, just copying hwpoison entry looks a right behavior to me.

Thanks,
Naoya Horiguchi

Quoted 50 lines

So change in behavior is current kernel loop indefinitly in kernel with hwpoison pte on fork, vs child get hwpoison pte with my patch. Meaning that both child and father can live as long as they dont access the hwpoisoned ptes.

Quoted 46 lines
Quoted 46 lines
---
 mm/memory.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
mm/memory.c +13 -13
index 1ce2e2a..9f907dd 100644
@@ -833,20 +833,20 @@ copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
 		if (!pte_file(pte)) {
 			swp_entry_t entry = pte_to_swp_entry(pte);
 
-			if (swap_duplicate(entry) < 0)
-				return entry.val;
-
-			/* make sure dst_mm is on swapoff's mmlist. */
-			if (unlikely(list_empty(&dst_mm->mmlist))) {
-				spin_lock(&mmlist_lock);
-				if (list_empty(&dst_mm->mmlist))
-					list_add(&dst_mm->mmlist,
-						 &src_mm->mmlist);
-				spin_unlock(&mmlist_lock);
-			}
-			if (likely(!non_swap_entry(entry)))
+			if (likely(!non_swap_entry(entry))) {
+				if (swap_duplicate(entry) < 0)
+					return entry.val;
+
+				/* make sure dst_mm is on swapoff's mmlist. */
+				if (unlikely(list_empty(&dst_mm->mmlist))) {
+					spin_lock(&mmlist_lock);
+					if (list_empty(&dst_mm->mmlist))
+						list_add(&dst_mm->mmlist,
+							 &src_mm->mmlist);
+					spin_unlock(&mmlist_lock);
+				}
 				rss[MM_SWAPENTS]++;
-			else if (is_migration_entry(entry)) {
+			} else if (is_migration_entry(entry)) {
 				page = migration_entry_to_page(entry);
 
 				if (PageAnon(page))
Signature · 1 line
1.8.3.1
Quoted 13 lines
Quoted 6 lines
Quoted 6 lines

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#4 lore raw
Quoted 32 lines · Naoya Horiguchi
Quoted 31 lines · Jerome Glisse
Quoted 29 lines · Naoya Horiguchi

Hi Jerome,

Quoted 12 lines · On Mon, Aug 12, 2013 at 11:43:24AM -0400, [email protected] wrote:

From: Jerome Glisse <[email protected]>

Prior to this copy_one_pte will never reach the special swap file handling code because swap_duplicate will return invalid value.

Note this is not fatal so nothing bad ever happen because of that. Reason is that copy_pte_range would break of its loop and call add_swap_count_continuation which would see its a special swap file and return 0 triggering copy_pte_range to try again. Because we try again there is a huge chance that the temporarily special migration pte is now again valid and pointing to a new valid page.

Quoted 6 lines

This patch just split handling of special swap entry from regular one inside copy_one_pte.

(Note i spotted that while reading code i haven't tested my theory.)

non_swap_entry() means not only migration entry, but also hwpoison entry, so it seems to me that simply moving the swap_duplicate() into the if(!non_swap_entry) block can change the behavior for hwpoison entry. Would it be nice to add check for such a case?

Thanks,
Naoya Horiguchi

Quoted 7 lines
Quoted 2 lines

Well if my reading of the code is right for hwpoison entry current code will loop indefinitly inside the kernel on fork if one entry is set to hwpoison.

(Sorry if I missed something, but) __swap_duplicate always returns -EINVAL if non_swap_entry is true and then swap_duplicate returns 0. So copy_one_pte() doesn't return at if (swap_duplicate(entry) < 0) block for non_swap_entry. So ...

No, it's my fault i didn't pay attention to swap_duplicate only to __swap_duplicate, also means my bug
is elsewhere.

Thanks for looking.
Cheers,
Jerome

Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

Prior to this copy_one_pte will never reach the special swap file handling code because swap_duplicate will return invalid value.

this seems not correct to me.
Could you explain more about this point?
(Maybe I don't understand the terminology "special swap file"...)

Quoted 72 lines
Quoted 3 lines

My patch does not handle hwpoison because it seems useless as there is nothing to do for hwpoison pte beside giving setting the new pte to hwpoison to. So the fork child will also have a pte with hwpoison. My patch do just that.

Yes, just copying hwpoison entry looks a right behavior to me.

Thanks,
Naoya Horiguchi

Quoted 63 lines

So change in behavior is current kernel loop indefinitly in kernel with hwpoison pte on fork, vs child get hwpoison pte with my patch. Meaning that both child and father can live as long as they dont access the hwpoisoned ptes.

Quoted 46 lines
Quoted 46 lines
---
 mm/memory.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)
mm/memory.c +13 -14
index 1ce2e2a..9f907dd 100644
@@ -833,20 +833,20 @@ copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm,
          if (!pte_file(pte)) {
                  swp_entry_t entry = pte_to_swp_entry(pte);
 
-                 if (swap_duplicate(entry) < 0)
-                         return entry.val;
-
-                 /* make sure dst_mm is on swapoff's mmlist. */
-                 if (unlikely(list_empty(&dst_mm->mmlist))) {
-                         spin_lock(&mmlist_lock);
-                         if (list_empty(&dst_mm->mmlist))
-                                 list_add(&dst_mm->mmlist,
-                                          &src_mm->mmlist);
-                         spin_unlock(&mmlist_lock);
-                 }
-                 if (likely(!non_swap_entry(entry)))
+                 if (likely(!non_swap_entry(entry))) {
+                         if (swap_duplicate(entry) < 0)
+                                 return entry.val;
+
+                         /* make sure dst_mm is on swapoff's mmlist. */
+                         if (unlikely(list_empty(&dst_mm->mmlist))) {
+                                 spin_lock(&mmlist_lock);
+                                 if (list_empty(&dst_mm->mmlist))
+                                         list_add(&dst_mm->mmlist,
+                                                  &src_mm->mmlist);
+                                 spin_unlock(&mmlist_lock);
+                         }
                          rss[MM_SWAPENTS]++;
-                 else if (is_migration_entry(entry)) {
+                 } else if (is_migration_entry(entry)) {
                          page = migration_entry_to_page(entry);
 
                          if (PageAnon(page))
--

1.8.3.1

Quoted 6 lines
Quoted 6 lines

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>