Skip to contentSkip to conversation

mm: Fix races between address_space dereference and free in page_evicatable

PATCH In review Huang, Ying 3 participants 7 messages Feb 12, 2018 – Feb 26, 2018

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

PATCH 1/1 mm: Fix races between address_space dereference and free in page_evicatable

From: Huang Ying <[email protected]>

When page_mapping() is called and the mapping is dereferenced in page_evicatable() through shrink_active_list(), it is possible for the inode to be truncated and the embedded address space to be freed at the same time. This may lead to the following race.

CPU1 CPU2

truncate(inode) shrink_active_list()
... page_evictable(page)

  truncate_inode_page(mapping, page);
    delete_from_page_cache(page)
      spin_lock_irqsave(&mapping->tree_lock, flags);
        __delete_from_page_cache(page, NULL)
          page_cache_tree_delete(..)
            ...                                         mapping = page_mapping(page);
            page->mapping = NULL;
            ...
      spin_unlock_irqrestore(&mapping->tree_lock, flags);
      page_cache_free_page(mapping, page)
        put_page(page)
          if (put_page_testzero(page)) -> false

- inode now has no pages and can be freed including embedded address_space

                                                        mapping_unevictable(mapping)
							  test_bit(AS_UNEVICTABLE, &mapping->flags);

- we've dereferenced mapping which is potentially already free.

Similar race exists between swap cache freeing and page_evicatable() too.

The address_space in inode and swap cache will be freed after a RCU grace period. So the races are fixed via enclosing the page_mapping() and address_space usage in rcu_read_lock/unlock(). Some comments are added in code to make it clear what is protected by the RCU read lock.

Cc Mel Gorman <[email protected]>
Cc Minchan Kim <[email protected]>
Cc "Huang, Ying" <[email protected]>
Cc Jan Kara <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
---
 mm/vmscan.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
mm/vmscan.c +7 -1
index d1c1e00b08bb..10a0f32a3f90 100644
@@ -3886,7 +3886,13 @@ int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
  */
 int page_evictable(struct page *page)
 {
-	return !mapping_unevictable(page_mapping(page)) && !PageMlocked(page);
+	int ret;
+
+	/* Prevent address_space of inode and swap cache from being freed */
+	rcu_read_lock();
+	ret = !mapping_unevictable(page_mapping(page)) && !PageMlocked(page);
+	rcu_read_unlock();
+	return ret;
 }
 
 #ifdef CONFIG_SHMEM
Signature · 7 lines
2.15.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>
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 36 lines · Ying

From: Huang Ying <[email protected]>

When page_mapping() is called and the mapping is dereferenced in page_evicatable() through shrink_active_list(), it is possible for the inode to be truncated and the embedded address space to be freed at the same time. This may lead to the following race.

CPU1 CPU2

truncate(inode) shrink_active_list()
... page_evictable(page)

  truncate_inode_page(mapping, page);
    delete_from_page_cache(page)
      spin_lock_irqsave(&mapping->tree_lock, flags);
        __delete_from_page_cache(page, NULL)
          page_cache_tree_delete(..)
            ...                                         mapping = page_mapping(page);
            page->mapping = NULL;
            ...
      spin_unlock_irqrestore(&mapping->tree_lock, flags);
      page_cache_free_page(mapping, page)
        put_page(page)
          if (put_page_testzero(page)) -> false

- inode now has no pages and can be freed including embedded address_space

                                                        mapping_unevictable(mapping)
							  test_bit(AS_UNEVICTABLE, &mapping->flags);

- we've dereferenced mapping which is potentially already free.

Similar race exists between swap cache freeing and page_evicatable() too.

The address_space in inode and swap cache will be freed after a RCU grace period. So the races are fixed via enclosing the page_mapping() and address_space usage in rcu_read_lock/unlock(). Some comments are added in code to make it clear what is protected by the RCU read lock.

Quoted 7 lines
Cc Mel Gorman <[email protected]>
Cc Minchan Kim <[email protected]>
Cc "Huang, Ying" <[email protected]>
Cc Jan Kara <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>

The race looks real (although very unlikely) and the patch looks good to me. You can add:

Honza

Quoted 26 lines
---
 mm/vmscan.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
mm/vmscan.c +7 -1
index d1c1e00b08bb..10a0f32a3f90 100644
@@ -3886,7 +3886,13 @@ int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
  */
 int page_evictable(struct page *page)
 {
-	return !mapping_unevictable(page_mapping(page)) && !PageMlocked(page);
+	int ret;
+
+	/* Prevent address_space of inode and swap cache from being freed */
+	rcu_read_lock();
+	ret = !mapping_unevictable(page_mapping(page)) && !PageMlocked(page);
+	rcu_read_unlock();
+	return ret;
 }
 
 #ifdef CONFIG_SHMEM
Signature · 1 line
2.15.1
Signature · 8 lines
Jan Kara <[email protected]>
SUSE Labs, CR

--
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 27 lines · Ying

From: Huang Ying <[email protected]>

When page_mapping() is called and the mapping is dereferenced in page_evicatable() through shrink_active_list(), it is possible for the inode to be truncated and the embedded address space to be freed at the same time. This may lead to the following race.

CPU1 CPU2

truncate(inode) shrink_active_list()
... page_evictable(page)

  truncate_inode_page(mapping, page);
    delete_from_page_cache(page)
      spin_lock_irqsave(&mapping->tree_lock, flags);
        __delete_from_page_cache(page, NULL)
          page_cache_tree_delete(..)
            ...                                         mapping = page_mapping(page);
            page->mapping = NULL;
            ...
      spin_unlock_irqrestore(&mapping->tree_lock, flags);
      page_cache_free_page(mapping, page)
        put_page(page)
          if (put_page_testzero(page)) -> false

- inode now has no pages and can be freed including embedded address_space

                                                        mapping_unevictable(mapping)
							  test_bit(AS_UNEVICTABLE, &mapping->flags);
Quoted 8 lines

- we've dereferenced mapping which is potentially already free.

Similar race exists between swap cache freeing and page_evicatable() too.

The address_space in inode and swap cache will be freed after a RCU grace period. So the races are fixed via enclosing the page_mapping() and address_space usage in rcu_read_lock/unlock(). Some comments are added in code to make it clear what is protected by the RCU read lock.

Is it always true for every FSes, even upcoming FSes?
IOW, do we have any strict rule FS folks must use RCU(i.e., call_rcu) to destroy inode?

Let's cc linux-fs.

Quoted 34 lines
Cc Mel Gorman <[email protected]>
Cc Minchan Kim <[email protected]>
Cc "Huang, Ying" <[email protected]>
Cc Jan Kara <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
---
 mm/vmscan.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
mm/vmscan.c +7 -1
index d1c1e00b08bb..10a0f32a3f90 100644
@@ -3886,7 +3886,13 @@ int node_reclaim(struct pglist_data *pgdat, gfp_t gfp_mask, unsigned int order)
  */
 int page_evictable(struct page *page)
 {
-	return !mapping_unevictable(page_mapping(page)) && !PageMlocked(page);
+	int ret;
+
+	/* Prevent address_space of inode and swap cache from being freed */
+	rcu_read_lock();
+	ret = !mapping_unevictable(page_mapping(page)) && !PageMlocked(page);
+	rcu_read_unlock();
+	return ret;
 }
 
 #ifdef CONFIG_SHMEM
Signature · 1 line
2.15.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>

reviewer
#3 lore raw

Hi Minchan,

Quoted 37 lines · Minchan Kim
Quoted 27 lines · Ying

From: Huang Ying <[email protected]>

When page_mapping() is called and the mapping is dereferenced in page_evicatable() through shrink_active_list(), it is possible for the inode to be truncated and the embedded address space to be freed at the same time. This may lead to the following race.

CPU1 CPU2

truncate(inode) shrink_active_list()
... page_evictable(page)

  truncate_inode_page(mapping, page);
    delete_from_page_cache(page)
      spin_lock_irqsave(&mapping->tree_lock, flags);
        __delete_from_page_cache(page, NULL)
          page_cache_tree_delete(..)
            ...                                         mapping = page_mapping(page);
            page->mapping = NULL;
            ...
      spin_unlock_irqrestore(&mapping->tree_lock, flags);
      page_cache_free_page(mapping, page)
        put_page(page)
          if (put_page_testzero(page)) -> false

- inode now has no pages and can be freed including embedded address_space

                                                        mapping_unevictable(mapping)
							  test_bit(AS_UNEVICTABLE, &mapping->flags);
Quoted 8 lines

- we've dereferenced mapping which is potentially already free.

Similar race exists between swap cache freeing and page_evicatable() too.

The address_space in inode and swap cache will be freed after a RCU grace period. So the races are fixed via enclosing the page_mapping() and address_space usage in rcu_read_lock/unlock(). Some comments are added in code to make it clear what is protected by the RCU read lock.

Quoted 5 lines

Is it always true for every FSes, even upcoming FSes?
IOW, do we have any strict rule FS folks must use RCU(i.e., call_rcu) to destroy inode?

Let's cc linux-fs.

That's actually a good question. Pathname lookup relies on inodes being protected by RCU so "normal" filesystems definitely need to use RCU freeing of inodes. OTOH a filesystem could in theory refuse any attempt for RCU pathname walk (in its .d_revalidate/.d_compare callback) and then get away with freeing its inodes normally AFAICT. I don't see that happening anywhere in the tree but in theory it is possible with some effort... But frankly I don't see a good reason for that so all we should do is to document that .destroy_inode needs to free the inode structure through RCU if it uses page cache? Al?

Honza

Signature · 8 lines
Jan Kara <[email protected]>
SUSE Labs, CR

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

Hi Jan,

Quoted 46 lines · Jan Kara

Hi Minchan,

Quoted 37 lines · Minchan Kim
Quoted 27 lines · Ying

From: Huang Ying <[email protected]>

When page_mapping() is called and the mapping is dereferenced in page_evicatable() through shrink_active_list(), it is possible for the inode to be truncated and the embedded address space to be freed at the same time. This may lead to the following race.

CPU1 CPU2

truncate(inode) shrink_active_list()
... page_evictable(page)

  truncate_inode_page(mapping, page);
    delete_from_page_cache(page)
      spin_lock_irqsave(&mapping->tree_lock, flags);
        __delete_from_page_cache(page, NULL)
          page_cache_tree_delete(..)
            ...                                         mapping = page_mapping(page);
            page->mapping = NULL;
            ...
      spin_unlock_irqrestore(&mapping->tree_lock, flags);
      page_cache_free_page(mapping, page)
        put_page(page)
          if (put_page_testzero(page)) -> false

- inode now has no pages and can be freed including embedded address_space

                                                        mapping_unevictable(mapping)
							  test_bit(AS_UNEVICTABLE, &mapping->flags);
Quoted 8 lines

- we've dereferenced mapping which is potentially already free.

Similar race exists between swap cache freeing and page_evicatable() too.

The address_space in inode and swap cache will be freed after a RCU grace period. So the races are fixed via enclosing the page_mapping() and address_space usage in rcu_read_lock/unlock(). Some comments are added in code to make it clear what is protected by the RCU read lock.

Quoted 5 lines

Is it always true for every FSes, even upcoming FSes?
IOW, do we have any strict rule FS folks must use RCU(i.e., call_rcu) to destroy inode?

Let's cc linux-fs.

Quoted 9 lines

That's actually a good question. Pathname lookup relies on inodes being protected by RCU so "normal" filesystems definitely need to use RCU freeing of inodes. OTOH a filesystem could in theory refuse any attempt for RCU pathname walk (in its .d_revalidate/.d_compare callback) and then get away with freeing its inodes normally AFAICT. I don't see that happening anywhere in the tree but in theory it is possible with some effort... But frankly I don't see a good reason for that so all we should do is to document that .destroy_inode needs to free the inode structure through RCU if it uses page cache? Al?

Yub, it would be much better. However, how does this patch fix the problem? Although it can make only page_evictable safe, we could go with the page further and finally uses page->mapping, again.
For instance,

shrink_active_list

	page_evictable();
	..
	page_referened()
		page_rmapping
			page->mapping

I think caller should lock the page to protect entire operation, which have been used more widely to pin a address_space.

Thanks.

--
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
Quoted 64 lines · Minchan Kim

Hi Jan,

Quoted 46 lines · Jan Kara

Hi Minchan,

Quoted 37 lines · Minchan Kim
Quoted 27 lines · Ying

From: Huang Ying <[email protected]>

When page_mapping() is called and the mapping is dereferenced in page_evicatable() through shrink_active_list(), it is possible for the inode to be truncated and the embedded address space to be freed at the same time. This may lead to the following race.

CPU1 CPU2

truncate(inode) shrink_active_list()
... page_evictable(page)

  truncate_inode_page(mapping, page);
    delete_from_page_cache(page)
      spin_lock_irqsave(&mapping->tree_lock, flags);
        __delete_from_page_cache(page, NULL)
          page_cache_tree_delete(..)
            ...                                         mapping = page_mapping(page);
            page->mapping = NULL;
            ...
      spin_unlock_irqrestore(&mapping->tree_lock, flags);
      page_cache_free_page(mapping, page)
        put_page(page)
          if (put_page_testzero(page)) -> false

- inode now has no pages and can be freed including embedded address_space

                                                        mapping_unevictable(mapping)
							  test_bit(AS_UNEVICTABLE, &mapping->flags);
Quoted 8 lines

- we've dereferenced mapping which is potentially already free.

Similar race exists between swap cache freeing and page_evicatable() too.

The address_space in inode and swap cache will be freed after a RCU grace period. So the races are fixed via enclosing the page_mapping() and address_space usage in rcu_read_lock/unlock(). Some comments are added in code to make it clear what is protected by the RCU read lock.

Quoted 5 lines

Is it always true for every FSes, even upcoming FSes?
IOW, do we have any strict rule FS folks must use RCU(i.e., call_rcu) to destroy inode?

Let's cc linux-fs.

Quoted 9 lines

That's actually a good question. Pathname lookup relies on inodes being protected by RCU so "normal" filesystems definitely need to use RCU freeing of inodes. OTOH a filesystem could in theory refuse any attempt for RCU pathname walk (in its .d_revalidate/.d_compare callback) and then get away with freeing its inodes normally AFAICT. I don't see that happening anywhere in the tree but in theory it is possible with some effort... But frankly I don't see a good reason for that so all we should do is to document that .destroy_inode needs to free the inode structure through RCU if it uses page cache? Al?

Yub, it would be much better. However, how does this patch fix the problem? Although it can make only page_evictable safe, we could go with the page further and finally uses page->mapping, again.
For instance,

Quoted 6 lines

shrink_active_list

	page_evictable();
	..
	page_referened()
		page_rmapping
			page->mapping

This only checks the value of page->mapping, not deference page->mapping. So it should be safe.

Best Regards,
Huang, Ying

Quoted 4 lines

I think caller should lock the page to protect entire operation, which have been used more widely to pin a address_space.

Thanks.

--
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 73 lines · Ying
Quoted 64 lines · Minchan Kim

Hi Jan,

Quoted 46 lines · Jan Kara

Hi Minchan,

Quoted 37 lines · Minchan Kim
Quoted 27 lines · Ying

From: Huang Ying <[email protected]>

When page_mapping() is called and the mapping is dereferenced in page_evicatable() through shrink_active_list(), it is possible for the inode to be truncated and the embedded address space to be freed at the same time. This may lead to the following race.

CPU1 CPU2

truncate(inode) shrink_active_list()
... page_evictable(page)

  truncate_inode_page(mapping, page);
    delete_from_page_cache(page)
      spin_lock_irqsave(&mapping->tree_lock, flags);
        __delete_from_page_cache(page, NULL)
          page_cache_tree_delete(..)
            ...                                         mapping = page_mapping(page);
            page->mapping = NULL;
            ...
      spin_unlock_irqrestore(&mapping->tree_lock, flags);
      page_cache_free_page(mapping, page)
        put_page(page)
          if (put_page_testzero(page)) -> false

- inode now has no pages and can be freed including embedded address_space

                                                        mapping_unevictable(mapping)
							  test_bit(AS_UNEVICTABLE, &mapping->flags);
Quoted 8 lines

- we've dereferenced mapping which is potentially already free.

Similar race exists between swap cache freeing and page_evicatable() too.

The address_space in inode and swap cache will be freed after a RCU grace period. So the races are fixed via enclosing the page_mapping() and address_space usage in rcu_read_lock/unlock(). Some comments are added in code to make it clear what is protected by the RCU read lock.

Quoted 5 lines

Is it always true for every FSes, even upcoming FSes?
IOW, do we have any strict rule FS folks must use RCU(i.e., call_rcu) to destroy inode?

Let's cc linux-fs.

Quoted 9 lines

That's actually a good question. Pathname lookup relies on inodes being protected by RCU so "normal" filesystems definitely need to use RCU freeing of inodes. OTOH a filesystem could in theory refuse any attempt for RCU pathname walk (in its .d_revalidate/.d_compare callback) and then get away with freeing its inodes normally AFAICT. I don't see that happening anywhere in the tree but in theory it is possible with some effort... But frankly I don't see a good reason for that so all we should do is to document that .destroy_inode needs to free the inode structure through RCU if it uses page cache? Al?

Yub, it would be much better. However, how does this patch fix the problem? Although it can make only page_evictable safe, we could go with the page further and finally uses page->mapping, again.
For instance,

Quoted 6 lines

shrink_active_list

	page_evictable();
	..
	page_referened()
		page_rmapping
			page->mapping
Quoted 2 lines

This only checks the value of page->mapping, not deference page->mapping. So it should be safe.

Oops, you're right. I got confused. However, I want to make the lock consistent(i.e., use page_lock to protect address_space) but cannot come with better way.

Sorry for the noise, Huang.

Quoted 8 lines

Best Regards,
Huang, Ying

Quoted 4 lines

I think caller should lock the page to protect entire operation, which have been used more widely to pin a address_space.

Thanks.

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