linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: John Hubbard <jhubbard@nvidia.com>
To: Alistair Popple <apopple@nvidia.com>,
	linux-mm@kvack.org, akpm@linux-foundation.org
Cc: linux-kernel@vger.kernel.org, "Sierra Guiza,
	Alejandro (Alex)" <alex.sierra@amd.com>,
	Chaitanya Kulkarni <kch@nvidia.com>,
	Dan Williams <dan.j.williams@intel.com>,
	Felix Kuehling <Felix.Kuehling@amd.com>,
	Jason Gunthorpe <jgg@nvidia.com>,
	Logan Gunthorpe <logang@deltatee.com>,
	Miaohe Lin <linmiaohe@huawei.com>,
	Muchun Song <songmuchun@bytedance.com>,
	Ralph Campbell <rcampbell@nvidia.com>,
	David Hildenbrand <david@redhat.com>,
	Matthew Wilcox <willy@infradead.org>
Subject: Re: [PATCH v4 2/2] mm/gup.c: Refactor check_and_migrate_movable_pages()
Date: Fri, 12 Aug 2022 00:05:18 -0700	[thread overview]
Message-ID: <dc8fd102-ba30-d980-bdbb-11f39326103d@nvidia.com> (raw)
In-Reply-To: <6b61e9bf7c65f78524db32ba3e65a7eb6b8a76a0.1660269441.git-series.apopple@nvidia.com>

On 8/11/22 19:13, Alistair Popple wrote:
> When pinning pages with FOLL_LONGTERM check_and_migrate_movable_pages()
> is called to migrate pages out of zones which should not contain any
> longterm pinned pages.
> 
> When migration succeeds all pages will have been unpinned so pinning
> needs to be retried. Migration can also fail, in which case the pages
> will also have been unpinned but the operation should not be retried. If
> all pages are in the correct zone nothing will be unpinned and no retry
> is required.
> 
> The logic in check_and_migrate_movable_pages() tracks unnecessary state
> and the return codes for each case are difficult to follow. Refactor the
> code to clean this up. No behaviour change is intended.
> 
> Signed-off-by: Alistair Popple <apopple@nvidia.com>

OK, I've finally convinced myself that this is a correct transformation.
This cleanup does help clarify things, definitely.

I've got two documentation additions (and changes) to suggest, below, and a
couple of too-long lines, but the code itself looks good, so with those
tweaks or something approximating them, please feel free to add:

Reviewed-by: John Hubbard <jhubbard@nvidia.com>

...

> +/*
> + * Check whether all pages are pinnable. If some pages are not pinnable migrate
> + * them and unpin all the pages. Returns -EAGAIN if pages were unpinned or zero
> + * if all pages are pinnable and in the right zone. Other errors indicate
> + * migration failure.
> + */

Instead of the above, I'd like to suggest this:

/*
  * Check whether all pages are *allowed* to be pinned. Rather confusingly, all
  * pages in the range are required to be pinned via FOLL_PIN, before calling
  * this routine.
  *
  * If any pages in the range are not allowed to be pinned, then this routine
  * will migrate those pages away, unpin all the pages in the range and return
  * -EAGAIN. The caller should re-pin the entire range with FOLL_PIN and then
  * call this routine again.
  *
  * If an error other than -EAGAIN occurs, this indicates a migration failure.
  * The caller should give up, and propagate the error back up the call stack.
  *
  * If everything is OK and all pages in the range are allowed to be pinned, then
  * this routine leaves all pages pinned and returns zero for success.
  */

> +static long check_and_migrate_movable_pages(unsigned long nr_pages,
> +					    struct page **pages)
> +{
> +	int ret;
> +	unsigned long collected;
> +	LIST_HEAD(movable_page_list);
> +
> +	collected = collect_longterm_unpinnable_pages(&movable_page_list, nr_pages, pages);

There is no reason to exceed 80 cols here.

> +	if (!collected)
> +		return 0;
> +
> +	ret = migrate_longterm_unpinnable_pages(&movable_page_list, nr_pages, pages);

Nor here.

...

> @@ -2051,10 +2079,10 @@ static long __gup_longterm_locked(struct mm_struct *mm,
>   			break;

...and in this routine, let's fortify the comment like so:

@@ -2068,7 +2078,15 @@ static long __gup_longterm_locked(struct mm_struct *mm,
  	if (!(gup_flags & FOLL_LONGTERM))
  		return __get_user_pages_locked(mm, start, nr_pages, pages, vmas,
  					       NULL, gup_flags);
-	/* check_and_migrate_movable_pages() assumes pages have been pinned. */
+	/*
+	 * If we get to this point then FOLL_LONGTERM is set. And FOLL_LONGTERM
+	 * implies FOLL_PIN (although the reverse is not true). And that, in
+	 * turn, makes it correct to unconditionally call
+	 * check_and_migrate_movable_pages(), which assumes pages have been
+	 * pinned via FOLL_PIN.
+	 *
+	 * Enforce the above reasoning, by asserting that FOLL_PIN is set:
+	 */
  	if (WARN_ON(!(gup_flags & FOLL_PIN)))
  		return -EINVAL;
  	flags = memalloc_pin_save();


...and with that, it's actually possible for the reader to work their way
through this story, I think.


thanks,
-- 
John Hubbard
NVIDIA


  reply	other threads:[~2022-08-12  7:05 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-12  2:13 [PATCH v4 1/2] mm/gup.c: Don't pass gup_flags to check_and_migrate_movable_pages() Alistair Popple
2022-08-12  2:13 ` [PATCH v4 2/2] mm/gup.c: Refactor check_and_migrate_movable_pages() Alistair Popple
2022-08-12  7:05   ` John Hubbard [this message]
2022-08-15 16:15   ` Jason Gunthorpe
2022-08-16  5:29     ` Alistair Popple
2022-08-16 11:29       ` Jason Gunthorpe
2022-08-17  2:01         ` Alistair Popple
2022-08-17 12:09           ` Jason Gunthorpe
2022-08-17 20:35             ` John Hubbard
2022-08-17 22:50               ` Jason Gunthorpe
2022-08-17 23:05                 ` John Hubbard
2022-08-17 23:24                 ` Alistair Popple
2022-08-17 23:40                   ` Jason Gunthorpe
2022-08-12 12:57 ` [PATCH v4 1/2] mm/gup.c: Don't pass gup_flags to check_and_migrate_movable_pages() Matthew Wilcox
2022-08-12 18:02   ` John Hubbard
2022-08-12 18:11     ` Matthew Wilcox
2022-08-15  5:52       ` Alistair Popple

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=dc8fd102-ba30-d980-bdbb-11f39326103d@nvidia.com \
    --to=jhubbard@nvidia.com \
    --cc=Felix.Kuehling@amd.com \
    --cc=akpm@linux-foundation.org \
    --cc=alex.sierra@amd.com \
    --cc=apopple@nvidia.com \
    --cc=dan.j.williams@intel.com \
    --cc=david@redhat.com \
    --cc=jgg@nvidia.com \
    --cc=kch@nvidia.com \
    --cc=linmiaohe@huawei.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=logang@deltatee.com \
    --cc=rcampbell@nvidia.com \
    --cc=songmuchun@bytedance.com \
    --cc=willy@infradead.org \
    /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