Skip to contentSkip to conversation

mm, swap: Fix false error message in __swp_swapcount()

PATCH In review Huang, Ying 3 participants 11 messages Oct 27, 2017 – Oct 31, 2017

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

PATCH 1/1 mm, swap: Fix false error message in __swp_swapcount()

From: Huang Ying <[email protected]>

When a page fault occurs for a swap entry, the physical swap readahead (not the VMA base swap readahead) may readahead several swap entries after the fault swap entry. The readahead algorithm calculates some of the swap entries to readahead via increasing the offset of the fault swap entry without checking whether they are beyond the end of the swap device and it relys on the __swp_swapcount() and swapcache_prepare() to check it. Although __swp_swapcount() checks for the swap entry passed in, it will complain with the error message as follow for the expected invalid swap entry. This may make the end users confused.

swap_info_get: Bad swap offset entry 0200f8a7

To fix the false error message, the swap entry checking is added in swap readahead to avoid to pass the out-bound swap entries and the swap entry reserved for the swap header to __swp_swapcount() and swapcache_prepare().

Cc Tim Chen <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Michal Hocko <[email protected]>
Cc <[email protected]> # 4.11-4.13
Fixes e8c26ab60598 ("mm/swap: skip readahead for unreferenced swap slots")
---
 include/linux/swap.h |  1 +
 mm/swap_state.c      |  6 ++++--
 mm/swapfile.c        | 21 +++++++++++++++++++++
 3 files changed, 26 insertions(+), 2 deletions(-)
include/linux/swap.h +1 -0
index 84255b3da7c1..43b4b821c805 100644
@@ -476,6 +476,7 @@ extern int page_swapcount(struct page *);
 extern int __swap_count(struct swap_info_struct *si, swp_entry_t entry);
 extern int __swp_swapcount(swp_entry_t entry);
 extern int swp_swapcount(swp_entry_t entry);
+extern bool swap_entry_check(swp_entry_t entry);
 extern struct swap_info_struct *page_swap_info(struct page *);
 extern struct swap_info_struct *swp_swap_info(swp_entry_t entry);
 extern bool reuse_swap_page(struct page *, int *);
mm/swap_state.c +4 -2
index 6c017ced11e6..7dd70e77058d 100644
@@ -569,11 +569,13 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
 	/* Read a page_cluster sized and aligned cluster around offset. */
 	start_offset = offset & ~mask;
 	end_offset = offset | mask;
-	if (!start_offset)	/* First page is swap header. */
-		start_offset++;
 
 	blk_start_plug(&plug);
 	for (offset = start_offset; offset <= end_offset ; offset++) {
+		swp_entry_t ent = swp_entry(swp_type(entry), offset);
+
+		if (!swap_entry_check(ent))
+			continue;
 		/* Ok, do the async read-ahead now */
 		page = __read_swap_cache_async(
 			swp_entry(swp_type(entry), offset),
mm/swapfile.c +21 -0
index 3074b02eaa09..b04cec29c234 100644
@@ -1107,6 +1107,27 @@ static struct swap_info_struct *swap_info_get_cont(swp_entry_t entry,
 	return p;
 }
 
+bool swap_entry_check(swp_entry_t entry)
+{
+	struct swap_info_struct *p;
+	unsigned long offset, type;
+
+	type = swp_type(entry);
+	if (type >= nr_swapfiles)
+		goto bad_file;
+	p = swap_info[type];
+	offset = swp_offset(entry);
+	if (unlikely(!offset || offset >= p->max))
+		goto out;
+
+	return true;
+
+bad_file:
+	pr_err("%s: %s%08lx\n", __func__, Bad_file, entry.val);
+out:
+	return false;
+}
+
 static unsigned char __swap_entry_free(struct swap_info_struct *p,
 				       swp_entry_t entry, unsigned char usage)
 {
Signature · 7 lines
2.14.2

--
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
Quoted 13 lines · Ying

From: Huang Ying <[email protected]>

When a page fault occurs for a swap entry, the physical swap readahead (not the VMA base swap readahead) may readahead several swap entries after the fault swap entry. The readahead algorithm calculates some of the swap entries to readahead via increasing the offset of the fault swap entry without checking whether they are beyond the end of the swap device and it relys on the __swp_swapcount() and swapcache_prepare() to check it. Although __swp_swapcount() checks for the swap entry passed in, it will complain with the error message as follow for the expected invalid swap entry. This may make the end users confused.

Quoted 6 lines

swap_info_get: Bad swap offset entry 0200f8a7

To fix the false error message, the swap entry checking is added in swap readahead to avoid to pass the out-bound swap entries and the swap entry reserved for the swap header to __swp_swapcount() and swapcache_prepare().

I have lost an overview in the swap code after recent changes so I cannot really give you a responsible ack but this looks much better than the previous attempt. So it looks good at first sight.

Quoted 80 lines
Cc Tim Chen <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Michal Hocko <[email protected]>
Cc <[email protected]> # 4.11-4.13
Fixes e8c26ab60598 ("mm/swap: skip readahead for unreferenced swap slots")
---
 include/linux/swap.h |  1 +
 mm/swap_state.c      |  6 ++++--
 mm/swapfile.c        | 21 +++++++++++++++++++++
 3 files changed, 26 insertions(+), 2 deletions(-)
include/linux/swap.h +1 -0
index 84255b3da7c1..43b4b821c805 100644
@@ -476,6 +476,7 @@ extern int page_swapcount(struct page *);
 extern int __swap_count(struct swap_info_struct *si, swp_entry_t entry);
 extern int __swp_swapcount(swp_entry_t entry);
 extern int swp_swapcount(swp_entry_t entry);
+extern bool swap_entry_check(swp_entry_t entry);
 extern struct swap_info_struct *page_swap_info(struct page *);
 extern struct swap_info_struct *swp_swap_info(swp_entry_t entry);
 extern bool reuse_swap_page(struct page *, int *);
mm/swap_state.c +4 -2
index 6c017ced11e6..7dd70e77058d 100644
@@ -569,11 +569,13 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
 	/* Read a page_cluster sized and aligned cluster around offset. */
 	start_offset = offset & ~mask;
 	end_offset = offset | mask;
-	if (!start_offset)	/* First page is swap header. */
-		start_offset++;
 
 	blk_start_plug(&plug);
 	for (offset = start_offset; offset <= end_offset ; offset++) {
+		swp_entry_t ent = swp_entry(swp_type(entry), offset);
+
+		if (!swap_entry_check(ent))
+			continue;
 		/* Ok, do the async read-ahead now */
 		page = __read_swap_cache_async(
 			swp_entry(swp_type(entry), offset),
mm/swapfile.c +21 -0
index 3074b02eaa09..b04cec29c234 100644
@@ -1107,6 +1107,27 @@ static struct swap_info_struct *swap_info_get_cont(swp_entry_t entry,
 	return p;
 }
 
+bool swap_entry_check(swp_entry_t entry)
+{
+	struct swap_info_struct *p;
+	unsigned long offset, type;
+
+	type = swp_type(entry);
+	if (type >= nr_swapfiles)
+		goto bad_file;
+	p = swap_info[type];
+	offset = swp_offset(entry);
+	if (unlikely(!offset || offset >= p->max))
+		goto out;
+
+	return true;
+
+bad_file:
+	pr_err("%s: %s%08lx\n", __func__, Bad_file, entry.val);
+out:
+	return false;
+}
+
 static unsigned char __swap_entry_free(struct swap_info_struct *p,
 				       swp_entry_t entry, unsigned char usage)
 {
Signature · 1 line
2.14.2
Signature · 8 lines
Michal Hocko
SUSE Labs

--
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
In reply to Huang, Ying

Hi Huang,

Quoted 99 lines · Ying

From: Huang Ying <[email protected]>

When a page fault occurs for a swap entry, the physical swap readahead (not the VMA base swap readahead) may readahead several swap entries after the fault swap entry. The readahead algorithm calculates some of the swap entries to readahead via increasing the offset of the fault swap entry without checking whether they are beyond the end of the swap device and it relys on the __swp_swapcount() and swapcache_prepare() to check it. Although __swp_swapcount() checks for the swap entry passed in, it will complain with the error message as follow for the expected invalid swap entry. This may make the end users confused.

swap_info_get: Bad swap offset entry 0200f8a7

To fix the false error message, the swap entry checking is added in swap readahead to avoid to pass the out-bound swap entries and the swap entry reserved for the swap header to __swp_swapcount() and swapcache_prepare().

Cc Tim Chen <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Michal Hocko <[email protected]>
Cc <[email protected]> # 4.11-4.13
Fixes e8c26ab60598 ("mm/swap: skip readahead for unreferenced swap slots")
---
 include/linux/swap.h |  1 +
 mm/swap_state.c      |  6 ++++--
 mm/swapfile.c        | 21 +++++++++++++++++++++
 3 files changed, 26 insertions(+), 2 deletions(-)
include/linux/swap.h +1 -0
index 84255b3da7c1..43b4b821c805 100644
@@ -476,6 +476,7 @@ extern int page_swapcount(struct page *);
 extern int __swap_count(struct swap_info_struct *si, swp_entry_t entry);
 extern int __swp_swapcount(swp_entry_t entry);
 extern int swp_swapcount(swp_entry_t entry);
+extern bool swap_entry_check(swp_entry_t entry);
 extern struct swap_info_struct *page_swap_info(struct page *);
 extern struct swap_info_struct *swp_swap_info(swp_entry_t entry);
 extern bool reuse_swap_page(struct page *, int *);
mm/swap_state.c +4 -2
index 6c017ced11e6..7dd70e77058d 100644
@@ -569,11 +569,13 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
 	/* Read a page_cluster sized and aligned cluster around offset. */
 	start_offset = offset & ~mask;
 	end_offset = offset | mask;
-	if (!start_offset)	/* First page is swap header. */
-		start_offset++;
 
 	blk_start_plug(&plug);
 	for (offset = start_offset; offset <= end_offset ; offset++) {
+		swp_entry_t ent = swp_entry(swp_type(entry), offset);
+
+		if (!swap_entry_check(ent))
+			continue;
 		/* Ok, do the async read-ahead now */
 		page = __read_swap_cache_async(
 			swp_entry(swp_type(entry), offset),
mm/swapfile.c +21 -0
index 3074b02eaa09..b04cec29c234 100644
@@ -1107,6 +1107,27 @@ static struct swap_info_struct *swap_info_get_cont(swp_entry_t entry,
 	return p;
 }
 
+bool swap_entry_check(swp_entry_t entry)
+{
+	struct swap_info_struct *p;
+	unsigned long offset, type;
+
+	type = swp_type(entry);
+	if (type >= nr_swapfiles)
+		goto bad_file;
+	p = swap_info[type];
+	offset = swp_offset(entry);
+	if (unlikely(!offset || offset >= p->max))
+		goto out;
+
+	return true;
+
+bad_file:
+	pr_err("%s: %s%08lx\n", __func__, Bad_file, entry.val);
+out:
+	return false;
+}
+
 static unsigned char __swap_entry_free(struct swap_info_struct *p,
 				       swp_entry_t entry, unsigned char usage)
 {
Signature · 1 line
2.14.2

Although it's better than old, we can make it simple, still.

include/linux/swapops.h +7 -0
index 291c4b534658..f50d5a48f03a 100644
@@ -41,6 +41,13 @@ static inline unsigned swp_type(swp_entry_t entry)
 	return (entry.val >> SWP_TYPE_SHIFT(entry));
 }
 
+extern struct swap_info_struct *swap_info[];
+
+static inline struct swap_info_struct *swp_si(swp_entry_t entry)
+{
+	return swap_info[swp_type(entry)];
+}
+
 /*
  * Extract the `offset' field from a swp_entry_t.  The swp_entry_t is in
  * arch-independent format
mm/swap_state.c +4 -0
index 378262d3a197..a0fe2d54ad09 100644
@@ -554,6 +554,7 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
 			struct vm_area_struct *vma, unsigned long addr)
 {
 	struct page *page;
+	struct swap_info_struct *si = swp_si(entry);
 	unsigned long entry_offset = swp_offset(entry);
 	unsigned long offset = entry_offset;
 	unsigned long start_offset, end_offset;
@@ -572,6 +573,9 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
 	if (!start_offset)	/* First page is swap header. */
 		start_offset++;
 
+	if (end_offset >= si->max)
+		end_offset = si->max - 1;
+
 	blk_start_plug(&plug);
 	for (offset = start_offset; offset <= end_offset ; offset++) {
 		/* Ok, do the async read-ahead now */

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

On Mon 30-10-17 08:57:13, Minchan Kim wrote:
[...]

Quoted 25 lines

Although it's better than old, we can make it simple, still.

include/linux/swapops.h +7 -0
index 291c4b534658..f50d5a48f03a 100644
@@ -41,6 +41,13 @@ static inline unsigned swp_type(swp_entry_t entry)
 	return (entry.val >> SWP_TYPE_SHIFT(entry));
 }
 
+extern struct swap_info_struct *swap_info[];
+
+static inline struct swap_info_struct *swp_si(swp_entry_t entry)
+{
+	return swap_info[swp_type(entry)];
+}
+
 /*
  * Extract the `offset' field from a swp_entry_t.  The swp_entry_t is in
  * arch-independent format
mm/swap_state.c +0 -0
index 378262d3a197..a0fe2d54ad09 100644

mm/swap_state.c struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask, · line 557

Quoted 4 lines
mm/swap_state.c +1 -0
@@ -554,6 +554,7 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
 			struct vm_area_struct *vma, unsigned long addr)
 {
 	struct page *page;
+	struct swap_info_struct *si = swp_si(entry);

Aren't you accessing beyond the array here?

Quoted 13 lines
mm/swap_state.c +3 -0
 	unsigned long entry_offset = swp_offset(entry);
 	unsigned long offset = entry_offset;
 	unsigned long start_offset, end_offset;
@@ -572,6 +573,9 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
 	if (!start_offset)	/* First page is swap header. */
 		start_offset++;
 
+	if (end_offset >= si->max)
+		end_offset = si->max - 1;
+
 	blk_start_plug(&plug);
 	for (offset = start_offset; offset <= end_offset ; offset++) {
 		/* Ok, do the async read-ahead now */
Signature · 8 lines
Michal Hocko
SUSE Labs

--
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 25 lines · Michal Hocko

On Mon 30-10-17 08:57:13, Minchan Kim wrote:
[...]

Quoted 23 lines

Although it's better than old, we can make it simple, still.

include/linux/swapops.h +7 -0
index 291c4b534658..f50d5a48f03a 100644
@@ -41,6 +41,13 @@ static inline unsigned swp_type(swp_entry_t entry)
 	return (entry.val >> SWP_TYPE_SHIFT(entry));
 }
 
+extern struct swap_info_struct *swap_info[];
+
+static inline struct swap_info_struct *swp_si(swp_entry_t entry)
+{
+	return swap_info[swp_type(entry)];
+}
+
 /*
  * Extract the `offset' field from a swp_entry_t.  The swp_entry_t is in
  * arch-independent format
mm/swap_state.c +0 -1
index 378262d3a197..a0fe2d54ad09 100644
--- a/mm/swap_state.c
Quoted 8 lines

mm/swap_state.c struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask, · line 557

Quoted 6 lines

+++ b/mm/swap_state.c

mm/swap_state.c +1 -0
@@ -554,6 +554,7 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
 			struct vm_area_struct *vma, unsigned long addr)
 {
 	struct page *page;
+	struct swap_info_struct *si = swp_si(entry);

Aren't you accessing beyond the array here?

I couldn't understand what you intend. Could you explain what case does it accesses beyond the arrary?

Thanks.

Quoted 8 lines
Quoted 8 lines
mm/swap_state.c +0 -0
 	unsigned long entry_offset = swp_offset(entry);
 	unsigned long offset = entry_offset;
 	unsigned long start_offset, end_offset;
@@ -572,6 +573,9 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
 	if (!start_offset)	/* First page is swap header. */
 		start_offset++;
 
Quoted 16 lines
Quoted 6 lines
mm/swap_state.c +3 -0
+	if (end_offset >= si->max)
+		end_offset = si->max - 1;
+
 	blk_start_plug(&plug);
 	for (offset = start_offset; offset <= end_offset ; offset++) {
 		/* Ok, do the async read-ahead now */
Signature · 8 lines
Michal Hocko
SUSE Labs

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

#8 lore raw
In reply to Minchan Kim

Hi, Minchan,

Quoted 110 lines · Minchan Kim

Hi Huang,

Quoted 99 lines · Ying

From: Huang Ying <[email protected]>

When a page fault occurs for a swap entry, the physical swap readahead (not the VMA base swap readahead) may readahead several swap entries after the fault swap entry. The readahead algorithm calculates some of the swap entries to readahead via increasing the offset of the fault swap entry without checking whether they are beyond the end of the swap device and it relys on the __swp_swapcount() and swapcache_prepare() to check it. Although __swp_swapcount() checks for the swap entry passed in, it will complain with the error message as follow for the expected invalid swap entry. This may make the end users confused.

swap_info_get: Bad swap offset entry 0200f8a7

To fix the false error message, the swap entry checking is added in swap readahead to avoid to pass the out-bound swap entries and the swap entry reserved for the swap header to __swp_swapcount() and swapcache_prepare().

Cc Tim Chen <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Michal Hocko <[email protected]>
Cc <[email protected]> # 4.11-4.13
Fixes e8c26ab60598 ("mm/swap: skip readahead for unreferenced swap slots")
---
 include/linux/swap.h |  1 +
 mm/swap_state.c      |  6 ++++--
 mm/swapfile.c        | 21 +++++++++++++++++++++
 3 files changed, 26 insertions(+), 2 deletions(-)
include/linux/swap.h +1 -0
index 84255b3da7c1..43b4b821c805 100644
@@ -476,6 +476,7 @@ extern int page_swapcount(struct page *);
 extern int __swap_count(struct swap_info_struct *si, swp_entry_t entry);
 extern int __swp_swapcount(swp_entry_t entry);
 extern int swp_swapcount(swp_entry_t entry);
+extern bool swap_entry_check(swp_entry_t entry);
 extern struct swap_info_struct *page_swap_info(struct page *);
 extern struct swap_info_struct *swp_swap_info(swp_entry_t entry);
 extern bool reuse_swap_page(struct page *, int *);
mm/swap_state.c +4 -2
index 6c017ced11e6..7dd70e77058d 100644
@@ -569,11 +569,13 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
 	/* Read a page_cluster sized and aligned cluster around offset. */
 	start_offset = offset & ~mask;
 	end_offset = offset | mask;
-	if (!start_offset)	/* First page is swap header. */
-		start_offset++;
 
 	blk_start_plug(&plug);
 	for (offset = start_offset; offset <= end_offset ; offset++) {
+		swp_entry_t ent = swp_entry(swp_type(entry), offset);
+
+		if (!swap_entry_check(ent))
+			continue;
 		/* Ok, do the async read-ahead now */
 		page = __read_swap_cache_async(
 			swp_entry(swp_type(entry), offset),
mm/swapfile.c +21 -0
index 3074b02eaa09..b04cec29c234 100644
@@ -1107,6 +1107,27 @@ static struct swap_info_struct *swap_info_get_cont(swp_entry_t entry,
 	return p;
 }
 
+bool swap_entry_check(swp_entry_t entry)
+{
+	struct swap_info_struct *p;
+	unsigned long offset, type;
+
+	type = swp_type(entry);
+	if (type >= nr_swapfiles)
+		goto bad_file;
+	p = swap_info[type];
+	offset = swp_offset(entry);
+	if (unlikely(!offset || offset >= p->max))
+		goto out;
+
+	return true;
+
+bad_file:
+	pr_err("%s: %s%08lx\n", __func__, Bad_file, entry.val);
+out:
+	return false;
+}
+
 static unsigned char __swap_entry_free(struct swap_info_struct *p,
 				       swp_entry_t entry, unsigned char usage)
 {
Signature · 1 line
2.14.2

Although it's better than old, we can make it simple, still.

include/linux/swapops.h +0 -0
index 291c4b534658..f50d5a48f03a 100644

include/linux/swapops.h static inline unsigned swp_type(swp_entry_t entry) · line 50

Quoted 10 lines
include/linux/swapops.h +7 -0
@@ -41,6 +41,13 @@ static inline unsigned swp_type(swp_entry_t entry)
 	return (entry.val >> SWP_TYPE_SHIFT(entry));
 }
 
+extern struct swap_info_struct *swap_info[];
+
+static inline struct swap_info_struct *swp_si(swp_entry_t entry)
+{
+	return swap_info[swp_type(entry)];
+}
+

Why not just use swp_swap_info()?

Quoted 16 lines
include/linux/swapops.h +0 -0
 /*
  * Extract the `offset' field from a swp_entry_t.  The swp_entry_t is in
  * arch-independent format
mm/swap_state.c +1 -0
index 378262d3a197..a0fe2d54ad09 100644
@@ -554,6 +554,7 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
 			struct vm_area_struct *vma, unsigned long addr)
 {
 	struct page *page;
+	struct swap_info_struct *si = swp_si(entry);
 	unsigned long entry_offset = swp_offset(entry);
 	unsigned long offset = entry_offset;
 	unsigned long start_offset, end_offset;

mm/swap_state.c struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask, · line 578

Quoted 6 lines
mm/swap_state.c +3 -0
@@ -572,6 +573,9 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
 	if (!start_offset)	/* First page is swap header. */
 		start_offset++;
 
+	if (end_offset >= si->max)
+		end_offset = si->max - 1;
+

I don't think two implementation has much difference. Both look OK for me.

Best Regards,
Huang, Ying

mm/swap_state.c struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask, · line 581

Quoted 3 lines
mm/swap_state.c +0 -0
 	blk_start_plug(&plug);
 	for (offset = start_offset; offset <= end_offset ; offset++) {
 		/* Ok, do the async read-ahead now */

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

#9 lore raw

Hi Huang,

Quoted 117 lines · Ying

Hi, Minchan,

Quoted 113 lines · Minchan Kim

Hi Huang,

Quoted 99 lines · Ying

From: Huang Ying <[email protected]>

When a page fault occurs for a swap entry, the physical swap readahead (not the VMA base swap readahead) may readahead several swap entries after the fault swap entry. The readahead algorithm calculates some of the swap entries to readahead via increasing the offset of the fault swap entry without checking whether they are beyond the end of the swap device and it relys on the __swp_swapcount() and swapcache_prepare() to check it. Although __swp_swapcount() checks for the swap entry passed in, it will complain with the error message as follow for the expected invalid swap entry. This may make the end users confused.

swap_info_get: Bad swap offset entry 0200f8a7

To fix the false error message, the swap entry checking is added in swap readahead to avoid to pass the out-bound swap entries and the swap entry reserved for the swap header to __swp_swapcount() and swapcache_prepare().

Cc Tim Chen <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Michal Hocko <[email protected]>
Cc <[email protected]> # 4.11-4.13
Fixes e8c26ab60598 ("mm/swap: skip readahead for unreferenced swap slots")
---
 include/linux/swap.h |  1 +
 mm/swap_state.c      |  6 ++++--
 mm/swapfile.c        | 21 +++++++++++++++++++++
 3 files changed, 26 insertions(+), 2 deletions(-)
include/linux/swap.h +1 -0
index 84255b3da7c1..43b4b821c805 100644
@@ -476,6 +476,7 @@ extern int page_swapcount(struct page *);
 extern int __swap_count(struct swap_info_struct *si, swp_entry_t entry);
 extern int __swp_swapcount(swp_entry_t entry);
 extern int swp_swapcount(swp_entry_t entry);
+extern bool swap_entry_check(swp_entry_t entry);
 extern struct swap_info_struct *page_swap_info(struct page *);
 extern struct swap_info_struct *swp_swap_info(swp_entry_t entry);
 extern bool reuse_swap_page(struct page *, int *);
mm/swap_state.c +4 -2
index 6c017ced11e6..7dd70e77058d 100644
@@ -569,11 +569,13 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
 	/* Read a page_cluster sized and aligned cluster around offset. */
 	start_offset = offset & ~mask;
 	end_offset = offset | mask;
-	if (!start_offset)	/* First page is swap header. */
-		start_offset++;
 
 	blk_start_plug(&plug);
 	for (offset = start_offset; offset <= end_offset ; offset++) {
+		swp_entry_t ent = swp_entry(swp_type(entry), offset);
+
+		if (!swap_entry_check(ent))
+			continue;
 		/* Ok, do the async read-ahead now */
 		page = __read_swap_cache_async(
 			swp_entry(swp_type(entry), offset),
mm/swapfile.c +21 -0
index 3074b02eaa09..b04cec29c234 100644
@@ -1107,6 +1107,27 @@ static struct swap_info_struct *swap_info_get_cont(swp_entry_t entry,
 	return p;
 }
 
+bool swap_entry_check(swp_entry_t entry)
+{
+	struct swap_info_struct *p;
+	unsigned long offset, type;
+
+	type = swp_type(entry);
+	if (type >= nr_swapfiles)
+		goto bad_file;
+	p = swap_info[type];
+	offset = swp_offset(entry);
+	if (unlikely(!offset || offset >= p->max))
+		goto out;
+
+	return true;
+
+bad_file:
+	pr_err("%s: %s%08lx\n", __func__, Bad_file, entry.val);
+out:
+	return false;
+}
+
 static unsigned char __swap_entry_free(struct swap_info_struct *p,
 				       swp_entry_t entry, unsigned char usage)
 {
Signature · 1 line
2.14.2

Although it's better than old, we can make it simple, still.

include/linux/swapops.h +0 -0
index 291c4b534658..f50d5a48f03a 100644
@@ -41,6 +41,13 @@ static inline unsigned swp_type(swp_entry_t entry)
 	return (entry.val >> SWP_TYPE_SHIFT(entry));
 }
 
Quoted 9 lines

include/linux/swapops.h static inline unsigned swp_type(swp_entry_t entry) · line 50

Quoted 7 lines
include/linux/swapops.h +7 -0
+extern struct swap_info_struct *swap_info[];
+
+static inline struct swap_info_struct *swp_si(swp_entry_t entry)
+{
+	return swap_info[swp_type(entry)];
+}
+

Why not just use swp_swap_info()?

Yeap, I forgot the one that even I added the function. :(

Quoted 20 lines
Quoted 20 lines
include/linux/swapops.h +0 -0
 /*
  * Extract the `offset' field from a swp_entry_t.  The swp_entry_t is in
  * arch-independent format
mm/swap_state.c +1 -0
index 378262d3a197..a0fe2d54ad09 100644
@@ -554,6 +554,7 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
 			struct vm_area_struct *vma, unsigned long addr)
 {
 	struct page *page;
+	struct swap_info_struct *si = swp_si(entry);
 	unsigned long entry_offset = swp_offset(entry);
 	unsigned long offset = entry_offset;
 	unsigned long start_offset, end_offset;
@@ -572,6 +573,9 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
 	if (!start_offset)	/* First page is swap header. */
 		start_offset++;
 
Quoted 6 lines

mm/swap_state.c struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask, · line 578

Quoted 3 lines
mm/swap_state.c +3 -0
+	if (end_offset >= si->max)
+		end_offset = si->max - 1;
+

I don't think two implementation has much difference. Both look OK for me.

The point is *readbility*. We always try to make code simple/understandable. Even, it's more important for stable candidate.

We don't need to check the validation in loop so reviewer don't need to take care of it once it passes boundary check in above logic.

As you say it's okay, please resend it.

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

#10 lore raw
Quoted 159 lines · Minchan Kim

Hi Huang,

Quoted 117 lines · Ying

Hi, Minchan,

Quoted 113 lines · Minchan Kim

Hi Huang,

Quoted 99 lines · Ying

From: Huang Ying <[email protected]>

When a page fault occurs for a swap entry, the physical swap readahead (not the VMA base swap readahead) may readahead several swap entries after the fault swap entry. The readahead algorithm calculates some of the swap entries to readahead via increasing the offset of the fault swap entry without checking whether they are beyond the end of the swap device and it relys on the __swp_swapcount() and swapcache_prepare() to check it. Although __swp_swapcount() checks for the swap entry passed in, it will complain with the error message as follow for the expected invalid swap entry. This may make the end users confused.

swap_info_get: Bad swap offset entry 0200f8a7

To fix the false error message, the swap entry checking is added in swap readahead to avoid to pass the out-bound swap entries and the swap entry reserved for the swap header to __swp_swapcount() and swapcache_prepare().

Cc Tim Chen <[email protected]>
Cc Minchan Kim <[email protected]>
Cc Michal Hocko <[email protected]>
Cc <[email protected]> # 4.11-4.13
Fixes e8c26ab60598 ("mm/swap: skip readahead for unreferenced swap slots")
---
 include/linux/swap.h |  1 +
 mm/swap_state.c      |  6 ++++--
 mm/swapfile.c        | 21 +++++++++++++++++++++
 3 files changed, 26 insertions(+), 2 deletions(-)
include/linux/swap.h +1 -0
index 84255b3da7c1..43b4b821c805 100644
@@ -476,6 +476,7 @@ extern int page_swapcount(struct page *);
 extern int __swap_count(struct swap_info_struct *si, swp_entry_t entry);
 extern int __swp_swapcount(swp_entry_t entry);
 extern int swp_swapcount(swp_entry_t entry);
+extern bool swap_entry_check(swp_entry_t entry);
 extern struct swap_info_struct *page_swap_info(struct page *);
 extern struct swap_info_struct *swp_swap_info(swp_entry_t entry);
 extern bool reuse_swap_page(struct page *, int *);
mm/swap_state.c +4 -2
index 6c017ced11e6..7dd70e77058d 100644
@@ -569,11 +569,13 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
 	/* Read a page_cluster sized and aligned cluster around offset. */
 	start_offset = offset & ~mask;
 	end_offset = offset | mask;
-	if (!start_offset)	/* First page is swap header. */
-		start_offset++;
 
 	blk_start_plug(&plug);
 	for (offset = start_offset; offset <= end_offset ; offset++) {
+		swp_entry_t ent = swp_entry(swp_type(entry), offset);
+
+		if (!swap_entry_check(ent))
+			continue;
 		/* Ok, do the async read-ahead now */
 		page = __read_swap_cache_async(
 			swp_entry(swp_type(entry), offset),
mm/swapfile.c +21 -0
index 3074b02eaa09..b04cec29c234 100644
@@ -1107,6 +1107,27 @@ static struct swap_info_struct *swap_info_get_cont(swp_entry_t entry,
 	return p;
 }
 
+bool swap_entry_check(swp_entry_t entry)
+{
+	struct swap_info_struct *p;
+	unsigned long offset, type;
+
+	type = swp_type(entry);
+	if (type >= nr_swapfiles)
+		goto bad_file;
+	p = swap_info[type];
+	offset = swp_offset(entry);
+	if (unlikely(!offset || offset >= p->max))
+		goto out;
+
+	return true;
+
+bad_file:
+	pr_err("%s: %s%08lx\n", __func__, Bad_file, entry.val);
+out:
+	return false;
+}
+
 static unsigned char __swap_entry_free(struct swap_info_struct *p,
 				       swp_entry_t entry, unsigned char usage)
 {
Signature · 1 line
2.14.2

Although it's better than old, we can make it simple, still.

include/linux/swapops.h +0 -0
index 291c4b534658..f50d5a48f03a 100644
@@ -41,6 +41,13 @@ static inline unsigned swp_type(swp_entry_t entry)
 	return (entry.val >> SWP_TYPE_SHIFT(entry));
 }
 
Quoted 9 lines

include/linux/swapops.h static inline unsigned swp_type(swp_entry_t entry) · line 50

Quoted 7 lines
include/linux/swapops.h +7 -0
+extern struct swap_info_struct *swap_info[];
+
+static inline struct swap_info_struct *swp_si(swp_entry_t entry)
+{
+	return swap_info[swp_type(entry)];
+}
+

Why not just use swp_swap_info()?

Yeap, I forgot the one that even I added the function. :(

Quoted 20 lines
Quoted 20 lines
include/linux/swapops.h +0 -0
 /*
  * Extract the `offset' field from a swp_entry_t.  The swp_entry_t is in
  * arch-independent format
mm/swap_state.c +1 -0
index 378262d3a197..a0fe2d54ad09 100644
@@ -554,6 +554,7 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
 			struct vm_area_struct *vma, unsigned long addr)
 {
 	struct page *page;
+	struct swap_info_struct *si = swp_si(entry);
 	unsigned long entry_offset = swp_offset(entry);
 	unsigned long offset = entry_offset;
 	unsigned long start_offset, end_offset;
@@ -572,6 +573,9 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
 	if (!start_offset)	/* First page is swap header. */
 		start_offset++;
 
Quoted 6 lines

mm/swap_state.c struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask, · line 578

Quoted 3 lines
mm/swap_state.c +3 -0
+	if (end_offset >= si->max)
+		end_offset = si->max - 1;
+

I don't think two implementation has much difference. Both look OK for me.

Quoted 7 lines

The point is *readbility*. We always try to make code simple/understandable. Even, it's more important for stable candidate.

We don't need to check the validation in loop so reviewer don't need to take care of it once it passes boundary check in above logic.

As you say it's okay, please resend it.

OK.

Best Regards,
Huang, Ying

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

#5 lore raw
In reply to Minchan Kim
Quoted 35 lines · Minchan Kim
Quoted 25 lines · Michal Hocko

On Mon 30-10-17 08:57:13, Minchan Kim wrote:
[...]

Quoted 23 lines

Although it's better than old, we can make it simple, still.

include/linux/swapops.h +7 -0
index 291c4b534658..f50d5a48f03a 100644
@@ -41,6 +41,13 @@ static inline unsigned swp_type(swp_entry_t entry)
 	return (entry.val >> SWP_TYPE_SHIFT(entry));
 }
 
+extern struct swap_info_struct *swap_info[];
+
+static inline struct swap_info_struct *swp_si(swp_entry_t entry)
+{
+	return swap_info[swp_type(entry)];
+}
+
 /*
  * Extract the `offset' field from a swp_entry_t.  The swp_entry_t is in
  * arch-independent format
mm/swap_state.c +0 -1
index 378262d3a197..a0fe2d54ad09 100644
--- a/mm/swap_state.c
Quoted 8 lines

mm/swap_state.c struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask, · line 557

Quoted 6 lines

+++ b/mm/swap_state.c

mm/swap_state.c +1 -0
@@ -554,6 +554,7 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
 			struct vm_area_struct *vma, unsigned long addr)
 {
 	struct page *page;
+	struct swap_info_struct *si = swp_si(entry);

Aren't you accessing beyond the array here?

Quoted 2 lines

I couldn't understand what you intend. Could you explain what case does it accesses beyond the arrary?

what if swp_type > nr_swapfiles

Signature · 8 lines
Michal Hocko
SUSE Labs

--
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>
#6 lore raw
Quoted 36 lines · Michal Hocko
Quoted 35 lines · Minchan Kim
Quoted 33 lines · Michal Hocko

On Mon 30-10-17 08:57:13, Minchan Kim wrote:
[...]

Quoted 25 lines

Although it's better than old, we can make it simple, still.

include/linux/swapops.h +7 -0
index 291c4b534658..f50d5a48f03a 100644
@@ -41,6 +41,13 @@ static inline unsigned swp_type(swp_entry_t entry)
 	return (entry.val >> SWP_TYPE_SHIFT(entry));
 }
 
+extern struct swap_info_struct *swap_info[];
+
+static inline struct swap_info_struct *swp_si(swp_entry_t entry)
+{
+	return swap_info[swp_type(entry)];
+}
+
 /*
  * Extract the `offset' field from a swp_entry_t.  The swp_entry_t is in
  * arch-independent format
mm/swap_state.c +0 -0
index 378262d3a197..a0fe2d54ad09 100644

mm/swap_state.c struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask, · line 557

Quoted 4 lines
mm/swap_state.c +1 -0
@@ -554,6 +554,7 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
 			struct vm_area_struct *vma, unsigned long addr)
 {
 	struct page *page;
+	struct swap_info_struct *si = swp_si(entry);

Aren't you accessing beyond the array here?

Quoted 4 lines
Quoted 2 lines

I couldn't understand what you intend. Could you explain what case does it accesses beyond the arrary?

what if swp_type > nr_swapfiles

Logicall, it shouldn't be happen but it's a bug once it happens. Such a bug can happen everywhere.
Why do you want to take care in this logic?

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

#7 lore raw
Quoted 37 lines · Minchan Kim
Quoted 36 lines · Michal Hocko
Quoted 35 lines · Minchan Kim
Quoted 33 lines · Michal Hocko

On Mon 30-10-17 08:57:13, Minchan Kim wrote:
[...]

Quoted 25 lines

Although it's better than old, we can make it simple, still.

include/linux/swapops.h +7 -0
index 291c4b534658..f50d5a48f03a 100644
@@ -41,6 +41,13 @@ static inline unsigned swp_type(swp_entry_t entry)
 	return (entry.val >> SWP_TYPE_SHIFT(entry));
 }
 
+extern struct swap_info_struct *swap_info[];
+
+static inline struct swap_info_struct *swp_si(swp_entry_t entry)
+{
+	return swap_info[swp_type(entry)];
+}
+
 /*
  * Extract the `offset' field from a swp_entry_t.  The swp_entry_t is in
  * arch-independent format
mm/swap_state.c +0 -0
index 378262d3a197..a0fe2d54ad09 100644

mm/swap_state.c struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask, · line 557

Quoted 4 lines
mm/swap_state.c +1 -0
@@ -554,6 +554,7 @@ struct page *swapin_readahead(swp_entry_t entry, gfp_t gfp_mask,
 			struct vm_area_struct *vma, unsigned long addr)
 {
 	struct page *page;
+	struct swap_info_struct *si = swp_si(entry);

Aren't you accessing beyond the array here?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

I couldn't understand what you intend. Could you explain what case does it accesses beyond the arrary?

what if swp_type > nr_swapfiles

Logicall, it shouldn't be happen but it's a bug once it happens. Such a bug can happen everywhere.
Why do you want to take care in this logic?

You are right. It was my misunderstanding of the patch. I though the readahead swap entries could overflow swp_type as well as the offset. My bad.

Signature · 8 lines
Michal Hocko
SUSE Labs

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