Skip to contentSkip to conversation

rfc: fix splice mapping race?

PATCH In review Nick Piggin 4 participants 5 messages Jun 18, 2006 – Jun 19, 2006

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

PATCH 1/1 rfc: fix splice mapping race?

Hi, I would be interested in confirmation/comments for this patch.

I believe splice is unsafe to access the page mapping obtained when the page was unlocked: the page could subsequently be truncated and the mapping reclaimed (see set_page_dirty_lock comments).

Modify the remove_mapping precondition to ensure the caller has locked the page and obtained the correct mapping. Modify callers to ensure the mapping is the correct one.

In page migration, detect the missing mapping early and bail out if that is the case: the page is not going to get un-truncated, so retrying is just a waste of time.

fs/splice.c +5 -1
Index: linux-2.6/fs/splice.c
===================================================================
@@ -55,9 +55,12 @@ static int page_cache_pipe_buf_steal(str
 				     struct pipe_buffer *buf)
 {
 	struct page *page = buf->page;
-	struct address_space *mapping = page_mapping(page);
+	struct address_space *mapping;
 
 	lock_page(page);
+	mapping = page_mapping(page);
+	if (!mapping)
+		goto out_failed;
 
 	WARN_ON(!PageUptodate(page));
 
@@ -74,6 +77,7 @@ static int page_cache_pipe_buf_steal(str
 		try_to_release_page(page, mapping_gfp_mask(mapping));
 
 	if (!remove_mapping(mapping, page)) {
+out_failed:
 		unlock_page(page);
 		return 1;
 	}
mm/migrate.c +5 -1
Index: linux-2.6/mm/migrate.c
===================================================================
@@ -136,9 +136,13 @@ static int swap_page(struct page *page)
 {
 	struct address_space *mapping = page_mapping(page);
 
-	if (page_mapped(page) && mapping)
+	if (!mapping)
+		return -EINVAL; /* page truncated. signal permanent failure */
+
+	if (page_mapped(page)) {
 		if (try_to_unmap(page, 1) != SWAP_SUCCESS)
 			goto unlock_retry;
+	}
 
 	if (PageDirty(page)) {
 		/* Page is dirty, try to write it out here */
mm/vmscan.c +3 -3
Index: linux-2.6/mm/vmscan.c
===================================================================
@@ -362,8 +362,8 @@ pageout_t pageout(struct page *page, str
 
 int remove_mapping(struct address_space *mapping, struct page *page)
 {
-	if (!mapping)
-		return 0;		/* truncate got there first */
+	BUG_ON(!PageLocked(page));
+	BUG_ON(mapping != page->mapping);
 
 	write_lock_irq(&mapping->tree_lock);
 
@@ -532,7 +532,7 @@ static unsigned long shrink_page_list(st
 				goto free_it;
 		}
 
-		if (!remove_mapping(mapping, page))
+		if (!mapping || !remove_mapping(mapping, page))
 			goto keep_locked;
 
 free_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>

#1 lore raw
Quoted 10 lines · Nick Piggin

Hi, I would be interested in confirmation/comments for this patch.

I believe splice is unsafe to access the page mapping obtained when the page was unlocked: the page could subsequently be truncated and the mapping reclaimed (see set_page_dirty_lock comments).

Modify the remove_mapping precondition to ensure the caller has locked the page and obtained the correct mapping. Modify callers to ensure the mapping is the correct one.

Quoted 5 lines

In page migration, detect the missing mapping early and bail out if that is the case: the page is not going to get un-truncated, so retrying is just a waste of time.

Looks sane, except the change in migrate (comment there). I like the remove_mapping() pre-conditions.

Quoted 32 lines

Index: linux-2.6/fs/splice.c
===================================================================

fs/splice.c +5 -1
@@ -55,9 +55,12 @@ static int page_cache_pipe_buf_steal(str
 				     struct pipe_buffer *buf)
 {
 	struct page *page = buf->page;
-	struct address_space *mapping = page_mapping(page);
+	struct address_space *mapping;
 
 	lock_page(page);
+	mapping = page_mapping(page);
+	if (!mapping)
+		goto out_failed;
 
 	WARN_ON(!PageUptodate(page));
 
@@ -74,6 +77,7 @@ static int page_cache_pipe_buf_steal(str
 		try_to_release_page(page, mapping_gfp_mask(mapping));
 
 	if (!remove_mapping(mapping, page)) {
+out_failed:
 		unlock_page(page);
 		return 1;
 	}

Index: linux-2.6/mm/migrate.c
===================================================================

mm/migrate.c static int swap_page(struct page *page) · line 140

Quoted 6 lines
mm/migrate.c +2 -1
@@ -136,9 +136,13 @@ static int swap_page(struct page *page)
 {
 	struct address_space *mapping = page_mapping(page);
 
-	if (page_mapped(page) && mapping)
+	if (!mapping)
+		return -EINVAL; /* page truncated. signal permanent failure */

Here, I think you need to unlock the page too.

Quoted 33 lines
mm/migrate.c +3 -0
+
+	if (page_mapped(page)) {
 		if (try_to_unmap(page, 1) != SWAP_SUCCESS)
 			goto unlock_retry;
+	}
 
 	if (PageDirty(page)) {
 		/* Page is dirty, try to write it out here */

Index: linux-2.6/mm/vmscan.c
===================================================================

mm/vmscan.c +3 -4
@@ -362,8 +362,8 @@ pageout_t pageout(struct page *page, str
 
 int remove_mapping(struct address_space *mapping, struct page *page)
 {
-	if (!mapping)
-		return 0;		/* truncate got there first */
+	BUG_ON(!PageLocked(page));
+	BUG_ON(mapping != page->mapping);
 
 	write_lock_irq(&mapping->tree_lock);
 
@@ -532,7 +532,7 @@ static unsigned long shrink_page_list(st
 				goto free_it;
 		}
 
-		if (!remove_mapping(mapping, page))
+		if (!mapping || !remove_mapping(mapping, page))
 			goto keep_locked;
 
 free_it:
-
Quoted 4 lines

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/

--
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 8 lines · Peter Zijlstra
Quoted 5 lines

In page migration, detect the missing mapping early and bail out if that is the case: the page is not going to get un-truncated, so retrying is just a waste of time.

Looks sane, except the change in migrate (comment there). I like the remove_mapping() pre-conditions.

Thanks.

Quoted 11 lines

mm/migrate.c static int swap_page(struct page *page) · line 140

Quoted 9 lines
mm/migrate.c +2 -1
@@ -136,9 +136,13 @@ static int swap_page(struct page *page)
 {
 	struct address_space *mapping = page_mapping(page);
 
-	if (page_mapped(page) && mapping)
+	if (!mapping)
+		return -EINVAL; /* page truncated. signal permanent failure */

Here, I think you need to unlock the page too.

Bah, yes thanks... I'll post an updated patch after others have had time to comment.

Nick

--
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
In reply to Nick Piggin
Quoted 6 lines · Nick Piggin

Hi, I would be interested in confirmation/comments for this patch.

I believe splice is unsafe to access the page mapping obtained when the page was unlocked: the page could subsequently be truncated and the mapping reclaimed (see set_page_dirty_lock comments).

Quoted 7 lines

Modify the remove_mapping precondition to ensure the caller has locked the page and obtained the correct mapping. Modify callers to ensure the mapping is the correct one.

In page migration, detect the missing mapping early and bail out if that is the case: the page is not going to get un-truncated, so retrying is just a waste of time.

splice bit looks good to me!

Signature · 1 line
Jens Axboe
#4 lore raw
In reply to Nick Piggin
Quoted 3 lines · Nick Piggin

In page migration, detect the missing mapping early and bail out if that is the case: the page is not going to get un-truncated, so retrying is just a waste of time.

Note that swap_page() has been removed in Andrew's tree.

We already check for the mapping being NULL before we get to swap_page by the way. See migrate_pages().

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