From: Alistair Popple <apopple@nvidia.com>
To: "Teterevkov, Ivan" <Ivan.Teterevkov@amd.com>
Cc: "linux-mm@kvack.org" <linux-mm@kvack.org>,
"jhubbard@nvidia.com" <jhubbard@nvidia.com>,
"jack@suse.cz" <jack@suse.cz>,
"rppt@linux.ibm.com" <rppt@linux.ibm.com>,
"jglisse@redhat.com" <jglisse@redhat.com>,
"ira.weiny@intel.com" <ira.weiny@intel.com>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: find_get_page() VS pin_user_pages()
Date: Wed, 12 Apr 2023 09:38:03 +1000 [thread overview]
Message-ID: <87mt3ehti4.fsf@nvidia.com> (raw)
In-Reply-To: <PH0PR12MB5606D4611050BC8B1CC430FEF09A9@PH0PR12MB5606.namprd12.prod.outlook.com>
"Teterevkov, Ivan" <Ivan.Teterevkov@amd.com> writes:
> Hello folks,
>
> I work with an application which aims to share memory in the userspace and
> interact with the NIC DMA. The memory allocation workflow begins in the
> userspace, which creates a new file backed by 2MiB hugepages with
> memfd_create(MFD_HUGETLB, MFD_HUGE_2MB) and fallocate(). Then the userspace
> makes an IOCTL to the kernel module with the file descriptor and size so that
> the kernel module can get the struct page with find_get_page(). Then the kernel
> module calls dma_map_single(page_address(page)) for NIC, which concludes the
> datapath. The allocated memory may (significantly) outlive the originating
> userspace application. The hugepages stay mapped with NIC, and the kernel
> module wants to continue using them and map to other applications that come and
> go with vm_mmap().
>
> I am studying the pin_user_pages*() family of functions, and I wonder if the
> outlined workflow requires it. The hugepages do not page out, but they can move
> as they may be allocated with GFP_HIGHUSER_MOVABLE. However, find_get_page()
> must increment the page reference counter without mapping and prevent it from
> moving. In particular, https://docs.kernel.org/mm/page_migration.html:
I'm not super familiar with the memfd_create()/find_get_page() workflow
but is there some reason you're not using pin_user_pages*(FOLL_LONGTERM)
to get the struct page initially? You're description above sounds
exactly the use case pin_user_pages() was designed for because it marks
the page as being writen to by DMA, makes sure it's not in a movable
zone, etc.
>> How migrate_pages() works
>> ...
>> Steps:
>> ...
>> 4. All the page table references to the page are converted to migration
>> entries. This decreases the mapcount of a page. If the resulting mapcount
>> is not zero then we do not migrate the page.
>
> Does find_get_page() achieve that condition or does the outlined workflow
> still requires pin_user_pages*() for safe DMA?
Yes. The extra page reference will prevent the migration regardless of
mapcount being zero or not. See folio_expected_refs() for how the extra
reference is detected.
> Thanks in advance,
> Ivan
next prev parent reply other threads:[~2023-04-11 23:46 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-11 19:43 Teterevkov, Ivan
2023-04-11 23:38 ` Alistair Popple [this message]
2023-04-12 9:04 ` Teterevkov, Ivan
2023-04-12 10:41 ` Jan Kara
2023-04-12 12:13 ` Teterevkov, Ivan
2023-04-12 8:17 ` David Hildenbrand
2023-04-12 9:43 ` Teterevkov, Ivan
2023-04-12 8:41 ` David Howells
2023-04-13 12:41 ` David Hildenbrand
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=87mt3ehti4.fsf@nvidia.com \
--to=apopple@nvidia.com \
--cc=Ivan.Teterevkov@amd.com \
--cc=ira.weiny@intel.com \
--cc=jack@suse.cz \
--cc=jglisse@redhat.com \
--cc=jhubbard@nvidia.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=rppt@linux.ibm.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