From: Michal Hocko <mhocko@kernel.org>
To: Zi Yan <zi.yan@sent.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
"Kirill A . Shutemov" <kirill@shutemov.name>,
linux-kernel@vger.kernel.org, linux-mm@kvack.org,
Zi Yan <zi.yan@cs.rutgers.edu>
Subject: Re: [PATCH] Lock mmap_sem when calling migrate_pages() in do_move_pages_to_node()
Date: Tue, 30 Jan 2018 09:14:15 +0100 [thread overview]
Message-ID: <20180130081415.GO21609@dhcp22.suse.cz> (raw)
In-Reply-To: <20180130030011.4310-1-zi.yan@sent.com>
On Mon 29-01-18 22:00:11, Zi Yan wrote:
> From: Zi Yan <zi.yan@cs.rutgers.edu>
>
> migrate_pages() requires at least down_read(mmap_sem) to protect
> related page tables and VMAs from changing. Let's do it in
> do_page_moves() for both do_move_pages_to_node() and
> add_page_for_migration().
>
> Also add this lock requirement in the comment of migrate_pages().
This doesn't make much sense to me, to be honest. We are holding
mmap_sem for _read_ so we allow parallel updates like page faults
or unmaps. Therefore we are isolating pages prior to the migration.
The sole purpose of the mmap_sem in add_page_for_migration is to protect
from vma going away _while_ need it to get the proper page.
Moving the lock up is just wrong because it allows caller to hold the
lock for way too long if a lot of pages is migrated. Not only that,
it is even incorrect because we are doing get_user() (aka page fault)
and while read lock recursion is OK, we might block and deadlock when
there is a writer pending. I haven't checked the current implementation
of semaphores but I believe we do not allow recursive locking.
> Signed-off-by: Zi Yan <zi.yan@cs.rutgers.edu>
> ---
> mm/migrate.c | 13 +++++++++++--
> 1 file changed, 11 insertions(+), 2 deletions(-)
>
> diff --git a/mm/migrate.c b/mm/migrate.c
> index 5d0dc7b85f90..52d029953c32 100644
> --- a/mm/migrate.c
> +++ b/mm/migrate.c
> @@ -1354,6 +1354,9 @@ static int unmap_and_move_huge_page(new_page_t get_new_page,
> * or free list only if ret != 0.
> *
> * Returns the number of pages that were not migrated, or an error code.
> + *
> + * The caller must hold at least down_read(mmap_sem) for to-be-migrated pages
> + * to protect related page tables and VMAs from changing.
> */
> int migrate_pages(struct list_head *from, new_page_t get_new_page,
> free_page_t put_new_page, unsigned long private,
> @@ -1457,6 +1460,12 @@ static int store_status(int __user *status, int start, int value, int nr)
> return 0;
> }
>
> +/*
> + * Migrates the pages from pagelist and put back those not migrated.
> + *
> + * The caller must at least hold down_read(mmap_sem), which is required
> + * for migrate_pages()
> + */
> static int do_move_pages_to_node(struct mm_struct *mm,
> struct list_head *pagelist, int node)
> {
> @@ -1487,7 +1496,6 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
> unsigned int follflags;
> int err;
>
> - down_read(&mm->mmap_sem);
> err = -EFAULT;
> vma = find_vma(mm, addr);
> if (!vma || addr < vma->vm_start || !vma_migratable(vma))
> @@ -1540,7 +1548,6 @@ static int add_page_for_migration(struct mm_struct *mm, unsigned long addr,
> */
> put_page(page);
> out:
> - up_read(&mm->mmap_sem);
> return err;
> }
>
> @@ -1561,6 +1568,7 @@ static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
>
> migrate_prep();
>
> + down_read(&mm->mmap_sem);
> for (i = start = 0; i < nr_pages; i++) {
> const void __user *p;
> unsigned long addr;
> @@ -1628,6 +1636,7 @@ static int do_pages_move(struct mm_struct *mm, nodemask_t task_nodes,
> if (!err)
> err = err1;
> out:
> + up_read(&mm->mmap_sem);
> return err;
> }
>
> --
> 2.15.1
--
Michal Hocko
SUSE Labs
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
next prev parent reply other threads:[~2018-01-30 8:14 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-01-30 3:00 Zi Yan
2018-01-30 4:10 ` Hugh Dickins
2018-01-30 15:56 ` Zi Yan
2018-01-30 8:14 ` Michal Hocko [this message]
2018-01-30 15:52 ` Zi Yan
2018-01-30 16:10 ` Michal Hocko
2018-01-30 19:12 ` Zi Yan
2018-01-31 8:01 ` Michal Hocko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180130081415.GO21609@dhcp22.suse.cz \
--to=mhocko@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=kirill@shutemov.name \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=zi.yan@cs.rutgers.edu \
--cc=zi.yan@sent.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox