Commit 89cb0888 authored by Jan Kara's avatar Jan Kara Committed by Linus Torvalds

mm: migrate: provide buffer_migrate_page_norefs()

Provide a variant of buffer_migrate_page() that also checks whether there
are no unexpected references to buffer heads.  This function will then be
safe to use for block device pages.

[akpm@linux-foundation.org: remove EXPORT_SYMBOL(buffer_migrate_page_norefs)]
Link: http://lkml.kernel.org/r/20181211172143.7358-5-jack@suse.czSigned-off-by: default avatarJan Kara <jack@suse.cz>
Acked-by: default avatarMel Gorman <mgorman@suse.de>
Cc: Michal Hocko <mhocko@suse.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 84ade7c1
...@@ -3269,8 +3269,12 @@ extern int generic_check_addressable(unsigned, u64); ...@@ -3269,8 +3269,12 @@ extern int generic_check_addressable(unsigned, u64);
extern int buffer_migrate_page(struct address_space *, extern int buffer_migrate_page(struct address_space *,
struct page *, struct page *, struct page *, struct page *,
enum migrate_mode); enum migrate_mode);
extern int buffer_migrate_page_norefs(struct address_space *,
struct page *, struct page *,
enum migrate_mode);
#else #else
#define buffer_migrate_page NULL #define buffer_migrate_page NULL
#define buffer_migrate_page_norefs NULL
#endif #endif
extern int setattr_prepare(struct dentry *, struct iattr *); extern int setattr_prepare(struct dentry *, struct iattr *);
......
...@@ -743,13 +743,9 @@ static bool buffer_migrate_lock_buffers(struct buffer_head *head, ...@@ -743,13 +743,9 @@ static bool buffer_migrate_lock_buffers(struct buffer_head *head,
return true; return true;
} }
/* static int __buffer_migrate_page(struct address_space *mapping,
* Migration function for pages with buffers. This function can only be used struct page *newpage, struct page *page, enum migrate_mode mode,
* if the underlying filesystem guarantees that no other references to "page" bool check_refs)
* exist.
*/
int buffer_migrate_page(struct address_space *mapping,
struct page *newpage, struct page *page, enum migrate_mode mode)
{ {
struct buffer_head *bh, *head; struct buffer_head *bh, *head;
int rc; int rc;
...@@ -767,6 +763,33 @@ int buffer_migrate_page(struct address_space *mapping, ...@@ -767,6 +763,33 @@ int buffer_migrate_page(struct address_space *mapping,
if (!buffer_migrate_lock_buffers(head, mode)) if (!buffer_migrate_lock_buffers(head, mode))
return -EAGAIN; return -EAGAIN;
if (check_refs) {
bool busy;
bool invalidated = false;
recheck_buffers:
busy = false;
spin_lock(&mapping->private_lock);
bh = head;
do {
if (atomic_read(&bh->b_count)) {
busy = true;
break;
}
bh = bh->b_this_page;
} while (bh != head);
spin_unlock(&mapping->private_lock);
if (busy) {
if (invalidated) {
rc = -EAGAIN;
goto unlock_buffers;
}
invalidate_bh_lrus();
invalidated = true;
goto recheck_buffers;
}
}
rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode, 0); rc = migrate_page_move_mapping(mapping, newpage, page, NULL, mode, 0);
if (rc != MIGRATEPAGE_SUCCESS) if (rc != MIGRATEPAGE_SUCCESS)
goto unlock_buffers; goto unlock_buffers;
...@@ -803,7 +826,30 @@ int buffer_migrate_page(struct address_space *mapping, ...@@ -803,7 +826,30 @@ int buffer_migrate_page(struct address_space *mapping,
return rc; return rc;
} }
/*
* Migration function for pages with buffers. This function can only be used
* if the underlying filesystem guarantees that no other references to "page"
* exist. For example attached buffer heads are accessed only under page lock.
*/
int buffer_migrate_page(struct address_space *mapping,
struct page *newpage, struct page *page, enum migrate_mode mode)
{
return __buffer_migrate_page(mapping, newpage, page, mode, false);
}
EXPORT_SYMBOL(buffer_migrate_page); EXPORT_SYMBOL(buffer_migrate_page);
/*
* Same as above except that this variant is more careful and checks that there
* are also no buffer head references. This function is the right one for
* mappings where buffer heads are directly looked up and referenced (such as
* block device mappings).
*/
int buffer_migrate_page_norefs(struct address_space *mapping,
struct page *newpage, struct page *page, enum migrate_mode mode)
{
return __buffer_migrate_page(mapping, newpage, page, mode, true);
}
#endif #endif
/* /*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment