Skip to content

Commit f7ebd9e

Browse files
Ralph CampbellJonathan Corbet
authored andcommitted
mm/doc: add usage description for migrate_vma_*()
The migrate_vma_setup(), migrate_vma_pages(), and migrate_vma_finalize() API usage by device drivers is not well documented. Add a description for how device drivers are expected to use it. Signed-off-by: Ralph Campbell <rcampbell@nvidia.com> Reviewed-by: Alistair Popple <apopple@nvidia.com> Link: https://lore.kernel.org/r/20200909212956.20104-1-rcampbell@nvidia.com Signed-off-by: Jonathan Corbet <corbet@lwn.net>
1 parent b854e83 commit f7ebd9e

1 file changed

Lines changed: 133 additions & 4 deletions

File tree

‎Documentation/vm/hmm.rst‎

Lines changed: 133 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,139 @@ map those pages from the CPU side.
271271
Migration to and from device memory
272272
===================================
273273

274-
Because the CPU cannot access device memory, migration must use the device DMA
275-
engine to perform copy from and to device memory. For this we need to use
276-
migrate_vma_setup(), migrate_vma_pages(), and migrate_vma_finalize() helpers.
277-
274+
Because the CPU cannot access device memory directly, the device driver must
275+
use hardware DMA or device specific load/store instructions to migrate data.
276+
The migrate_vma_setup(), migrate_vma_pages(), and migrate_vma_finalize()
277+
functions are designed to make drivers easier to write and to centralize common
278+
code across drivers.
279+
280+
Before migrating pages to device private memory, special device private
281+
``struct page`` need to be created. These will be used as special "swap"
282+
page table entries so that a CPU process will fault if it tries to access
283+
a page that has been migrated to device private memory.
284+
285+
These can be allocated and freed with::
286+
287+
struct resource *res;
288+
struct dev_pagemap pagemap;
289+
290+
res = request_free_mem_region(&iomem_resource, /* number of bytes */,
291+
"name of driver resource");
292+
pagemap.type = MEMORY_DEVICE_PRIVATE;
293+
pagemap.range.start = res->start;
294+
pagemap.range.end = res->end;
295+
pagemap.nr_range = 1;
296+
pagemap.ops = &device_devmem_ops;
297+
memremap_pages(&pagemap, numa_node_id());
298+
299+
memunmap_pages(&pagemap);
300+
release_mem_region(pagemap.range.start, range_len(&pagemap.range));
301+
302+
There are also devm_request_free_mem_region(), devm_memremap_pages(),
303+
devm_memunmap_pages(), and devm_release_mem_region() when the resources can
304+
be tied to a ``struct device``.
305+
306+
The overall migration steps are similar to migrating NUMA pages within system
307+
memory (see :ref:`Page migration <page_migration>`) but the steps are split
308+
between device driver specific code and shared common code:
309+
310+
1. ``mmap_read_lock()``
311+
312+
The device driver has to pass a ``struct vm_area_struct`` to
313+
migrate_vma_setup() so the mmap_read_lock() or mmap_write_lock() needs to
314+
be held for the duration of the migration.
315+
316+
2. ``migrate_vma_setup(struct migrate_vma *args)``
317+
318+
The device driver initializes the ``struct migrate_vma`` fields and passes
319+
the pointer to migrate_vma_setup(). The ``args->flags`` field is used to
320+
filter which source pages should be migrated. For example, setting
321+
``MIGRATE_VMA_SELECT_SYSTEM`` will only migrate system memory and
322+
``MIGRATE_VMA_SELECT_DEVICE_PRIVATE`` will only migrate pages residing in
323+
device private memory. If the latter flag is set, the ``args->pgmap_owner``
324+
field is used to identify device private pages owned by the driver. This
325+
avoids trying to migrate device private pages residing in other devices.
326+
Currently only anonymous private VMA ranges can be migrated to or from
327+
system memory and device private memory.
328+
329+
One of the first steps migrate_vma_setup() does is to invalidate other
330+
device's MMUs with the ``mmu_notifier_invalidate_range_start(()`` and
331+
``mmu_notifier_invalidate_range_end()`` calls around the page table
332+
walks to fill in the ``args->src`` array with PFNs to be migrated.
333+
The ``invalidate_range_start()`` callback is passed a
334+
``struct mmu_notifier_range`` with the ``event`` field set to
335+
``MMU_NOTIFY_MIGRATE`` and the ``migrate_pgmap_owner`` field set to
336+
the ``args->pgmap_owner`` field passed to migrate_vma_setup(). This is
337+
allows the device driver to skip the invalidation callback and only
338+
invalidate device private MMU mappings that are actually migrating.
339+
This is explained more in the next section.
340+
341+
While walking the page tables, a ``pte_none()`` or ``is_zero_pfn()``
342+
entry results in a valid "zero" PFN stored in the ``args->src`` array.
343+
This lets the driver allocate device private memory and clear it instead
344+
of copying a page of zeros. Valid PTE entries to system memory or
345+
device private struct pages will be locked with ``lock_page()``, isolated
346+
from the LRU (if system memory since device private pages are not on
347+
the LRU), unmapped from the process, and a special migration PTE is
348+
inserted in place of the original PTE.
349+
migrate_vma_setup() also clears the ``args->dst`` array.
350+
351+
3. The device driver allocates destination pages and copies source pages to
352+
destination pages.
353+
354+
The driver checks each ``src`` entry to see if the ``MIGRATE_PFN_MIGRATE``
355+
bit is set and skips entries that are not migrating. The device driver
356+
can also choose to skip migrating a page by not filling in the ``dst``
357+
array for that page.
358+
359+
The driver then allocates either a device private struct page or a
360+
system memory page, locks the page with ``lock_page()``, and fills in the
361+
``dst`` array entry with::
362+
363+
dst[i] = migrate_pfn(page_to_pfn(dpage)) | MIGRATE_PFN_LOCKED;
364+
365+
Now that the driver knows that this page is being migrated, it can
366+
invalidate device private MMU mappings and copy device private memory
367+
to system memory or another device private page. The core Linux kernel
368+
handles CPU page table invalidations so the device driver only has to
369+
invalidate its own MMU mappings.
370+
371+
The driver can use ``migrate_pfn_to_page(src[i])`` to get the
372+
``struct page`` of the source and either copy the source page to the
373+
destination or clear the destination device private memory if the pointer
374+
is ``NULL`` meaning the source page was not populated in system memory.
375+
376+
4. ``migrate_vma_pages()``
377+
378+
This step is where the migration is actually "committed".
379+
380+
If the source page was a ``pte_none()`` or ``is_zero_pfn()`` page, this
381+
is where the newly allocated page is inserted into the CPU's page table.
382+
This can fail if a CPU thread faults on the same page. However, the page
383+
table is locked and only one of the new pages will be inserted.
384+
The device driver will see that the ``MIGRATE_PFN_MIGRATE`` bit is cleared
385+
if it loses the race.
386+
387+
If the source page was locked, isolated, etc. the source ``struct page``
388+
information is now copied to destination ``struct page`` finalizing the
389+
migration on the CPU side.
390+
391+
5. Device driver updates device MMU page tables for pages still migrating,
392+
rolling back pages not migrating.
393+
394+
If the ``src`` entry still has ``MIGRATE_PFN_MIGRATE`` bit set, the device
395+
driver can update the device MMU and set the write enable bit if the
396+
``MIGRATE_PFN_WRITE`` bit is set.
397+
398+
6. ``migrate_vma_finalize()``
399+
400+
This step replaces the special migration page table entry with the new
401+
page's page table entry and releases the reference to the source and
402+
destination ``struct page``.
403+
404+
7. ``mmap_read_unlock()``
405+
406+
The lock can now be released.
278407

279408
Memory cgroup (memcg) and rss accounting
280409
========================================

0 commit comments

Comments
 (0)