All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alistair Popple <apopple@nvidia.com>
To: <akpm@linux-foundation.org>, <linux-mm@kvack.org>,
	Felix Kuehling <felix.kuehling@amd.com>
Cc: alex.sierra@amd.com, rcampbell@nvidia.com, willy@infradead.org,
	jhubbard@nvidia.com, dri-devel@lists.freedesktop.org,
	linux-xfs@vger.kernel.org, jglisse@redhat.com,
	amd-gfx@lists.freedesktop.org, jgg@nvidia.com,
	linux-ext4@vger.kernel.org, hch@lst.de
Subject: Re: [PATCH 2/3] mm/gup.c: Migrate device coherent pages when pinning instead of failing
Date: Mon, 7 Feb 2022 15:19:44 +1100	[thread overview]
Message-ID: <2160866.MLLZV48HG3@nvdebian> (raw)
In-Reply-To: <49253aca-5c0b-84d4-4b2a-13426b1064ec@amd.com>

On Wednesday, 2 February 2022 2:03:01 AM AEDT Felix Kuehling wrote:
> 
> Am 2022-02-01 um 02:05 schrieb Alistair Popple:
> > Currently any attempts to pin a device coherent page will fail. This is
> > because device coherent pages need to be managed by a device driver, and
> > pinning them would prevent a driver from migrating them off the device.
> >
> > However this is no reason to fail pinning of these pages. These are
> > coherent and accessible from the CPU so can be migrated just like
> > pinning ZONE_MOVABLE pages. So instead of failing all attempts to pin
> > them first try migrating them out of ZONE_DEVICE.
> >
> > Signed-off-by: Alistair Popple <apopple@nvidia.com>
> 
> Thank you for working on this. I have two questions inline.
> 
> Other than that, patches 1 and 2 are
> 
> Acked-by: Felix Kuehling <Felix.Kuehling@amd.com>
> 
> 
> > ---
> >   mm/gup.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++------
> >   1 file changed, 95 insertions(+), 10 deletions(-)
> >
> > diff --git a/mm/gup.c b/mm/gup.c
> > index f596b93..2cbef54 100644
> > --- a/mm/gup.c
> > +++ b/mm/gup.c
> > @@ -1834,6 +1834,60 @@ struct page *get_dump_page(unsigned long addr)
> >   
> >   #ifdef CONFIG_MIGRATION
> >   /*
> > + * Migrates a device coherent page back to normal memory. Caller should have a
> > + * reference on page which will be copied to the new page if migration is
> > + * successful or dropped on failure.
> > + */
> > +static struct page *migrate_device_page(struct page *page,
> > +					unsigned int gup_flags)
> > +{
> > +	struct page *dpage;
> > +	struct migrate_vma args;
> > +	unsigned long src_pfn, dst_pfn = 0;
> > +
> > +	lock_page(page);
> > +	src_pfn = migrate_pfn(page_to_pfn(page)) | MIGRATE_PFN_MIGRATE;
> > +	args.src = &src_pfn;
> > +	args.dst = &dst_pfn;
> > +	args.cpages = 1;
> > +	args.npages = 1;
> > +	args.vma = NULL;
> > +	migrate_vma_setup(&args);
> > +	if (!(src_pfn & MIGRATE_PFN_MIGRATE))
> > +		return NULL;
> > +
> > +	dpage = alloc_pages(GFP_USER | __GFP_NOWARN, 0);
> 
> Don't you need to check dpage for NULL before the try_grab_page call below?

Yes, thanks for pointing that out. Will fix for v2.

> > +
> > +	/*
> > +	 * get/pin the new page now so we don't have to retry gup after
> > +	 * migrating. We already have a reference so this should never fail.
> > +	 */
> > +	if (WARN_ON_ONCE(!try_grab_page(dpage, gup_flags))) {
> > +		__free_pages(dpage, 0);
> > +		dpage = NULL;
> > +	}
> > +
> > +	if (dpage) {
> > +		lock_page(dpage);
> > +		dst_pfn = migrate_pfn(page_to_pfn(dpage));
> > +	}
> > +
> > +	migrate_vma_pages(&args);
> > +	if (src_pfn & MIGRATE_PFN_MIGRATE)
> > +		copy_highpage(dpage, page);
> 
> Can't dpage can be NULL here as well?

No - migrate_vma_pages() will clear src_pfn & MIGRATE_PFN_MIGRATE if no
destination page is provided in dst_pfn.

> Regards,
>    Felix
> 
> 
> > +	migrate_vma_finalize(&args);
> > +	if (dpage && !(src_pfn & MIGRATE_PFN_MIGRATE)) {
> > +		if (gup_flags & FOLL_PIN)
> > +			unpin_user_page(dpage);
> > +		else
> > +			put_page(dpage);
> > +		dpage = NULL;
> > +	}
> > +
> > +	return dpage;
> > +}
> > +
> > +/*
> >    * Check whether all pages are pinnable, if so return number of pages.  If some
> >    * pages are not pinnable, migrate them, and unpin all pages. Return zero if
> >    * pages were migrated, or if some pages were not successfully isolated.
> > @@ -1861,15 +1915,40 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages,
> >   			continue;
> >   		prev_head = head;
> >   		/*
> > -		 * If we get a movable page, since we are going to be pinning
> > -		 * these entries, try to move them out if possible.
> > +		 * Device coherent pages are managed by a driver and should not
> > +		 * be pinned indefinitely as it prevents the driver moving the
> > +		 * page. So when trying to pin with FOLL_LONGTERM instead try
> > +		 * migrating page out of device memory.
> >   		 */
> >   		if (is_dev_private_or_coherent_page(head)) {
> > +			/*
> > +			 * device private pages will get faulted in during gup
> > +			 * so it shouldn't be possible to see one here.
> > +			 */
> >   			WARN_ON_ONCE(is_device_private_page(head));
> > -			ret = -EFAULT;
> > -			goto unpin_pages;
> > +			WARN_ON_ONCE(PageCompound(head));
> > +
> > +			/*
> > +			 * migration will fail if the page is pinned, so convert
> > +			 * the pin on the source page to a normal reference.
> > +			 */
> > +			if (gup_flags & FOLL_PIN) {
> > +				get_page(head);
> > +				unpin_user_page(head);
> > +			}
> > +
> > +			pages[i] = migrate_device_page(head, gup_flags);
> > +			if (!pages[i]) {
> > +				ret = -EBUSY;
> > +				break;
> > +			}
> > +			continue;
> >   		}
> >   
> > +		/*
> > +		 * If we get a movable page, since we are going to be pinning
> > +		 * these entries, try to move them out if possible.
> > +		 */
> >   		if (!is_pinnable_page(head)) {
> >   			if (PageHuge(head)) {
> >   				if (!isolate_huge_page(head, &movable_page_list))
> > @@ -1897,16 +1976,22 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages,
> >   	 * If list is empty, and no isolation errors, means that all pages are
> >   	 * in the correct zone.
> >   	 */
> > -	if (list_empty(&movable_page_list) && !isolation_error_count)
> > +	if (!ret && list_empty(&movable_page_list) && !isolation_error_count)
> >   		return nr_pages;
> >   
> > -unpin_pages:
> > -	if (gup_flags & FOLL_PIN) {
> > -		unpin_user_pages(pages, nr_pages);
> > -	} else {
> > -		for (i = 0; i < nr_pages; i++)
> > +	for (i = 0; i < nr_pages; i++)
> > +		if (!pages[i])
> > +			continue;
> > +		else if (gup_flags & FOLL_PIN)
> > +			unpin_user_page(pages[i]);
> > +		else
> >   			put_page(pages[i]);
> > +
> > +	if (ret && !list_empty(&movable_page_list)) {
> > +		putback_movable_pages(&movable_page_list);
> > +		return ret;
> >   	}
> > +
> >   	if (!list_empty(&movable_page_list)) {
> >   		ret = migrate_pages(&movable_page_list, alloc_migration_target,
> >   				    NULL, (unsigned long)&mtc, MIGRATE_SYNC,
> 





WARNING: multiple messages have this Message-ID (diff)
From: Alistair Popple <apopple@nvidia.com>
To: <akpm@linux-foundation.org>, <linux-mm@kvack.org>,
	Felix Kuehling <felix.kuehling@amd.com>
Cc: <rcampbell@nvidia.com>, <linux-ext4@vger.kernel.org>,
	<linux-xfs@vger.kernel.org>, <amd-gfx@lists.freedesktop.org>,
	<dri-devel@lists.freedesktop.org>, <hch@lst.de>, <jgg@nvidia.com>,
	<jglisse@redhat.com>, <willy@infradead.org>,
	<alex.sierra@amd.com>, <jhubbard@nvidia.com>
Subject: Re: [PATCH 2/3] mm/gup.c: Migrate device coherent pages when pinning instead of failing
Date: Mon, 7 Feb 2022 15:19:44 +1100	[thread overview]
Message-ID: <2160866.MLLZV48HG3@nvdebian> (raw)
In-Reply-To: <49253aca-5c0b-84d4-4b2a-13426b1064ec@amd.com>

On Wednesday, 2 February 2022 2:03:01 AM AEDT Felix Kuehling wrote:
> 
> Am 2022-02-01 um 02:05 schrieb Alistair Popple:
> > Currently any attempts to pin a device coherent page will fail. This is
> > because device coherent pages need to be managed by a device driver, and
> > pinning them would prevent a driver from migrating them off the device.
> >
> > However this is no reason to fail pinning of these pages. These are
> > coherent and accessible from the CPU so can be migrated just like
> > pinning ZONE_MOVABLE pages. So instead of failing all attempts to pin
> > them first try migrating them out of ZONE_DEVICE.
> >
> > Signed-off-by: Alistair Popple <apopple@nvidia.com>
> 
> Thank you for working on this. I have two questions inline.
> 
> Other than that, patches 1 and 2 are
> 
> Acked-by: Felix Kuehling <Felix.Kuehling@amd.com>
> 
> 
> > ---
> >   mm/gup.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++------
> >   1 file changed, 95 insertions(+), 10 deletions(-)
> >
> > diff --git a/mm/gup.c b/mm/gup.c
> > index f596b93..2cbef54 100644
> > --- a/mm/gup.c
> > +++ b/mm/gup.c
> > @@ -1834,6 +1834,60 @@ struct page *get_dump_page(unsigned long addr)
> >   
> >   #ifdef CONFIG_MIGRATION
> >   /*
> > + * Migrates a device coherent page back to normal memory. Caller should have a
> > + * reference on page which will be copied to the new page if migration is
> > + * successful or dropped on failure.
> > + */
> > +static struct page *migrate_device_page(struct page *page,
> > +					unsigned int gup_flags)
> > +{
> > +	struct page *dpage;
> > +	struct migrate_vma args;
> > +	unsigned long src_pfn, dst_pfn = 0;
> > +
> > +	lock_page(page);
> > +	src_pfn = migrate_pfn(page_to_pfn(page)) | MIGRATE_PFN_MIGRATE;
> > +	args.src = &src_pfn;
> > +	args.dst = &dst_pfn;
> > +	args.cpages = 1;
> > +	args.npages = 1;
> > +	args.vma = NULL;
> > +	migrate_vma_setup(&args);
> > +	if (!(src_pfn & MIGRATE_PFN_MIGRATE))
> > +		return NULL;
> > +
> > +	dpage = alloc_pages(GFP_USER | __GFP_NOWARN, 0);
> 
> Don't you need to check dpage for NULL before the try_grab_page call below?

Yes, thanks for pointing that out. Will fix for v2.

> > +
> > +	/*
> > +	 * get/pin the new page now so we don't have to retry gup after
> > +	 * migrating. We already have a reference so this should never fail.
> > +	 */
> > +	if (WARN_ON_ONCE(!try_grab_page(dpage, gup_flags))) {
> > +		__free_pages(dpage, 0);
> > +		dpage = NULL;
> > +	}
> > +
> > +	if (dpage) {
> > +		lock_page(dpage);
> > +		dst_pfn = migrate_pfn(page_to_pfn(dpage));
> > +	}
> > +
> > +	migrate_vma_pages(&args);
> > +	if (src_pfn & MIGRATE_PFN_MIGRATE)
> > +		copy_highpage(dpage, page);
> 
> Can't dpage can be NULL here as well?

No - migrate_vma_pages() will clear src_pfn & MIGRATE_PFN_MIGRATE if no
destination page is provided in dst_pfn.

> Regards,
>    Felix
> 
> 
> > +	migrate_vma_finalize(&args);
> > +	if (dpage && !(src_pfn & MIGRATE_PFN_MIGRATE)) {
> > +		if (gup_flags & FOLL_PIN)
> > +			unpin_user_page(dpage);
> > +		else
> > +			put_page(dpage);
> > +		dpage = NULL;
> > +	}
> > +
> > +	return dpage;
> > +}
> > +
> > +/*
> >    * Check whether all pages are pinnable, if so return number of pages.  If some
> >    * pages are not pinnable, migrate them, and unpin all pages. Return zero if
> >    * pages were migrated, or if some pages were not successfully isolated.
> > @@ -1861,15 +1915,40 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages,
> >   			continue;
> >   		prev_head = head;
> >   		/*
> > -		 * If we get a movable page, since we are going to be pinning
> > -		 * these entries, try to move them out if possible.
> > +		 * Device coherent pages are managed by a driver and should not
> > +		 * be pinned indefinitely as it prevents the driver moving the
> > +		 * page. So when trying to pin with FOLL_LONGTERM instead try
> > +		 * migrating page out of device memory.
> >   		 */
> >   		if (is_dev_private_or_coherent_page(head)) {
> > +			/*
> > +			 * device private pages will get faulted in during gup
> > +			 * so it shouldn't be possible to see one here.
> > +			 */
> >   			WARN_ON_ONCE(is_device_private_page(head));
> > -			ret = -EFAULT;
> > -			goto unpin_pages;
> > +			WARN_ON_ONCE(PageCompound(head));
> > +
> > +			/*
> > +			 * migration will fail if the page is pinned, so convert
> > +			 * the pin on the source page to a normal reference.
> > +			 */
> > +			if (gup_flags & FOLL_PIN) {
> > +				get_page(head);
> > +				unpin_user_page(head);
> > +			}
> > +
> > +			pages[i] = migrate_device_page(head, gup_flags);
> > +			if (!pages[i]) {
> > +				ret = -EBUSY;
> > +				break;
> > +			}
> > +			continue;
> >   		}
> >   
> > +		/*
> > +		 * If we get a movable page, since we are going to be pinning
> > +		 * these entries, try to move them out if possible.
> > +		 */
> >   		if (!is_pinnable_page(head)) {
> >   			if (PageHuge(head)) {
> >   				if (!isolate_huge_page(head, &movable_page_list))
> > @@ -1897,16 +1976,22 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages,
> >   	 * If list is empty, and no isolation errors, means that all pages are
> >   	 * in the correct zone.
> >   	 */
> > -	if (list_empty(&movable_page_list) && !isolation_error_count)
> > +	if (!ret && list_empty(&movable_page_list) && !isolation_error_count)
> >   		return nr_pages;
> >   
> > -unpin_pages:
> > -	if (gup_flags & FOLL_PIN) {
> > -		unpin_user_pages(pages, nr_pages);
> > -	} else {
> > -		for (i = 0; i < nr_pages; i++)
> > +	for (i = 0; i < nr_pages; i++)
> > +		if (!pages[i])
> > +			continue;
> > +		else if (gup_flags & FOLL_PIN)
> > +			unpin_user_page(pages[i]);
> > +		else
> >   			put_page(pages[i]);
> > +
> > +	if (ret && !list_empty(&movable_page_list)) {
> > +		putback_movable_pages(&movable_page_list);
> > +		return ret;
> >   	}
> > +
> >   	if (!list_empty(&movable_page_list)) {
> >   		ret = migrate_pages(&movable_page_list, alloc_migration_target,
> >   				    NULL, (unsigned long)&mtc, MIGRATE_SYNC,
> 





  reply	other threads:[~2022-02-07  9:10 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-01  7:05 [PATCH 0/3] Migrate device coherent pages on get_user_pages() Alistair Popple
2022-02-01  7:05 ` Alistair Popple
2022-02-01  7:05 ` [PATCH 1/3] migrate.c: Remove vma check in migrate_vma_setup() Alistair Popple
2022-02-01  7:05   ` Alistair Popple
2022-02-01  7:05 ` [PATCH 2/3] mm/gup.c: Migrate device coherent pages when pinning instead of failing Alistair Popple
2022-02-01  7:05   ` Alistair Popple
2022-02-01 15:03   ` Felix Kuehling
2022-02-01 15:03     ` Felix Kuehling
2022-02-07  4:19     ` Alistair Popple [this message]
2022-02-07  4:19       ` Alistair Popple
2022-02-01  7:05 ` [PATCH 3/3] tools: add hmm gup test for long term pinned device pages Alistair Popple
2022-02-01  7:05   ` Alistair Popple
2022-02-01 15:09   ` Felix Kuehling
2022-02-01 15:09     ` Felix Kuehling

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=2160866.MLLZV48HG3@nvdebian \
    --to=apopple@nvidia.com \
    --cc=akpm@linux-foundation.org \
    --cc=alex.sierra@amd.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=felix.kuehling@amd.com \
    --cc=hch@lst.de \
    --cc=jgg@nvidia.com \
    --cc=jglisse@redhat.com \
    --cc=jhubbard@nvidia.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-xfs@vger.kernel.org \
    --cc=rcampbell@nvidia.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.