All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alistair Popple <apopple@nvidia.com>
To: <akpm@linux-foundation.org>, <Felix.Kuehling@amd.com>,
	<linux-mm@kvack.org>, <rcampbell@nvidia.com>,
	<linux-ext4@vger.kernel.org>, <linux-xfs@vger.kernel.org>,
	Alex Sierra <alex.sierra@amd.com>
Cc: amd-gfx@lists.freedesktop.org, willy@infradead.org,
	jglisse@redhat.com, dri-devel@lists.freedesktop.org,
	jgg@nvidia.com, hch@lst.de
Subject: Re: [PATCH v2 03/11] mm/gup: migrate PIN_LONGTERM dev coherent pages to system
Date: Wed, 8 Dec 2021 22:31:58 +1100	[thread overview]
Message-ID: <2858338.J0npWUQLIM@nvdebian> (raw)
In-Reply-To: <20211206185251.20646-4-alex.sierra@amd.com>

On Tuesday, 7 December 2021 5:52:43 AM AEDT Alex Sierra wrote:
> Avoid long term pinning for Coherent device type pages. This could
> interfere with their own device memory manager.
> If caller tries to get user device coherent pages with PIN_LONGTERM flag
> set, those pages will be migrated back to system memory.
> 
> Signed-off-by: Alex Sierra <alex.sierra@amd.com>
> ---
>  mm/gup.c | 32 ++++++++++++++++++++++++++++++--
>  1 file changed, 30 insertions(+), 2 deletions(-)
> 
> diff --git a/mm/gup.c b/mm/gup.c
> index 886d6148d3d0..1572eacf07f4 100644
> --- a/mm/gup.c
> +++ b/mm/gup.c
> @@ -1689,17 +1689,37 @@ struct page *get_dump_page(unsigned long addr)
>  #endif /* CONFIG_ELF_CORE */
>  
>  #ifdef CONFIG_MIGRATION
> +static int migrate_device_page(unsigned long address,
> +				struct page *page)
> +{
> +	struct vm_area_struct *vma = find_vma(current->mm, address);
> +	struct vm_fault vmf = {
> +		.vma = vma,
> +		.address = address & PAGE_MASK,
> +		.flags = FAULT_FLAG_USER,
> +		.pgoff = linear_page_index(vma, address),
> +		.gfp_mask = GFP_KERNEL,
> +		.page = page,
> +	};
> +	if (page->pgmap && page->pgmap->ops->migrate_to_ram)
> +		return page->pgmap->ops->migrate_to_ram(&vmf);

How does this synchronise against pgmap being released? As I understand things
at this point we're not holding a reference on either the page or pgmap, so
the page and therefore the pgmap may have been freed.

I think a similar problem exists for device private fault handling as well and
it has been on my list of things to fix for a while. I think the solution is to
call try_get_page(), except it doesn't work with device pages due to the whole
refcount thing. That issue is blocking a fair bit of work now so I've started
looking into it.

> +
> +	return -EBUSY;
> +}
> +
>  /*
>   * 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.
>   * Return negative error if migration fails.
>   */
> -static long check_and_migrate_movable_pages(unsigned long nr_pages,
> +static long check_and_migrate_movable_pages(unsigned long start,
> +					    unsigned long nr_pages,
>  					    struct page **pages,
>  					    unsigned int gup_flags)
>  {
>  	unsigned long i;
> +	unsigned long page_index;
>  	unsigned long isolation_error_count = 0;
>  	bool drain_allow = true;
>  	LIST_HEAD(movable_page_list);
> @@ -1720,6 +1740,10 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages,
>  		 * If we get a movable page, since we are going to be pinning
>  		 * these entries, try to move them out if possible.
>  		 */
> +		if (is_device_page(head)) {
> +			page_index = i;
> +			goto unpin_pages;
> +		}
>  		if (!is_pinnable_page(head)) {
>  			if (PageHuge(head)) {
>  				if (!isolate_huge_page(head, &movable_page_list))
> @@ -1750,12 +1774,16 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages,
>  	if (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++)
>  			put_page(pages[i]);
>  	}
> +	if (is_device_page(head))
> +		return migrate_device_page(start + page_index * PAGE_SIZE, head);

This isn't very optimal - if a range contains more than one device page (which
seems likely) we will have to go around the whole gup/check_and_migrate loop
once for each device page which seems unnecessary. You should be able to either
build a list or migrate them as you go through the loop. I'm also currently
looking into how to extend migrate_pages() to support device pages which might
be useful here too.

> +
>  	if (!list_empty(&movable_page_list)) {
>  		ret = migrate_pages(&movable_page_list, alloc_migration_target,
>  				    NULL, (unsigned long)&mtc, MIGRATE_SYNC,
> @@ -1798,7 +1826,7 @@ static long __gup_longterm_locked(struct mm_struct *mm,
>  					     NULL, gup_flags);
>  		if (rc <= 0)
>  			break;
> -		rc = check_and_migrate_movable_pages(rc, pages, gup_flags);
> +		rc = check_and_migrate_movable_pages(start, rc, pages, gup_flags);
>  	} while (!rc);
>  	memalloc_pin_restore(flags);
>  
> 





WARNING: multiple messages have this Message-ID (diff)
From: Alistair Popple <apopple@nvidia.com>
To: <akpm@linux-foundation.org>, <Felix.Kuehling@amd.com>,
	<linux-mm@kvack.org>, <rcampbell@nvidia.com>,
	<linux-ext4@vger.kernel.org>, <linux-xfs@vger.kernel.org>,
	Alex Sierra <alex.sierra@amd.com>
Cc: <amd-gfx@lists.freedesktop.org>,
	<dri-devel@lists.freedesktop.org>, <hch@lst.de>, <jgg@nvidia.com>,
	<jglisse@redhat.com>, <willy@infradead.org>
Subject: Re: [PATCH v2 03/11] mm/gup: migrate PIN_LONGTERM dev coherent pages to system
Date: Wed, 8 Dec 2021 22:31:58 +1100	[thread overview]
Message-ID: <2858338.J0npWUQLIM@nvdebian> (raw)
In-Reply-To: <20211206185251.20646-4-alex.sierra@amd.com>

On Tuesday, 7 December 2021 5:52:43 AM AEDT Alex Sierra wrote:
> Avoid long term pinning for Coherent device type pages. This could
> interfere with their own device memory manager.
> If caller tries to get user device coherent pages with PIN_LONGTERM flag
> set, those pages will be migrated back to system memory.
> 
> Signed-off-by: Alex Sierra <alex.sierra@amd.com>
> ---
>  mm/gup.c | 32 ++++++++++++++++++++++++++++++--
>  1 file changed, 30 insertions(+), 2 deletions(-)
> 
> diff --git a/mm/gup.c b/mm/gup.c
> index 886d6148d3d0..1572eacf07f4 100644
> --- a/mm/gup.c
> +++ b/mm/gup.c
> @@ -1689,17 +1689,37 @@ struct page *get_dump_page(unsigned long addr)
>  #endif /* CONFIG_ELF_CORE */
>  
>  #ifdef CONFIG_MIGRATION
> +static int migrate_device_page(unsigned long address,
> +				struct page *page)
> +{
> +	struct vm_area_struct *vma = find_vma(current->mm, address);
> +	struct vm_fault vmf = {
> +		.vma = vma,
> +		.address = address & PAGE_MASK,
> +		.flags = FAULT_FLAG_USER,
> +		.pgoff = linear_page_index(vma, address),
> +		.gfp_mask = GFP_KERNEL,
> +		.page = page,
> +	};
> +	if (page->pgmap && page->pgmap->ops->migrate_to_ram)
> +		return page->pgmap->ops->migrate_to_ram(&vmf);

How does this synchronise against pgmap being released? As I understand things
at this point we're not holding a reference on either the page or pgmap, so
the page and therefore the pgmap may have been freed.

I think a similar problem exists for device private fault handling as well and
it has been on my list of things to fix for a while. I think the solution is to
call try_get_page(), except it doesn't work with device pages due to the whole
refcount thing. That issue is blocking a fair bit of work now so I've started
looking into it.

> +
> +	return -EBUSY;
> +}
> +
>  /*
>   * 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.
>   * Return negative error if migration fails.
>   */
> -static long check_and_migrate_movable_pages(unsigned long nr_pages,
> +static long check_and_migrate_movable_pages(unsigned long start,
> +					    unsigned long nr_pages,
>  					    struct page **pages,
>  					    unsigned int gup_flags)
>  {
>  	unsigned long i;
> +	unsigned long page_index;
>  	unsigned long isolation_error_count = 0;
>  	bool drain_allow = true;
>  	LIST_HEAD(movable_page_list);
> @@ -1720,6 +1740,10 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages,
>  		 * If we get a movable page, since we are going to be pinning
>  		 * these entries, try to move them out if possible.
>  		 */
> +		if (is_device_page(head)) {
> +			page_index = i;
> +			goto unpin_pages;
> +		}
>  		if (!is_pinnable_page(head)) {
>  			if (PageHuge(head)) {
>  				if (!isolate_huge_page(head, &movable_page_list))
> @@ -1750,12 +1774,16 @@ static long check_and_migrate_movable_pages(unsigned long nr_pages,
>  	if (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++)
>  			put_page(pages[i]);
>  	}
> +	if (is_device_page(head))
> +		return migrate_device_page(start + page_index * PAGE_SIZE, head);

This isn't very optimal - if a range contains more than one device page (which
seems likely) we will have to go around the whole gup/check_and_migrate loop
once for each device page which seems unnecessary. You should be able to either
build a list or migrate them as you go through the loop. I'm also currently
looking into how to extend migrate_pages() to support device pages which might
be useful here too.

> +
>  	if (!list_empty(&movable_page_list)) {
>  		ret = migrate_pages(&movable_page_list, alloc_migration_target,
>  				    NULL, (unsigned long)&mtc, MIGRATE_SYNC,
> @@ -1798,7 +1826,7 @@ static long __gup_longterm_locked(struct mm_struct *mm,
>  					     NULL, gup_flags);
>  		if (rc <= 0)
>  			break;
> -		rc = check_and_migrate_movable_pages(rc, pages, gup_flags);
> +		rc = check_and_migrate_movable_pages(start, rc, pages, gup_flags);
>  	} while (!rc);
>  	memalloc_pin_restore(flags);
>  
> 





  reply	other threads:[~2021-12-09 16:53 UTC|newest]

Thread overview: 52+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-06 18:52 [PATCH v2 00/11] Add MEMORY_DEVICE_COHERENT for coherent device memory mapping Alex Sierra
2021-12-06 18:52 ` Alex Sierra
2021-12-06 18:52 ` [PATCH v2 01/11] mm: add zone device coherent type memory support Alex Sierra
2021-12-06 18:52   ` Alex Sierra
2021-12-06 18:52 ` [PATCH v2 02/11] mm: add device coherent vma selection for memory migration Alex Sierra
2021-12-06 18:52   ` Alex Sierra
2021-12-06 18:52 ` [PATCH v2 03/11] mm/gup: migrate PIN_LONGTERM dev coherent pages to system Alex Sierra
2021-12-06 18:52   ` Alex Sierra
2021-12-08 11:31   ` Alistair Popple [this message]
2021-12-08 11:31     ` Alistair Popple
2021-12-08 13:53     ` Jason Gunthorpe
2021-12-08 13:53       ` Jason Gunthorpe
2021-12-09  1:45       ` Alistair Popple
2021-12-09  1:45         ` Alistair Popple
2021-12-09  2:53         ` Jason Gunthorpe
2021-12-09  2:53           ` Jason Gunthorpe
2021-12-08 16:58     ` Felix Kuehling
2021-12-08 16:58       ` Felix Kuehling
2021-12-08 17:30       ` Felix Kuehling
2021-12-08 17:30         ` Felix Kuehling
2021-12-08 18:55         ` Sierra Guiza, Alejandro (Alex)
2021-12-08 18:55           ` Sierra Guiza, Alejandro (Alex)
2021-12-09 10:53           ` Alistair Popple
2021-12-09 10:53             ` Alistair Popple
2021-12-09 16:29             ` Felix Kuehling
2021-12-09 16:29               ` Felix Kuehling
2021-12-09 16:54               ` Sierra Guiza, Alejandro (Alex)
2021-12-09 16:54                 ` Sierra Guiza, Alejandro (Alex)
2021-12-10  1:31                 ` Alistair Popple
2021-12-10  1:31                   ` Alistair Popple
2021-12-10 16:39                   ` Felix Kuehling
2021-12-10 16:39                     ` Felix Kuehling
2021-12-06 18:52 ` [PATCH v2 04/11] drm/amdkfd: add SPM support for SVM Alex Sierra
2021-12-06 18:52   ` Alex Sierra
2021-12-06 18:52 ` [PATCH v2 05/11] drm/amdkfd: coherent type as sys mem on migration to ram Alex Sierra
2021-12-06 18:52   ` Alex Sierra
2021-12-06 18:52 ` [PATCH v2 06/11] lib: test_hmm add ioctl to get zone device type Alex Sierra
2021-12-06 18:52   ` Alex Sierra
2021-12-06 18:52 ` [PATCH v2 07/11] lib: test_hmm add module param for " Alex Sierra
2021-12-06 18:52   ` Alex Sierra
2021-12-06 18:52 ` [PATCH v2 08/11] lib: add support for device coherent type in test_hmm Alex Sierra
2021-12-06 18:52   ` Alex Sierra
2022-01-03 20:24   ` Liam Howlett
2022-01-03 20:24     ` Liam Howlett
2021-12-06 18:52 ` [PATCH v2 09/11] tools: update hmm-test to support device coherent type Alex Sierra
2021-12-06 18:52   ` Alex Sierra
2021-12-06 18:52 ` [PATCH v2 10/11] tools: update test_hmm script to support SP config Alex Sierra
2021-12-06 18:52   ` Alex Sierra
2021-12-06 18:52 ` [PATCH v2 11/11] tools: add hmm gup test for long term pinned device pages Alex Sierra
2021-12-06 18:52   ` Alex Sierra
2021-12-07 19:31   ` Jason Gunthorpe
2021-12-07 19:31     ` Jason Gunthorpe

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=2858338.J0npWUQLIM@nvdebian \
    --to=apopple@nvidia.com \
    --cc=Felix.Kuehling@amd.com \
    --cc=akpm@linux-foundation.org \
    --cc=alex.sierra@amd.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=hch@lst.de \
    --cc=jgg@nvidia.com \
    --cc=jglisse@redhat.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.