Skip to contentSkip to conversation

HMM (Heterogeneous Memory Management) v25

PATCH Applied Jérôme Glisse 9 participants 63 messages Aug 17, 2017 – Dec 20, 2018

Subsystems (24)
lore
Share & tools
lore
#0 lore raw

cover HMM (Heterogeneous Memory Management) v25

Patchset is on top of git://git.cmpxchg.org/linux-mmotm.git so i test same kernel as kbuild system, git branch:

https://cgit.freedesktop.org/~glisse/linux/log/?h=hmm-v25

Change since v24 are:
- more comments and more documentations
- fix dumb mistake when registering device
- fix race when multiple concurrent device fault
- merge HMM-CDM patchset with HMM patchset to avoid unecessary code churn

The overall logic is the same. Below is the long description of what HMM is about and why. At the end of this email i describe briefly each patch and suggest reviewers for each of them.

Please consider this patchset for 4.14

Heterogeneous Memory Management (HMM) (description and justification)

Today device driver expose dedicated memory allocation API through their device file, often relying on a combination of IOCTL and mmap calls. The device can only access and use memory allocated through this API. This effectively split the program address space into object allocated for the device and useable by the device and other regular memory (malloc, mmap of a file, share memory, Ac) only accessible by CPU (or in a very limited way by a device by pinning memory).

Allowing different isolated component of a program to use a device thus require duplication of the input data structure using device memory allocator. This is reasonable for simple data structure (array, grid, image, Ac) but this get extremely complex with advance data structure (list, tree, graph, Ac) that rely on a web of memory pointers. This is becoming a serious limitation on the kind of work load that can be offloaded to device like GPU.

New industry standard like C++, OpenCL or CUDA are pushing to remove this barrier. This require a shared address space between GPU device and CPU so that GPU can access any memory of a process (while still obeying memory protection like read only). This kind of feature is also appearing in various other operating systems.

HMM is a set of helpers to facilitate several aspects of address space sharing and device memory management. Unlike existing sharing mechanism that rely on pining pages use by a device, HMM relies on mmu_notifier to propagate CPU page table update to device page table.

Duplicating CPU page table is only one aspect necessary for efficiently using device like GPU. GPU local memory have bandwidth in the TeraBytes/ second range but they are connected to main memory through a system bus like PCIE that is limited to 32GigaBytes/second (PCIE 4.0 16x). Thus it is necessary to allow migration of process memory from main system memory to device memory. Issue is that on platform that only have PCIE the device memory is not accessible by the CPU with the same properties as main memory (cache coherency, atomic operations, ...).

To allow migration from main memory to device memory HMM provides a set of helper to hotplug device memory as a new type of ZONE_DEVICE memory which is un-addressable by CPU but still has struct page representing it. This allow most of the core kernel logic that deals with a process memory to stay oblivious of the peculiarity of device memory.

When page backing an address of a process is migrated to device memory the CPU page table entry is set to a new specific swap entry. CPU access to such address triggers a migration back to system memory, just like if the page was swap on disk. HMM also blocks any one from pinning a ZONE_DEVICE page so that it can always be migrated back to system memory if CPU access it. Conversely HMM does not migrate to device memory any page that is pin in system memory.

To allow efficient migration between device memory and main memory a new migrate_vma() helpers is added with this patchset. It allows to leverage device DMA engine to perform the copy operation.

This feature will be use by upstream driver like nouveau mlx5 and probably other in the future (amdgpu is next suspect in line). We are actively working on nouveau and mlx5 support. To test this patchset we also worked with NVidia close source driver team, they have more resources than us to test this kind of infrastructure and also a bigger and better userspace eco-system with various real industry workload they can be use to test and profile HMM.

The expected workload is a program builds a data set on the CPU (from disk, from network, from sensors, Ac). Program uses GPU API (OpenCL, CUDA, ...) to give hint on memory placement for the input data and also for the output buffer. Program call GPU API to schedule a GPU job, this happens using device driver specific ioctl. All this is hidden from programmer point of view in case of C++ compiler that transparently offload some part of a program to GPU. Program can keep doing other stuff on the CPU while the GPU is crunching numbers.

It is expected that CPU will not access the same data set as the GPU while GPU is working on it, but this is not mandatory. In fact we expect some small memory object to be actively access by both GPU and CPU concurrently as synchronization channel and/or for monitoring purposes. Such object will stay in system memory and should not be bottlenecked by system bus bandwidth (rare write and read access from both CPU and GPU).

As we are relying on device driver API, HMM does not introduce any new syscall nor does it modify any existing ones. It does not change any POSIX semantics or behaviors. For instance the child after a fork of a process that is using HMM will not be impacted in anyway, nor is there any data hazard between child COW or parent COW of memory that was migrated to device prior to fork.

HMM assume a numbers of hardware features. Device must allow device page table to be updated at any time (ie device job must be preemptable). Device page table must provides memory protection such as read only. Device must track write access (dirty bit). Device must have a minimum granularity that match PAGE_SIZE (ie 4k).

Reviewer (just hint):
Patch 1 HMM documentation
Patch 2 introduce core infrastructure and definition of HMM, pretty
small patch and easy to review
Patch 3 introduce the mirror functionality of HMM, it relies on
mmu_notifier and thus someone familiar with that part would be in better position to review
Patch 4 is an helper to snapshot CPU page table while synchronizing with
concurrent page table update. Understanding mmu_notifier makes review easier.
Patch 5 is mostly a wrapper around handle_mm_fault()
Patch 6 add new add_pages() helper to avoid modifying each arch memory
hot plug function
Patch 7 add a new memory type for ZONE_DEVICE and also add all the logic
in various core mm to support this new type. Dan Williams and any core mm contributor are best people to review each half of this patchset
Patch 8 special case HMM ZONE_DEVICE pages inside put_page() Kirill and
Dan Williams are best person to review this
Patch 9 allow to uncharge a page from memory group without using the lru
list field of struct page (best reviewer: Johannes Weiner or Vladimir Davydov or Michal Hocko)
Patch 10 Add support to uncharge ZONE_DEVICE page from a memory cgroup (best
reviewer: Johannes Weiner or Vladimir Davydov or Michal Hocko)
Patch 11 add helper to hotplug un-addressable device memory as new type
of ZONE_DEVICE memory (new type introducted in patch 3 of this serie). This is boiler plate code around memory hotplug and it also pick a free range of physical address for the device memory. Note that the physical address do not point to anything (at least as far as the kernel knows).
Patch 12 introduce a new hmm_device class as an helper for device driver
that want to expose multiple device memory under a common fake device driver. This is usefull for multi-gpu configuration. Anyone familiar with device driver infrastructure can review this. Boiler plate code really.
Patch 13 add a new migrate mode. Any one familiar with page migration is
welcome to review.
Patch 14 introduce a new migration helper (migrate_vma()) that allow to
migrate a range of virtual address of a process using device DMA engine to perform the copy. It is not limited to do copy from and to device but can also do copy between any kind of source and destination memory. Again anyone familiar with migration code should be able to verify the logic.
Patch 15 optimize the new migrate_vma() by unmapping pages while we are
collecting them. This can be review by any mm folks.
Patch 16 add unaddressable memory migration to helper introduced in patch
7, this can be review by anyone familiar with migration code
Patch 17 add a feature that allow device to allocate non-present page on
the GPU when migrating a range of address to device memory. This is an helper for device driver to avoid having to first allocate system memory before migration to device memory
Patch 18 add a new kind of ZONE_DEVICE memory for cache coherent device
memory (CDM)
Patch 19 add an helper to hotplug CDM memory

Previous patchset posting :
v1 http://lwn.net/Articles/597289/
v2 https://lkml.org/lkml/2014/6/12/559
v3 https://lkml.org/lkml/2014/6/13/633
v4 https://lkml.org/lkml/2014/8/29/423
v5 https://lkml.org/lkml/2014/11/3/759
v6 http://lwn.net/Articles/619737/
v7 http://lwn.net/Articles/627316/
v8 https://lwn.net/Articles/645515/
v9 https://lwn.net/Articles/651553/
v10 https://lwn.net/Articles/654430/
v11 http://www.gossamer-threads.com/lists/linux/kernel/2286424
v12 http://www.kernelhub.org/?msg=972982&p=2
v13 https://lwn.net/Articles/706856/
v14 https://lkml.org/lkml/2016/12/8/344
v15 http://www.mail-archive.com/linux-kernel@xxxxxxxxxxxxxxx/msg1304107.html
v16 http://www.spinics.net/lists/linux-mm/msg119814.html
v17 https://lkml.org/lkml/2017/1/27/847
v18 https://lkml.org/lkml/2017/3/16/596
v19 https://lkml.org/lkml/2017/4/5/831
v20 https://lwn.net/Articles/720715/
v21 https://lkml.org/lkml/2017/4/24/747
v22 http://lkml.iu.edu/hypermail/linux/kernel/1705.2/05176.html
v23 https://www.mail-archive.com/[email protected]/msg1404788.html
v24 https://lwn.net/Articles/726691/

JA(C)rA'me Glisse (18):
hmm: heterogeneous memory management documentation v3 mm/hmm: heterogeneous memory management (HMM for short) v5 mm/hmm/mirror: mirror process address space on device with HMM helpers

    v3

mm/hmm/mirror: helper to snapshot CPU page table v4 mm/hmm/mirror: device page fault handler
mm/ZONE_DEVICE: new type of ZONE_DEVICE for unaddressable memory v5 mm/ZONE_DEVICE: special case put_page() for device private pages v4 mm/memcontrol: allow to uncharge page without using page->lru field mm/memcontrol: support MEMORY_DEVICE_PRIVATE v4 mm/hmm/devmem: device memory hotplug using ZONE_DEVICE v7 mm/hmm/devmem: dummy HMM device for ZONE_DEVICE memory v3 mm/migrate: new migrate mode MIGRATE_SYNC_NO_COPY mm/migrate: new memory migration helper for use with device memory v5 mm/migrate: migrate_vma() unmap page from vma while collecting pages mm/migrate: support un-addressable ZONE_DEVICE page in migration v3 mm/migrate: allow migrate_vma() to alloc new page on empty entry v4 mm/device-public-memory: device memory cache coherent with CPU v5 mm/hmm: add new helper to hotplug CDM memory region v3

Michal Hocko (1):
mm/memory_hotplug: introduce add_pages

 Documentation/vm/hmm.txt       |  384 ++++++++++++
 MAINTAINERS                    |    7 +
 arch/x86/Kconfig               |    4 +
 arch/x86/mm/init_64.c          |   22 +-
 fs/aio.c                       |    8 +
 fs/f2fs/data.c                 |    5 +-
 fs/hugetlbfs/inode.c           |    5 +-
 fs/proc/task_mmu.c             |    9 +-
 fs/ubifs/file.c                |    5 +-
 include/linux/hmm.h            |  518 ++++++++++++++++
 include/linux/ioport.h         |    2 +
 include/linux/memory_hotplug.h |   11 +
 include/linux/memremap.h       |  107 ++++
 include/linux/migrate.h        |  124 ++++
 include/linux/migrate_mode.h   |    5 +
 include/linux/mm.h             |   33 +-
 include/linux/mm_types.h       |    6 +
 include/linux/swap.h           |   24 +-
 include/linux/swapops.h        |   68 +++
 kernel/fork.c                  |    3 +
 kernel/memremap.c              |   60 +-
 mm/Kconfig                     |   47 +-
 mm/Makefile                    |    2 +-
 mm/balloon_compaction.c        |    8 +
 mm/gup.c                       |    7 +
 mm/hmm.c                       | 1273 ++++++++++++++++++++++++++++++++++++++++
 mm/madvise.c                   |    2 +-
 mm/memcontrol.c                |  222 ++++---
 mm/memory.c                    |  107 +++-
 mm/memory_hotplug.c            |   10 +-
 mm/migrate.c                   |  928 ++++++++++++++++++++++++++++-
 mm/mprotect.c                  |   14 +
 mm/page_vma_mapped.c           |   10 +
 mm/rmap.c                      |   25 +
 mm/swap.c                      |   11 +
 mm/zsmalloc.c                  |    8 +
 36 files changed, 3964 insertions(+), 120 deletions(-)
 create mode 100644 Documentation/vm/hmm.txt
 create mode 100644 include/linux/hmm.h
 create mode 100644 mm/hmm.c
Signature · 7 lines
2.13.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw

PATCH 1/19 hmm: heterogeneous memory management documentation v3

This add documentation for HMM (Heterogeneous Memory Management). It presents the motivation behind it, the features necessary for it to be useful and and gives an overview of how this is implemented.

Changed since v2:
- add section about memory accounting cgroup (memcg)
Changed since v1:
- removed outdated section

---
 Documentation/vm/hmm.txt | 384 +++++++++++++++++++++++++++++++++++++++++++++++
 MAINTAINERS              |   7 +
 2 files changed, 391 insertions(+)
 create mode 100644 Documentation/vm/hmm.txt
Documentation/vm/hmm.txt new file +384 -0
new file mode 100644
index 000000000000..4d3aac9f4a5d
@@ -0,0 +1,384 @@
+Heterogeneous Memory Management (HMM)
+
+Transparently allow any component of a program to use any memory region of said
+program with a device without using device specific memory allocator. This is
+becoming a requirement to simplify the use of advance heterogeneous computing
+where GPU, DSP or FPGA are use to perform various computations.
+
+This document is divided as follow, in the first section i expose the problems
+related to the use of a device specific allocator. The second section i expose
+the hardware limitations that are inherent to many platforms. The third section
+gives an overview of HMM designs. The fourth section explains how CPU page-
+table mirroring works and what is HMM purpose in this context. Fifth section
+deals with how device memory is represented inside the kernel. Finaly the last
+section present the new migration helper that allow to leverage the device DMA
+engine.
+
+
+1) Problems of using device specific memory allocator:
+2) System bus, device memory characteristics
+3) Share address space and migration
+4) Address space mirroring implementation and API
+5) Represent and manage device memory from core kernel point of view
+6) Migrate to and from device memory
+7) Memory cgroup (memcg) and rss accounting
+
+
+-------------------------------------------------------------------------------
+
+1) Problems of using device specific memory allocator:
+
+Device with large amount of on board memory (several giga bytes) like GPU have
+historically manage their memory through dedicated driver specific API. This
+creates a disconnect between memory allocated and managed by device driver and
+regular application memory (private anonymous, share memory or regular file
+back memory). From here on i will refer to this aspect as split address space.
+I use share address space to refer to the opposite situation ie one in which
+any memory region can be use by device transparently.
+
+Split address space because device can only access memory allocated through the
+device specific API. This imply that all memory object in a program are not
+equal from device point of view which complicate large program that rely on a
+wide set of libraries.
+
+Concretly this means that code that wants to leverage device like GPU need to
+copy object between genericly allocated memory (malloc, mmap private/share/)
+and memory allocated through the device driver API (this still end up with an
+mmap but of the device file).
+
+For flat dataset (array, grid, image, ...) this isn't too hard to achieve but
+complex data-set (list, tree, ...) are hard to get right. Duplicating a complex
+data-set need to re-map all the pointer relations between each of its elements.
+This is error prone and program gets harder to debug because of the duplicate
+data-set.
+
+Split address space also means that library can not transparently use data they
+are getting from core program or other library and thus each library might have
+to duplicate its input data-set using specific memory allocator. Large project
+suffer from this and waste resources because of the various memory copy.
+
+Duplicating each library API to accept as input or output memory allocted by
+each device specific allocator is not a viable option. It would lead to a
+combinatorial explosions in the library entry points.
+
+Finaly with the advance of high level language constructs (in C++ but in other
+language too) it is now possible for compiler to leverage GPU or other devices
+without even the programmer knowledge. Some of compiler identified patterns are
+only do-able with a share address. It is as well more reasonable to use a share
+address space for all the other patterns.
+
+
+-------------------------------------------------------------------------------
+
+2) System bus, device memory characteristics
+
+System bus cripple share address due to few limitations. Most system bus only
+allow basic memory access from device to main memory, even cache coherency is
+often optional. Access to device memory from CPU is even more limited, most
+often than not it is not cache coherent.
+
+If we only consider the PCIE bus than device can access main memory (often
+through an IOMMU) and be cache coherent with the CPUs. However it only allows
+a limited set of atomic operation from device on main memory. This is worse
+in the other direction the CPUs can only access a limited range of the device
+memory and can not perform atomic operations on it. Thus device memory can not
+be consider like regular memory from kernel point of view.
+
+Another crippling factor is the limited bandwidth (~32GBytes/s with PCIE 4.0
+and 16 lanes). This is 33 times less that fastest GPU memory (1 TBytes/s).
+The final limitation is latency, access to main memory from the device has an
+order of magnitude higher latency than when the device access its own memory.
+
+Some platform are developing new system bus or additions/modifications to PCIE
+to address some of those limitations (OpenCAPI, CCIX). They mainly allow two
+way cache coherency between CPU and device and allow all atomic operations the
+architecture supports. Saddly not all platform are following this trends and
+some major architecture are left without hardware solutions to those problems.
+
+So for share address space to make sense not only we must allow device to
+access any memory memory but we must also permit any memory to be migrated to
+device memory while device is using it (blocking CPU access while it happens).
+
+
+-------------------------------------------------------------------------------
+
+3) Share address space and migration
+
+HMM intends to provide two main features. First one is to share the address
+space by duplication the CPU page table into the device page table so same
+address point to same memory and this for any valid main memory address in
+the process address space.
+
+To achieve this, HMM offer a set of helpers to populate the device page table
+while keeping track of CPU page table updates. Device page table updates are
+not as easy as CPU page table updates. To update the device page table you must
+allow a buffer (or use a pool of pre-allocated buffer) and write GPU specifics
+commands in it to perform the update (unmap, cache invalidations and flush,
+...). This can not be done through common code for all device. Hence why HMM
+provides helpers to factor out everything that can be while leaving the gory
+details to the device driver.
+
+The second mechanism HMM provide is a new kind of ZONE_DEVICE memory that does
+allow to allocate a struct page for each page of the device memory. Those page
+are special because the CPU can not map them. They however allow to migrate
+main memory to device memory using exhisting migration mechanism and everything
+looks like if page was swap out to disk from CPU point of view. Using a struct
+page gives the easiest and cleanest integration with existing mm mechanisms.
+Again here HMM only provide helpers, first to hotplug new ZONE_DEVICE memory
+for the device memory and second to perform migration. Policy decision of what
+and when to migrate things is left to the device driver.
+
+Note that any CPU access to a device page trigger a page fault and a migration
+back to main memory ie when a page backing an given address A is migrated from
+a main memory page to a device page then any CPU access to address A trigger a
+page fault and initiate a migration back to main memory.
+
+
+With this two features, HMM not only allow a device to mirror a process address
+space and keeps both CPU and device page table synchronize, but also allow to
+leverage device memory by migrating part of data-set that is actively use by a
+device.
+
+
+-------------------------------------------------------------------------------
+
+4) Address space mirroring implementation and API
+
+Address space mirroring main objective is to allow to duplicate range of CPU
+page table into a device page table and HMM helps keeping both synchronize. A
+device driver that want to mirror a process address space must start with the
+registration of an hmm_mirror struct:
+
+ int hmm_mirror_register(struct hmm_mirror *mirror,
+                         struct mm_struct *mm);
+ int hmm_mirror_register_locked(struct hmm_mirror *mirror,
+                                struct mm_struct *mm);
+
+The locked variant is to be use when the driver is already holding the mmap_sem
+of the mm in write mode. The mirror struct has a set of callback that are use
+to propagate CPU page table:
+
+ struct hmm_mirror_ops {
+     /* sync_cpu_device_pagetables() - synchronize page tables
+      *
+      * @mirror: pointer to struct hmm_mirror
+      * @update_type: type of update that occurred to the CPU page table
+      * @start: virtual start address of the range to update
+      * @end: virtual end address of the range to update
+      *
+      * This callback ultimately originates from mmu_notifiers when the CPU
+      * page table is updated. The device driver must update its page table
+      * in response to this callback. The update argument tells what action
+      * to perform.
+      *
+      * The device driver must not return from this callback until the device
+      * page tables are completely updated (TLBs flushed, etc); this is a
+      * synchronous call.
+      */
+      void (*update)(struct hmm_mirror *mirror,
+                     enum hmm_update action,
+                     unsigned long start,
+                     unsigned long end);
+ };
+
+Device driver must perform update to the range following action (turn range
+read only, or fully unmap, ...). Once driver callback returns the device must
+be done with the update.
+
+
+When device driver wants to populate a range of virtual address it can use
+either:
+ int hmm_vma_get_pfns(struct vm_area_struct *vma,
+                      struct hmm_range *range,
+                      unsigned long start,
+                      unsigned long end,
+                      hmm_pfn_t *pfns);
+ int hmm_vma_fault(struct vm_area_struct *vma,
+                   struct hmm_range *range,
+                   unsigned long start,
+                   unsigned long end,
+                   hmm_pfn_t *pfns,
+                   bool write,
+                   bool block);
+
+First one (hmm_vma_get_pfns()) will only fetch present CPU page table entry and
+will not trigger a page fault on missing or non present entry. The second one
+do trigger page fault on missing or read only entry if write parameter is true.
+Page fault use the generic mm page fault code path just like a CPU page fault.
+
+Both function copy CPU page table into their pfns array argument. Each entry in
+that array correspond to an address in the virtual range. HMM provide a set of
+flags to help driver identify special CPU page table entries.
+
+Locking with the update() callback is the most important aspect the driver must
+respect in order to keep things properly synchronize. The usage pattern is :
+
+ int driver_populate_range(...)
+ {
+      struct hmm_range range;
+      ...
+ again:
+      ret = hmm_vma_get_pfns(vma, &range, start, end, pfns);
+      if (ret)
+          return ret;
+      take_lock(driver->update);
+      if (!hmm_vma_range_done(vma, &range)) {
+          release_lock(driver->update);
+          goto again;
+      }
+
+      // Use pfns array content to update device page table
+
+      release_lock(driver->update);
+      return 0;
+ }
+
+The driver->update lock is the same lock that driver takes inside its update()
+callback. That lock must be call before hmm_vma_range_done() to avoid any race
+with a concurrent CPU page table update.
+
+HMM implements all this on top of the mmu_notifier API because we wanted to a
+simpler API and also to be able to perform optimization latter own like doing
+concurrent device update in multi-devices scenario.
+
+HMM also serve as an impedence missmatch between how CPU page table update are
+done (by CPU write to the page table and TLB flushes) from how device update
+their own page table. Device update is a multi-step process, first appropriate
+commands are write to a buffer, then this buffer is schedule for execution on
+the device. It is only once the device has executed commands in the buffer that
+the update is done. Creating and scheduling update command buffer can happen
+concurrently for multiple devices. Waiting for each device to report commands
+as executed is serialize (there is no point in doing this concurrently).
+
+
+-------------------------------------------------------------------------------
+
+5) Represent and manage device memory from core kernel point of view
+
+Several differents design were try to support device memory. First one use
+device specific data structure to keep information about migrated memory and
+HMM hooked itself in various place of mm code to handle any access to address
+that were back by device memory. It turns out that this ended up replicating
+most of the fields of struct page and also needed many kernel code path to be
+updated to understand this new kind of memory.
+
+Thing is most kernel code path never try to access the memory behind a page
+but only care about struct page contents. Because of this HMM switchted to
+directly using struct page for device memory which left most kernel code path
+un-aware of the difference. We only need to make sure that no one ever try to
+map those page from the CPU side.
+
+HMM provide a set of helpers to register and hotplug device memory as a new
+region needing struct page. This is offer through a very simple API:
+
+ struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
+                                   struct device *device,
+                                   unsigned long size);
+ void hmm_devmem_remove(struct hmm_devmem *devmem);
+
+The hmm_devmem_ops is where most of the important things are:
+
+ struct hmm_devmem_ops {
+     void (*free)(struct hmm_devmem *devmem, struct page *page);
+     int (*fault)(struct hmm_devmem *devmem,
+                  struct vm_area_struct *vma,
+                  unsigned long addr,
+                  struct page *page,
+                  unsigned flags,
+                  pmd_t *pmdp);
+ };
+
+The first callback (free()) happens when the last reference on a device page is
+drop. This means the device page is now free and no longer use by anyone. The
+second callback happens whenever CPU try to access a device page which it can
+not do. This second callback must trigger a migration back to system memory.
+
+
+-------------------------------------------------------------------------------
+
+6) Migrate to and from device memory
+
+Because CPU can not access device memory, migration must use device DMA engine
+to perform copy from and to device memory. For this we need a new migration
+helper:
+
+ int migrate_vma(const struct migrate_vma_ops *ops,
+                 struct vm_area_struct *vma,
+                 unsigned long mentries,
+                 unsigned long start,
+                 unsigned long end,
+                 unsigned long *src,
+                 unsigned long *dst,
+                 void *private);
+
+Unlike other migration function it works on a range of virtual address, there
+is two reasons for that. First device DMA copy has a high setup overhead cost
+and thus batching multiple pages is needed as otherwise the migration overhead
+make the whole excersie pointless. The second reason is because driver trigger
+such migration base on range of address the device is actively accessing.
+
+The migrate_vma_ops struct define two callbacks. First one (alloc_and_copy())
+control destination memory allocation and copy operation. Second one is there
+to allow device driver to perform cleanup operation after migration.
+
+ struct migrate_vma_ops {
+     void (*alloc_and_copy)(struct vm_area_struct *vma,
+                            const unsigned long *src,
+                            unsigned long *dst,
+                            unsigned long start,
+                            unsigned long end,
+                            void *private);
+     void (*finalize_and_map)(struct vm_area_struct *vma,
+                              const unsigned long *src,
+                              const unsigned long *dst,
+                              unsigned long start,
+                              unsigned long end,
+                              void *private);
+ };
+
+It is important to stress that this migration helpers allow for hole in the
+virtual address range. Some pages in the range might not be migrated for all
+the usual reasons (page is pin, page is lock, ...). This helper does not fail
+but just skip over those pages.
+
+The alloc_and_copy() might as well decide to not migrate all pages in the
+range (for reasons under the callback control). For those the callback just
+have to leave the corresponding dst entry empty.
+
+Finaly the migration of the struct page might fails (for file back page) for
+various reasons (failure to freeze reference, or update page cache, ...). If
+that happens then the finalize_and_map() can catch any pages that was not
+migrated. Note those page were still copied to new page and thus we wasted
+bandwidth but this is considered as a rare event and a price that we are
+willing to pay to keep all the code simpler.
+
+
+-------------------------------------------------------------------------------
+
+7) Memory cgroup (memcg) and rss accounting
+
+For now device memory is accounted as any regular page in rss counters (either
+anonymous if device page is use for anonymous, file if device page is use for
+file back page or shmem if device page is use for share memory). This is a
+deliberate choice to keep existing application that might start using device
+memory without knowing about it to keep runing unimpacted.
+
+Drawbacks is that OOM killer might kill an application using a lot of device
+memory and not a lot of regular system memory and thus not freeing much system
+memory. We want to gather more real world experience on how application and
+system react under memory pressure in the presence of device memory before
+deciding to account device memory differently.
+
+
+Same decision was made for memory cgroup. Device memory page are accounted
+against same memory cgroup a regular page would be accounted to. This does
+simplify migration to and from device memory. This also means that migration
+back from device memory to regular memory can not fail because it would
+go above memory cgroup limit. We might revisit this choice latter on once we
+get more experience in how device memory is use and its impact on memory
+resource control.
+
+
+Note that device memory can never be pin nor by device driver nor through GUP
+and thus such memory is always free upon process exit. Or when last reference
+is drop in case of share memory or file back memory.
MAINTAINERS +7 -0
index a982e0afd894..d8e43f32f246 100644
@@ -7744,6 +7744,13 @@ M:	Sasha Levin <[email protected]>
 S:	Maintained
 F:	tools/lib/lockdep/
 
+HMM - Heterogeneous Memory Management
+M:	JA(C)rA'me Glisse <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	mm/hmm*
+F:	include/linux/hmm*
+
 LIBNVDIMM BLK: MMIO-APERTURE DRIVER
 M:	Ross Zwisler <[email protected]>
 L:	[email protected]
Signature · 7 lines
2.13.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#2 lore raw

PATCH 2/19 mm/hmm: heterogeneous memory management (HMM for short) v5

In reply to Jérôme Glisse

HMM provides 3 separate types of functionality:
- Mirroring: synchronize CPU page table and device page table
- Device memory: allocating struct page for device memory
- Migration: migrating regular memory to device memory

This patch introduces some common helpers and definitions to all of those 3 functionality.

Changed since v4:
- added hmm_mm_init() call to init mm's HMM fields
Changed since v3:
- Unconditionaly build hmm.c for static keys
Changed since v2:
- s/device unaddressable/device private
Changed since v1:
- Kconfig logic (depend on x86-64 and use ARCH_HAS pattern)

---
 include/linux/hmm.h      | 152 +++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/mm_types.h |   6 ++
 kernel/fork.c            |   3 +
 mm/Kconfig               |  13 ++++
 mm/Makefile              |   2 +-
 mm/hmm.c                 |  74 +++++++++++++++++++++++
 6 files changed, 249 insertions(+), 1 deletion(-)
 create mode 100644 include/linux/hmm.h
 create mode 100644 mm/hmm.c
include/linux/hmm.h new file +152 -0
new file mode 100644
index 000000000000..ca60595ce784
@@ -0,0 +1,152 @@
+/*
+ * Copyright 2013 Red Hat Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * Authors: JA(C)rA'me Glisse <[email protected]>
+ */
+/*
+ * Heterogeneous Memory Management (HMM)
+ *
+ * See Documentation/vm/hmm.txt for reasons and overview of what HMM is and it
+ * is for. Here we focus on the HMM API description, with some explanation of
+ * the underlying implementation.
+ *
+ * Short description: HMM provides a set of helpers to share a virtual address
+ * space between CPU and a device, so that the device can access any valid
+ * address of the process (while still obeying memory protection). HMM also
+ * provides helpers to migrate process memory to device memory, and back. Each
+ * set of functionality (address space mirroring, and migration to and from
+ * device memory) can be used independently of the other.
+ *
+ *
+ * HMM address space mirroring API:
+ *
+ * Use HMM address space mirroring if you want to mirror range of the CPU page
+ * table of a process into a device page table. Here, "mirror" means "keep
+ * synchronized". Prerequisites: the device must provide the ability to write-
+ * protect its page tables (at PAGE_SIZE granularity), and must be able to
+ * recover from the resulting potential page faults.
+ *
+ * HMM guarantees that at any point in time, a given virtual address points to
+ * either the same memory in both CPU and device page tables (that is: CPU and
+ * device page tables each point to the same pages), or that one page table (CPU
+ * or device) points to no entry, while the other still points to the old page
+ * for the address. The latter case happens when the CPU page table update
+ * happens first, and then the update is mirrored over to the device page table.
+ * This does not cause any issue, because the CPU page table cannot start
+ * pointing to a new page until the device page table is invalidated.
+ *
+ * HMM uses mmu_notifiers to monitor the CPU page tables, and forwards any
+ * updates to each device driver that has registered a mirror. It also provides
+ * some API calls to help with taking a snapshot of the CPU page table, and to
+ * synchronize with any updates that might happen concurrently.
+ *
+ *
+ * HMM migration to and from device memory:
+ *
+ * HMM provides a set of helpers to hotplug device memory as ZONE_DEVICE, with
+ * a new MEMORY_DEVICE_PRIVATE type. This provides a struct page for each page
+ * of the device memory, and allows the device driver to manage its memory
+ * using those struct pages. Having struct pages for device memory makes
+ * migration easier. Because that memory is not addressable by the CPU it must
+ * never be pinned to the device; in other words, any CPU page fault can always
+ * cause the device memory to be migrated (copied/moved) back to regular memory.
+ *
+ * A new migrate helper (migrate_vma()) has been added (see mm/migrate.c) that
+ * allows use of a device DMA engine to perform the copy operation between
+ * regular system memory and device memory.
+ */
+#ifndef LINUX_HMM_H
+#define LINUX_HMM_H
+
+#include <linux/kconfig.h>
+
+#if IS_ENABLED(CONFIG_HMM)
+
+
+/*
+ * hmm_pfn_t - HMM uses its own pfn type to keep several flags per page
+ *
+ * Flags:
+ * HMM_PFN_VALID: pfn is valid
+ * HMM_PFN_WRITE: CPU page table has write permission set
+ */
+typedef unsigned long hmm_pfn_t;
+
+#define HMM_PFN_VALID (1 << 0)
+#define HMM_PFN_WRITE (1 << 1)
+#define HMM_PFN_SHIFT 2
+
+/*
+ * hmm_pfn_t_to_page() - return struct page pointed to by a valid hmm_pfn_t
+ * @pfn: hmm_pfn_t to convert to struct page
+ * Returns: struct page pointer if pfn is a valid hmm_pfn_t, NULL otherwise
+ *
+ * If the hmm_pfn_t is valid (ie valid flag set) then return the struct page
+ * matching the pfn value stored in the hmm_pfn_t. Otherwise return NULL.
+ */
+static inline struct page *hmm_pfn_t_to_page(hmm_pfn_t pfn)
+{
+	if (!(pfn & HMM_PFN_VALID))
+		return NULL;
+	return pfn_to_page(pfn >> HMM_PFN_SHIFT);
+}
+
+/*
+ * hmm_pfn_t_to_pfn() - return pfn value store in a hmm_pfn_t
+ * @pfn: hmm_pfn_t to extract pfn from
+ * Returns: pfn value if hmm_pfn_t is valid, -1UL otherwise
+ */
+static inline unsigned long hmm_pfn_t_to_pfn(hmm_pfn_t pfn)
+{
+	if (!(pfn & HMM_PFN_VALID))
+		return -1UL;
+	return (pfn >> HMM_PFN_SHIFT);
+}
+
+/*
+ * hmm_pfn_t_from_page() - create a valid hmm_pfn_t value from struct page
+ * @page: struct page pointer for which to create the hmm_pfn_t
+ * Returns: valid hmm_pfn_t for the page
+ */
+static inline hmm_pfn_t hmm_pfn_t_from_page(struct page *page)
+{
+	return (page_to_pfn(page) << HMM_PFN_SHIFT) | HMM_PFN_VALID;
+}
+
+/*
+ * hmm_pfn_t_from_pfn() - create a valid hmm_pfn_t value from pfn
+ * @pfn: pfn value for which to create the hmm_pfn_t
+ * Returns: valid hmm_pfn_t for the pfn
+ */
+static inline hmm_pfn_t hmm_pfn_t_from_pfn(unsigned long pfn)
+{
+	return (pfn << HMM_PFN_SHIFT) | HMM_PFN_VALID;
+}
+
+
+/* Below are for HMM internal use only! Not to be used by device driver! */
+void hmm_mm_destroy(struct mm_struct *mm);
+
+static inline void hmm_mm_init(struct mm_struct *mm)
+{
+	mm->hmm = NULL;
+}
+
+#else /* IS_ENABLED(CONFIG_HMM) */
+
+/* Below are for HMM internal use only! Not to be used by device driver! */
+static inline void hmm_mm_destroy(struct mm_struct *mm) {}
+static inline void hmm_mm_init(struct mm_struct *mm) {}
+
+#endif /* IS_ENABLED(CONFIG_HMM) */
+#endif /* LINUX_HMM_H */
include/linux/mm_types.h +6 -0
index 7f384bb62d8e..7f9a04fbbe01 100644
@@ -23,6 +23,7 @@
 
 struct address_space;
 struct mem_cgroup;
+struct hmm;
 
 /*
  * Each physical page in the system has a struct page associated with
@@ -504,6 +505,11 @@ struct mm_struct {
 	atomic_long_t hugetlb_usage;
 #endif
 	struct work_struct async_put_work;
+
+#if IS_ENABLED(CONFIG_HMM)
+	/* HMM needs to track a few things per mm */
+	struct hmm *hmm;
+#endif
 } __randomize_layout;
 
 extern struct mm_struct init_mm;
kernel/fork.c +3 -0
index d747e14ce27b..2760f1b15538 100644
@@ -37,6 +37,7 @@
 #include <linux/binfmts.h>
 #include <linux/mman.h>
 #include <linux/mmu_notifier.h>
+#include <linux/hmm.h>
 #include <linux/fs.h>
 #include <linux/mm.h>
 #include <linux/vmacache.h>
@@ -807,6 +808,7 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
 	mm_init_aio(mm);
 	mm_init_owner(mm, p);
 	mmu_notifier_mm_init(mm);
+	hmm_mm_init(mm);
 	clear_tlb_flush_pending(mm);
 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) && !USE_SPLIT_PMD_PTLOCKS
 	mm->pmd_huge_pte = NULL;
@@ -885,6 +887,7 @@ void __mmdrop(struct mm_struct *mm)
 	BUG_ON(mm == &init_mm);
 	mm_free_pgd(mm);
 	destroy_context(mm);
+	hmm_mm_destroy(mm);
 	mmu_notifier_mm_destroy(mm);
 	check_mm(mm);
 	put_user_ns(mm->user_ns);
mm/Kconfig +13 -0
index ab937c8d247f..f0b0f2d6c123 100644
@@ -691,6 +691,19 @@ config ZONE_DEVICE
 
 	  If FS_DAX is enabled, then say Y.
 
+config ARCH_HAS_HMM
+	bool
+	default y
+	depends on (X86_64 || PPC64)
+	depends on ZONE_DEVICE
+	depends on MMU && 64BIT
+	depends on MEMORY_HOTPLUG
+	depends on MEMORY_HOTREMOVE
+	depends on SPARSEMEM_VMEMMAP
+
+config HMM
+	bool
+
 config FRAME_VECTOR
 	bool
 
mm/Makefile +1 -1
index 411bd24d4a7c..1cde2a8bed97 100644
@@ -39,7 +39,7 @@ obj-y			:= filemap.o mempool.o oom_kill.o \
 			   mm_init.o mmu_context.o percpu.o slab_common.o \
 			   compaction.o vmacache.o swap_slots.o \
 			   interval_tree.o list_lru.o workingset.o \
-			   debug.o $(mmu-y)
+			   debug.o hmm.o $(mmu-y)
 
 obj-y += init-mm.o
 
mm/hmm.c new file +74 -0

74 lines not on this page · raw message

Signature · 7 lines
2.13.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#3 lore raw

PATCH 3/19 mm/hmm/mirror: mirror process address space on device with HMM helpers v3

In reply to Jérôme Glisse

This is a heterogeneous memory management (HMM) process address space mirroring. In a nutshell this provide an API to mirror process address space on a device. This boils down to keeping CPU and device page table synchronize (we assume that both device and CPU are cache coherent like PCIe device can be).

This patch provide a simple API for device driver to achieve address space mirroring thus avoiding each device driver to grow its own CPU page table walker and its own CPU page table synchronization mechanism.

This is useful for NVidia GPU >= Pascal, Mellanox IB >= mlx5 and more hardware in the future.

Changed since v2:
- s/device unaddressable/device private/
Changed since v1:
- Kconfig logic (depend on x86-64 and use ARCH_HAS pattern)

---
 include/linux/hmm.h | 110 ++++++++++++++++++++++++++++++++++
 mm/Kconfig          |  12 ++++
 mm/hmm.c            | 168 +++++++++++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 275 insertions(+), 15 deletions(-)
include/linux/hmm.h +110 -0
index ca60595ce784..61c707970aa6 100644
@@ -72,6 +72,7 @@
 
 #if IS_ENABLED(CONFIG_HMM)
 
+struct hmm;
 
 /*
  * hmm_pfn_t - HMM uses its own pfn type to keep several flags per page
@@ -134,6 +135,115 @@ static inline hmm_pfn_t hmm_pfn_t_from_pfn(unsigned long pfn)
 }
 
 
+#if IS_ENABLED(CONFIG_HMM_MIRROR)
+/*
+ * Mirroring: how to synchronize device page table with CPU page table.
+ *
+ * A device driver that is participating in HMM mirroring must always
+ * synchronize with CPU page table updates. For this, device drivers can either
+ * directly use mmu_notifier APIs or they can use the hmm_mirror API. Device
+ * drivers can decide to register one mirror per device per process, or just
+ * one mirror per process for a group of devices. The pattern is:
+ *
+ *      int device_bind_address_space(..., struct mm_struct *mm, ...)
+ *      {
+ *          struct device_address_space *das;
+ *
+ *          // Device driver specific initialization, and allocation of das
+ *          // which contains an hmm_mirror struct as one of its fields.
+ *          ...
+ *
+ *          ret = hmm_mirror_register(&das->mirror, mm, &device_mirror_ops);
+ *          if (ret) {
+ *              // Cleanup on error
+ *              return ret;
+ *          }
+ *
+ *          // Other device driver specific initialization
+ *          ...
+ *      }
+ *
+ * Once an hmm_mirror is registered for an address space, the device driver
+ * will get callbacks through sync_cpu_device_pagetables() operation (see
+ * hmm_mirror_ops struct).
+ *
+ * Device driver must not free the struct containing the hmm_mirror struct
+ * before calling hmm_mirror_unregister(). The expected usage is to do that when
+ * the device driver is unbinding from an address space.
+ *
+ *
+ *      void device_unbind_address_space(struct device_address_space *das)
+ *      {
+ *          // Device driver specific cleanup
+ *          ...
+ *
+ *          hmm_mirror_unregister(&das->mirror);
+ *
+ *          // Other device driver specific cleanup, and now das can be freed
+ *          ...
+ *      }
+ */
+
+struct hmm_mirror;
+
+/*
+ * enum hmm_update_type - type of update
+ * @HMM_UPDATE_INVALIDATE: invalidate range (no indication as to why)
+ */
+enum hmm_update_type {
+	HMM_UPDATE_INVALIDATE,
+};
+
+/*
+ * struct hmm_mirror_ops - HMM mirror device operations callback
+ *
+ * @update: callback to update range on a device
+ */
+struct hmm_mirror_ops {
+	/* sync_cpu_device_pagetables() - synchronize page tables
+	 *
+	 * @mirror: pointer to struct hmm_mirror
+	 * @update_type: type of update that occurred to the CPU page table
+	 * @start: virtual start address of the range to update
+	 * @end: virtual end address of the range to update
+	 *
+	 * This callback ultimately originates from mmu_notifiers when the CPU
+	 * page table is updated. The device driver must update its page table
+	 * in response to this callback. The update argument tells what action
+	 * to perform.
+	 *
+	 * The device driver must not return from this callback until the device
+	 * page tables are completely updated (TLBs flushed, etc); this is a
+	 * synchronous call.
+	 */
+	void (*sync_cpu_device_pagetables)(struct hmm_mirror *mirror,
+					   enum hmm_update_type update_type,
+					   unsigned long start,
+					   unsigned long end);
+};
+
+/*
+ * struct hmm_mirror - mirror struct for a device driver
+ *
+ * @hmm: pointer to struct hmm (which is unique per mm_struct)
+ * @ops: device driver callback for HMM mirror operations
+ * @list: for list of mirrors of a given mm
+ *
+ * Each address space (mm_struct) being mirrored by a device must register one
+ * instance of an hmm_mirror struct with HMM. HMM will track the list of all
+ * mirrors for each mm_struct.
+ */
+struct hmm_mirror {
+	struct hmm			*hmm;
+	const struct hmm_mirror_ops	*ops;
+	struct list_head		list;
+};
+
+int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm);
+void hmm_mirror_unregister(struct hmm_mirror *mirror);
+#endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
+
+
 /* Below are for HMM internal use only! Not to be used by device driver! */
 void hmm_mm_destroy(struct mm_struct *mm);
 
mm/Kconfig +12 -0
index f0b0f2d6c123..87d530cbbbc7 100644
@@ -704,6 +704,18 @@ config ARCH_HAS_HMM
 config HMM
 	bool
 
+config HMM_MIRROR
+	bool "HMM mirror CPU page table into a device page table"
+	depends on ARCH_HAS_HMM
+	select MMU_NOTIFIER
+	select HMM
+	help
+	  Select HMM_MIRROR if you want to mirror range of the CPU page table of a
+	  process into a device page table. Here, mirror means "keep synchronized".
+	  Prerequisites: the device must provide the ability to write-protect its
+	  page tables (at PAGE_SIZE granularity), and must be able to recover from
+	  the resulting potential page faults.
+
 config FRAME_VECTOR
 	bool
 
mm/hmm.c +153 -15

199 lines not on this page · raw message

Signature · 7 lines
2.13.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#4 lore raw

PATCH 4/19 mm/hmm/mirror: helper to snapshot CPU page table v4

In reply to Jérôme Glisse

This does not use existing page table walker because we want to share same code for our page fault handler.

Changed since v3:
- fix THP handling
Changed since v2:
- s/device unaddressable/device private/
Changes since v1:
- Use spinlock instead of rcu synchronized list traversal

---
 include/linux/hmm.h |  55 +++++++++-
 mm/hmm.c            | 285 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 338 insertions(+), 2 deletions(-)
include/linux/hmm.h +53 -2
index 61c707970aa6..4cb6f9706c3c 100644
@@ -79,13 +79,26 @@ struct hmm;
  *
  * Flags:
  * HMM_PFN_VALID: pfn is valid
+ * HMM_PFN_READ:  CPU page table has read permission set
  * HMM_PFN_WRITE: CPU page table has write permission set
+ * HMM_PFN_ERROR: corresponding CPU page table entry points to poisoned memory
+ * HMM_PFN_EMPTY: corresponding CPU page table entry is pte_none()
+ * HMM_PFN_SPECIAL: corresponding CPU page table entry is special; i.e., the
+ *      result of vm_insert_pfn() or vm_insert_page(). Therefore, it should not
+ *      be mirrored by a device, because the entry will never have HMM_PFN_VALID
+ *      set and the pfn value is undefined.
+ * HMM_PFN_DEVICE_UNADDRESSABLE: unaddressable device memory (ZONE_DEVICE)
  */
 typedef unsigned long hmm_pfn_t;
 
 #define HMM_PFN_VALID (1 << 0)
-#define HMM_PFN_WRITE (1 << 1)
-#define HMM_PFN_SHIFT 2
+#define HMM_PFN_READ (1 << 1)
+#define HMM_PFN_WRITE (1 << 2)
+#define HMM_PFN_ERROR (1 << 3)
+#define HMM_PFN_EMPTY (1 << 4)
+#define HMM_PFN_SPECIAL (1 << 5)
+#define HMM_PFN_DEVICE_UNADDRESSABLE (1 << 6)
+#define HMM_PFN_SHIFT 7
 
 /*
  * hmm_pfn_t_to_page() - return struct page pointed to by a valid hmm_pfn_t
@@ -241,6 +254,44 @@ struct hmm_mirror {
 
 int hmm_mirror_register(struct hmm_mirror *mirror, struct mm_struct *mm);
 void hmm_mirror_unregister(struct hmm_mirror *mirror);
+
+
+/*
+ * struct hmm_range - track invalidation lock on virtual address range
+ *
+ * @list: all range lock are on a list
+ * @start: range virtual start address (inclusive)
+ * @end: range virtual end address (exclusive)
+ * @pfns: array of pfns (big enough for the range)
+ * @valid: pfns array did not change since it has been fill by an HMM function
+ */
+struct hmm_range {
+	struct list_head	list;
+	unsigned long		start;
+	unsigned long		end;
+	hmm_pfn_t		*pfns;
+	bool			valid;
+};
+
+/*
+ * To snapshot the CPU page table, call hmm_vma_get_pfns(), then take a device
+ * driver lock that serializes device page table updates, then call
+ * hmm_vma_range_done(), to check if the snapshot is still valid. The same
+ * device driver page table update lock must also be used in the
+ * hmm_mirror_ops.sync_cpu_device_pagetables() callback, so that CPU page
+ * table invalidation serializes on it.
+ *
+ * YOU MUST CALL hmm_vma_range_done() ONCE AND ONLY ONCE EACH TIME YOU CALL
+ * hmm_vma_get_pfns() WITHOUT ERROR !
+ *
+ * IF YOU DO NOT FOLLOW THE ABOVE RULE THE SNAPSHOT CONTENT MIGHT BE INVALID !
+ */
+int hmm_vma_get_pfns(struct vm_area_struct *vma,
+		     struct hmm_range *range,
+		     unsigned long start,
+		     unsigned long end,
+		     hmm_pfn_t *pfns);
+bool hmm_vma_range_done(struct vm_area_struct *vma, struct hmm_range *range);
 #endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
 
 
mm/hmm.c +285 -0

323 lines not on this page · raw message

Signature · 7 lines
2.13.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#5 lore raw

PATCH 5/19 mm/hmm/mirror: device page fault handler

In reply to Jérôme Glisse

This handle page fault on behalf of device driver, unlike handle_mm_fault() it does not trigger migration back to system memory for device memory.

---
 include/linux/hmm.h |  27 ++++++
 mm/hmm.c            | 256 +++++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 271 insertions(+), 12 deletions(-)
include/linux/hmm.h +27 -0
index 4cb6f9706c3c..79f5d4fa2d4f 100644
@@ -292,6 +292,33 @@ int hmm_vma_get_pfns(struct vm_area_struct *vma,
 		     unsigned long end,
 		     hmm_pfn_t *pfns);
 bool hmm_vma_range_done(struct vm_area_struct *vma, struct hmm_range *range);
+
+
+/*
+ * Fault memory on behalf of device driver. Unlike handle_mm_fault(), this will
+ * not migrate any device memory back to system memory. The hmm_pfn_t array will
+ * be updated with the fault result and current snapshot of the CPU page table
+ * for the range.
+ *
+ * The mmap_sem must be taken in read mode before entering and it might be
+ * dropped by the function if the block argument is false. In that case, the
+ * function returns -EAGAIN.
+ *
+ * Return value does not reflect if the fault was successful for every single
+ * address or not. Therefore, the caller must to inspect the hmm_pfn_t array to
+ * determine fault status for each address.
+ *
+ * Trying to fault inside an invalid vma will result in -EINVAL.
+ *
+ * See the function description in mm/hmm.c for further documentation.
+ */
+int hmm_vma_fault(struct vm_area_struct *vma,
+		  struct hmm_range *range,
+		  unsigned long start,
+		  unsigned long end,
+		  hmm_pfn_t *pfns,
+		  bool write,
+		  bool block);
 #endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
 
 
mm/hmm.c +244 -12

358 lines not on this page · raw message

Signature · 7 lines
2.13.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#6 lore raw

PATCH 6/19 mm/memory_hotplug: introduce add_pages

In reply to Jérôme Glisse

From: Michal Hocko <[email protected]>

There are new users of memory hotplug emerging. Some of them require different subset of arch_add_memory. There are some which only require allocation of struct pages without mapping those pages to the kernel address space. We currently have __add_pages for that purpose. But this is rather lowlevel and not very suitable for the code outside of the memory hotplug. E.g. x86_64 wants to update max_pfn which should be done by the caller. Introduce add_pages() which should care about those details if they are needed. Each architecture should define its implementation and select CONFIG_ARCH_HAS_ADD_PAGES. All others use the currently existing __add_pages.

---
 arch/x86/Kconfig               |  4 ++++
 arch/x86/mm/init_64.c          | 22 +++++++++++++++-------
 include/linux/memory_hotplug.h | 11 +++++++++++
 3 files changed, 30 insertions(+), 7 deletions(-)
arch/x86/Kconfig +4 -0
index cf3012220d10..78492ac02779 100644
@@ -2319,6 +2319,10 @@ source "kernel/livepatch/Kconfig"
 
 endmenu
 
+config ARCH_HAS_ADD_PAGES
+	def_bool y
+	depends on X86_64 && ARCH_ENABLE_MEMORY_HOTPLUG
+
 config ARCH_ENABLE_MEMORY_HOTPLUG
 	def_bool y
 	depends on X86_64 || (X86_32 && HIGHMEM)
arch/x86/mm/init_64.c +15 -7
index 136422d7d539..048fbe8fc274 100644
@@ -761,7 +761,7 @@ void __init paging_init(void)
  * After memory hotplug the variables max_pfn, max_low_pfn and high_memory need
  * updating.
  */
-static void  update_end_of_memory_vars(u64 start, u64 size)
+static void update_end_of_memory_vars(u64 start, u64 size)
 {
 	unsigned long end_pfn = PFN_UP(start + size);
 
@@ -772,22 +772,30 @@ static void  update_end_of_memory_vars(u64 start, u64 size)
 	}
 }
 
-int arch_add_memory(int nid, u64 start, u64 size, bool want_memblock)
+int add_pages(int nid, unsigned long start_pfn,
+	      unsigned long nr_pages, bool want_memblock)
 {
-	unsigned long start_pfn = start >> PAGE_SHIFT;
-	unsigned long nr_pages = size >> PAGE_SHIFT;
 	int ret;
 
-	init_memory_mapping(start, start + size);
-
 	ret = __add_pages(nid, start_pfn, nr_pages, want_memblock);
 	WARN_ON_ONCE(ret);
 
 	/* update max_pfn, max_low_pfn and high_memory */
-	update_end_of_memory_vars(start, size);
+	update_end_of_memory_vars(start_pfn << PAGE_SHIFT,
+				  nr_pages << PAGE_SHIFT);
 
 	return ret;
 }
+
+int arch_add_memory(int nid, u64 start, u64 size, bool want_memblock)
+{
+	unsigned long start_pfn = start >> PAGE_SHIFT;
+	unsigned long nr_pages = size >> PAGE_SHIFT;
+
+	init_memory_mapping(start, start + size);
+
+	return add_pages(nid, start_pfn, nr_pages, want_memblock);
+}
 EXPORT_SYMBOL_GPL(arch_add_memory);
 
 #define PAGE_INUSE 0xFD
include/linux/memory_hotplug.h +11 -0
index 5e6e4cc36ff4..0995e1a2b458 100644
@@ -133,6 +133,17 @@ extern int __remove_pages(struct zone *zone, unsigned long start_pfn,
 extern int __add_pages(int nid, unsigned long start_pfn,
 	unsigned long nr_pages, bool want_memblock);
 
+#ifndef CONFIG_ARCH_HAS_ADD_PAGES
+static inline int add_pages(int nid, unsigned long start_pfn,
+			    unsigned long nr_pages, bool want_memblock)
+{
+	return __add_pages(nid, start_pfn, nr_pages, want_memblock);
+}
+#else /* ARCH_HAS_ADD_PAGES */
+int add_pages(int nid, unsigned long start_pfn,
+	      unsigned long nr_pages, bool want_memblock);
+#endif /* ARCH_HAS_ADD_PAGES */
+
 #ifdef CONFIG_NUMA
 extern int memory_add_physaddr_to_nid(u64 start);
 #else
Signature · 7 lines
2.13.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#7 lore raw

PATCH 7/19 mm/ZONE_DEVICE: new type of ZONE_DEVICE for unaddressable memory v5

In reply to Jérôme Glisse

HMM (heterogeneous memory management) need struct page to support migration from system main memory to device memory. Reasons for HMM and migration to device memory is explained with HMM core patch.

This patch deals with device memory that is un-addressable memory (ie CPU can not access it). Hence we do not want those struct page to be manage like regular memory. That is why we extend ZONE_DEVICE to support different types of memory.

A persistent memory type is define for existing user of ZONE_DEVICE and a new device un-addressable type is added for the un-addressable memory type. There is a clear separation between what is expected from each memory type and existing user of ZONE_DEVICE are un-affected by new requirement and new use of the un-addressable type. All specific code path are protect with test against the memory type.

Because memory is un-addressable we use a new special swap type for when a page is migrated to device memory (this reduces the number of maximum swap file).

The main two additions beside memory type to ZONE_DEVICE is two callbacks. First one, page_free() is call whenever page refcount reach 1 (which means the page is free as ZONE_DEVICE page never reach a refcount of 0). This allow device driver to manage its memory and associated struct page.

The second callback page_fault() happens when there is a CPU access to an address that is back by a device page (which are un-addressable by the CPU). This callback is responsible to migrate the page back to system main memory. Device driver can not block migration back to system memory, HMM make sure that such page can not be pin into device memory.

If device is in some error condition and can not migrate memory back then a CPU page fault to device memory should end with SIGBUS.

Changed since v4:
- s/DEVICE_PUBLIC/DEVICE_HOST (to free DEVICE_PUBLIC for HMM-CDM)
Changed since v3:
- fix comments that was still using UNADDRESSABLE as keyword
- kernel configuration simplification
Changed since v2:
- s/DEVICE_UNADDRESSABLE/DEVICE_PRIVATE
Changed since v1:
- rename to device private memory (from device unaddressable)

---
 fs/proc/task_mmu.c       |  7 +++++
 include/linux/ioport.h   |  1 +
 include/linux/memremap.h | 73 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/mm.h       | 12 ++++++++
 include/linux/swap.h     | 24 ++++++++++++++--
 include/linux/swapops.h  | 68 ++++++++++++++++++++++++++++++++++++++++++++
 kernel/memremap.c        | 34 ++++++++++++++++++++++
 mm/Kconfig               | 11 +++++++-
 mm/memory.c              | 61 ++++++++++++++++++++++++++++++++++++++++
 mm/memory_hotplug.c      | 10 +++++--
 mm/mprotect.c            | 14 ++++++++++
 11 files changed, 309 insertions(+), 6 deletions(-)
fs/proc/task_mmu.c +7 -0
index 186e907187ab..e3ba93b35925 100644
@@ -535,6 +535,8 @@ static void smaps_pte_entry(pte_t *pte, unsigned long addr,
 			}
 		} else if (is_migration_entry(swpent))
 			page = migration_entry_to_page(swpent);
+		else if (is_device_private_entry(swpent))
+			page = device_private_entry_to_page(swpent);
 	} else if (unlikely(IS_ENABLED(CONFIG_SHMEM) && mss->check_shmem_swap
 							&& pte_none(*pte))) {
 		page = find_get_entry(vma->vm_file->f_mapping,
@@ -698,6 +700,8 @@ static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask,
 
 		if (is_migration_entry(swpent))
 			page = migration_entry_to_page(swpent);
+		else if (is_device_private_entry(swpent))
+			page = device_private_entry_to_page(swpent);
 	}
 	if (page) {
 		int mapcount = page_mapcount(page);
@@ -1198,6 +1202,9 @@ static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
 		flags |= PM_SWAP;
 		if (is_migration_entry(entry))
 			page = migration_entry_to_page(entry);
+
+		if (is_device_private_entry(entry))
+			page = device_private_entry_to_page(entry);
 	}
 
 	if (page && !PageAnon(page))
include/linux/ioport.h +1 -0
index 6230064d7f95..3a4f69137bc2 100644
@@ -130,6 +130,7 @@ enum {
 	IORES_DESC_ACPI_NV_STORAGE		= 3,
 	IORES_DESC_PERSISTENT_MEMORY		= 4,
 	IORES_DESC_PERSISTENT_MEMORY_LEGACY	= 5,
+	IORES_DESC_DEVICE_PRIVATE_MEMORY	= 6,
 };
 
 /* helpers to define resources */
include/linux/memremap.h +73 -0

97 lines not on this page · raw message

include/linux/mm.h +12 -0
index 9fee3213a75e..a59e149b958a 100644
@@ -788,11 +788,23 @@ static inline bool is_zone_device_page(const struct page *page)
 {
 	return page_zonenum(page) == ZONE_DEVICE;
 }
+
+static inline bool is_device_private_page(const struct page *page)
+{
+	/* See MEMORY_DEVICE_PRIVATE in include/linux/memory_hotplug.h */
+	return ((page_zonenum(page) == ZONE_DEVICE) &&
+		(page->pgmap->type == MEMORY_DEVICE_PRIVATE));
+}
 #else
 static inline bool is_zone_device_page(const struct page *page)
 {
 	return false;
 }
+
+static inline bool is_device_private_page(const struct page *page)
+{
+	return false;
+}
 #endif
 
 static inline void get_page(struct page *page)
include/linux/swap.h +21 -3
index 76f1632eea5a..12beb55ccdd4 100644
@@ -51,6 +51,23 @@ static inline int current_is_kswapd(void)
  */
 
 /*
+ * Unaddressable device memory support. See include/linux/hmm.h and
+ * Documentation/vm/hmm.txt. Short description is we need struct pages for
+ * device memory that is unaddressable (inaccessible) by CPU, so that we can
+ * migrate part of a process memory to device memory.
+ *
+ * When a page is migrated from CPU to device, we set the CPU page table entry
+ * to a special SWP_DEVICE_* entry.
+ */
+#ifdef CONFIG_DEVICE_PRIVATE
+#define SWP_DEVICE_NUM 2
+#define SWP_DEVICE_WRITE (MAX_SWAPFILES+SWP_HWPOISON_NUM+SWP_MIGRATION_NUM)
+#define SWP_DEVICE_READ (MAX_SWAPFILES+SWP_HWPOISON_NUM+SWP_MIGRATION_NUM+1)
+#else
+#define SWP_DEVICE_NUM 0
+#endif
+
+/*
  * NUMA node memory migration support
  */
 #ifdef CONFIG_MIGRATION
@@ -72,7 +89,8 @@ static inline int current_is_kswapd(void)
 #endif
 
 #define MAX_SWAPFILES \
-	((1 << MAX_SWAPFILES_SHIFT) - SWP_MIGRATION_NUM - SWP_HWPOISON_NUM)
+	((1 << MAX_SWAPFILES_SHIFT) - SWP_DEVICE_NUM - \
+	SWP_MIGRATION_NUM - SWP_HWPOISON_NUM)
 
 /*
  * Magic header for a swap area. The first part of the union is
@@ -435,8 +453,8 @@ static inline void show_swap_cache_info(void)
 {
 }
 
-#define free_swap_and_cache(swp)	is_migration_entry(swp)
-#define swapcache_prepare(swp)		is_migration_entry(swp)
+#define free_swap_and_cache(e) (is_migration_entry(e) || is_device_private_entry(e))
+#define swapcache_prepare(e) (is_migration_entry(e) || is_device_private_entry(e))
 
 static inline int add_swap_count_continuation(swp_entry_t swp, gfp_t gfp_mask)
 {
include/linux/swapops.h +68 -0

74 lines not on this page · raw message

kernel/memremap.c +34 -0
index 9afdc434fb49..d3241f51c1f0 100644
@@ -18,6 +18,8 @@
 #include <linux/io.h>
 #include <linux/mm.h>
 #include <linux/memory_hotplug.h>
+#include <linux/swap.h>
+#include <linux/swapops.h>
 
 #ifndef ioremap_cache
 /* temporary while we convert existing ioremap_cache users to memremap */
@@ -194,6 +196,34 @@ struct page_map {
 	struct vmem_altmap altmap;
 };
 
+#if IS_ENABLED(CONFIG_DEVICE_PRIVATE)
+int device_private_entry_fault(struct vm_area_struct *vma,
+		       unsigned long addr,
+		       swp_entry_t entry,
+		       unsigned int flags,
+		       pmd_t *pmdp)
+{
+	struct page *page = device_private_entry_to_page(entry);
+
+	/*
+	 * The page_fault() callback must migrate page back to system memory
+	 * so that CPU can access it. This might fail for various reasons
+	 * (device issue, device was unsafely unplugged, ...). When such
+	 * error conditions happen, the callback must return VM_FAULT_SIGBUS.
+	 *
+	 * Note that because memory cgroup charges are accounted to the device
+	 * memory, this should never fail because of memory restrictions (but
+	 * allocation of regular system page might still fail because we are
+	 * out of memory).
+	 *
+	 * There is a more in-depth description of what that callback can and
+	 * cannot do, in include/linux/memremap.h
+	 */
+	return page->pgmap->page_fault(vma, addr, page, flags, pmdp);
+}
+EXPORT_SYMBOL(device_private_entry_fault);
+#endif /* CONFIG_DEVICE_PRIVATE */
+
 static void pgmap_radix_release(struct resource *res)
 {
 	resource_size_t key, align_start, align_size, align_end;
@@ -333,6 +363,10 @@ void *devm_memremap_pages(struct device *dev, struct resource *res,
 	}
 	pgmap->ref = ref;
 	pgmap->res = &page_map->res;
+	pgmap->type = MEMORY_DEVICE_HOST;
+	pgmap->page_fault = NULL;
+	pgmap->page_free = NULL;
+	pgmap->data = NULL;
 
 	mutex_lock(&pgmap_lock);
 	error = 0;
mm/Kconfig +10 -1
index 87d530cbbbc7..c07cb590b2d6 100644
@@ -676,7 +676,7 @@ config ARCH_HAS_ZONE_DEVICE
 	bool
 
 config ZONE_DEVICE
-	bool "Device memory (pmem, etc...) hotplug support"
+	bool "Device memory (pmem, HMM, etc...) hotplug support"
 	depends on MEMORY_HOTPLUG
 	depends on MEMORY_HOTREMOVE
 	depends on SPARSEMEM_VMEMMAP
@@ -716,6 +716,15 @@ config HMM_MIRROR
 	  page tables (at PAGE_SIZE granularity), and must be able to recover from
 	  the resulting potential page faults.
 
+config DEVICE_PRIVATE
+	bool "Unaddressable device memory (GPU memory, ...)"
+	depends on ARCH_HAS_HMM
+
+	help
+	  Allows creation of struct pages to represent unaddressable device
+	  memory; i.e., memory that is only accessible from the device (or
+	  group of devices). You likely also want to select HMM_MIRROR.
+
 config FRAME_VECTOR
 	bool
 
mm/memory.c +61 -0

85 lines not on this page · raw message

mm/memory_hotplug.c +8 -2
index 1de2f132bca3..459bbc182d10 100644
@@ -99,7 +99,7 @@ void mem_hotplug_done(void)
 /* add this memory to iomem resource */
 static struct resource *register_memory_resource(u64 start, u64 size)
 {
-	struct resource *res;
+	struct resource *res, *conflict;
 	res = kzalloc(sizeof(struct resource), GFP_KERNEL);
 	if (!res)
 		return ERR_PTR(-ENOMEM);
@@ -108,7 +108,13 @@ static struct resource *register_memory_resource(u64 start, u64 size)
 	res->start = start;
 	res->end = start + size - 1;
 	res->flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
-	if (request_resource(&iomem_resource, res) < 0) {
+	conflict =  request_resource_conflict(&iomem_resource, res);
+	if (conflict) {
+		if (conflict->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY) {
+			pr_debug("Device unaddressable memory block "
+				 "memory hotplug at %#010llx !\n",
+				 (unsigned long long)start);
+		}
 		pr_debug("System RAM resource %pR cannot be added\n", res);
 		kfree(res);
 		return ERR_PTR(-EEXIST);
mm/mprotect.c +14 -0
index 5be04e28709f..b70de621ee83 100644
@@ -125,6 +125,20 @@ static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
 
 				pages++;
 			}
+
+			if (is_write_device_private_entry(entry)) {
+				pte_t newpte;
+
+				/*
+				 * We do not preserve soft-dirtiness. See
+				 * copy_one_pte() for explanation.
+				 */
+				make_device_private_entry_read(&entry);
+				newpte = swp_entry_to_pte(entry);
+				set_pte_at(mm, addr, pte, newpte);
+
+				pages++;
+			}
 		}
 	} while (pte++, addr += PAGE_SIZE, addr != end);
 	arch_leave_lazy_mmu_mode();
Signature · 7 lines
2.13.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#8 lore raw

Re: [HMM-v25 07/19] mm/ZONE_DEVICE: new type of ZONE_DEVICE for unaddressable memory v5

Quoted 48 lines · Jérôme Glisse

HMM (heterogeneous memory management) need struct page to support migration from system main memory to device memory. Reasons for HMM and migration to device memory is explained with HMM core patch.

This patch deals with device memory that is un-addressable memory (ie CPU can not access it). Hence we do not want those struct page to be manage like regular memory. That is why we extend ZONE_DEVICE to support different types of memory.

A persistent memory type is define for existing user of ZONE_DEVICE and a new device un-addressable type is added for the un-addressable memory type. There is a clear separation between what is expected from each memory type and existing user of ZONE_DEVICE are un-affected by new requirement and new use of the un-addressable type. All specific code path are protect with test against the memory type.

Because memory is un-addressable we use a new special swap type for when a page is migrated to device memory (this reduces the number of maximum swap file).

The main two additions beside memory type to ZONE_DEVICE is two callbacks. First one, page_free() is call whenever page refcount reach 1 (which means the page is free as ZONE_DEVICE page never reach a refcount of 0). This allow device driver to manage its memory and associated struct page.

The second callback page_fault() happens when there is a CPU access to an address that is back by a device page (which are un-addressable by the CPU). This callback is responsible to migrate the page back to system main memory. Device driver can not block migration back to system memory, HMM make sure that such page can not be pin into device memory.

If device is in some error condition and can not migrate memory back then a CPU page fault to device memory should end with SIGBUS.

Changed since v4:
- s/DEVICE_PUBLIC/DEVICE_HOST (to free DEVICE_PUBLIC for HMM-CDM)
Changed since v3:
- fix comments that was still using UNADDRESSABLE as keyword
- kernel configuration simplification
Changed since v2:
- s/DEVICE_UNADDRESSABLE/DEVICE_PRIVATE
Changed since v1:
- rename to device private memory (from device unaddressable)

[..]

Quoted 13 lines
 fs/proc/task_mmu.c       |  7 +++++
 include/linux/ioport.h   |  1 +
 include/linux/memremap.h | 73 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/mm.h       | 12 ++++++++
 include/linux/swap.h     | 24 ++++++++++++++--
 include/linux/swapops.h  | 68 ++++++++++++++++++++++++++++++++++++++++++++
 kernel/memremap.c        | 34 ++++++++++++++++++++++
 mm/Kconfig               | 11 +++++++-
 mm/memory.c              | 61 ++++++++++++++++++++++++++++++++++++++++
 mm/memory_hotplug.c      | 10 +++++--
 mm/mprotect.c            | 14 ++++++++++
 11 files changed, 309 insertions(+), 6 deletions(-)

[..]

include/linux/memremap.h line 8

Quoted 10 lines
include/linux/memremap.h +2 -0
index 93416196ba64..8e164ec9eed0 100644
@@ -4,6 +4,8 @@
 #include <linux/ioport.h>
 #include <linux/percpu-refcount.h>
 
+#include <asm/pgtable.h>
+

So it turns out, over a year later, that this include was a mistake and makes the build fragile.

Quoted 3 lines
include/linux/memremap.h +0 -0
 struct resource;
 struct device;
 

[..]

include/linux/memremap.h static inline struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start) · line 100

Quoted 5 lines
include/linux/memremap.h +5 -0
+typedef int (*dev_page_fault_t)(struct vm_area_struct *vma,
+                               unsigned long addr,
+                               const struct page *page,
+                               unsigned int flags,
+                               pmd_t *pmdp);

I recently included this file somewhere that did not have a pile of other mm headers included and 0day reports:

In file included from arch/m68k/include/asm/pgtable_mm.h:148:0,

                    from arch/m68k/include/asm/pgtable.h:5,
                    from include/linux/memremap.h:7,
                    from drivers//dax/bus.c:3:

arch/m68k/include/asm/motorola_pgtable.h: In function 'pgd_offset':

Quoted 1 line
Quoted 1 line

arch/m68k/include/asm/motorola_pgtable.h:199:11: error: dereferencing pointer to incomplete type 'const struct mm_struct'

     return mm->pgd + pgd_index(address);
              ^~

I assume this pulls in the entirety of pgtable.h just to get the pmd_t definition?

Quoted 5 lines
include/linux/memremap.h +3 -0
+typedef void (*dev_page_free_t)(struct page *page, void *data);
+
 /**
  * struct dev_pagemap - metadata for ZONE_DEVICE mappings
+ * @page_fault: callback when CPU fault on an unaddressable device page

include/linux/memremap.h static inline struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start) · line 115

Quoted 10 lines
include/linux/memremap.h +4 -0
+ * @page_free: free page callback when page refcount reaches 1
  * @altmap: pre-allocated/reserved memory for vmemmap allocations
  * @res: physical address range covered by @ref
  * @ref: reference count that pins the devm_memremap_pages() mapping
  * @dev: host device of the mapping for debug
+ * @data: private data pointer for page_free()
+ * @type: memory type: see MEMORY_* in memory_hotplug.h
  */
 struct dev_pagemap {
+       dev_page_fault_t page_fault;

Rather than try to figure out how to forward declare pmd_t, how about just move dev_page_fault_t out of the generic dev_pagemap and into the HMM specific container structure? This should be straightfoward on top of the recent refactor.

#9 lore raw

Re: [HMM-v25 07/19] mm/ZONE_DEVICE: new type of ZONE_DEVICE for unaddressable memory v5

Quoted 114 lines · Dan Williams
Quoted 48 lines · J�r�me Glisse

HMM (heterogeneous memory management) need struct page to support migration from system main memory to device memory. Reasons for HMM and migration to device memory is explained with HMM core patch.

This patch deals with device memory that is un-addressable memory (ie CPU can not access it). Hence we do not want those struct page to be manage like regular memory. That is why we extend ZONE_DEVICE to support different types of memory.

A persistent memory type is define for existing user of ZONE_DEVICE and a new device un-addressable type is added for the un-addressable memory type. There is a clear separation between what is expected from each memory type and existing user of ZONE_DEVICE are un-affected by new requirement and new use of the un-addressable type. All specific code path are protect with test against the memory type.

Because memory is un-addressable we use a new special swap type for when a page is migrated to device memory (this reduces the number of maximum swap file).

The main two additions beside memory type to ZONE_DEVICE is two callbacks. First one, page_free() is call whenever page refcount reach 1 (which means the page is free as ZONE_DEVICE page never reach a refcount of 0). This allow device driver to manage its memory and associated struct page.

The second callback page_fault() happens when there is a CPU access to an address that is back by a device page (which are un-addressable by the CPU). This callback is responsible to migrate the page back to system main memory. Device driver can not block migration back to system memory, HMM make sure that such page can not be pin into device memory.

If device is in some error condition and can not migrate memory back then a CPU page fault to device memory should end with SIGBUS.

Changed since v4:
- s/DEVICE_PUBLIC/DEVICE_HOST (to free DEVICE_PUBLIC for HMM-CDM)
Changed since v3:
- fix comments that was still using UNADDRESSABLE as keyword
- kernel configuration simplification
Changed since v2:
- s/DEVICE_UNADDRESSABLE/DEVICE_PRIVATE
Changed since v1:
- rename to device private memory (from device unaddressable)

[..]

Quoted 13 lines
 fs/proc/task_mmu.c       |  7 +++++
 include/linux/ioport.h   |  1 +
 include/linux/memremap.h | 73 ++++++++++++++++++++++++++++++++++++++++++++++++
 include/linux/mm.h       | 12 ++++++++
 include/linux/swap.h     | 24 ++++++++++++++--
 include/linux/swapops.h  | 68 ++++++++++++++++++++++++++++++++++++++++++++
 kernel/memremap.c        | 34 ++++++++++++++++++++++
 mm/Kconfig               | 11 +++++++-
 mm/memory.c              | 61 ++++++++++++++++++++++++++++++++++++++++
 mm/memory_hotplug.c      | 10 +++++--
 mm/mprotect.c            | 14 ++++++++++
 11 files changed, 309 insertions(+), 6 deletions(-)

[..]

include/linux/memremap.h line 8

Quoted 10 lines
include/linux/memremap.h +2 -0
index 93416196ba64..8e164ec9eed0 100644
@@ -4,6 +4,8 @@
 #include <linux/ioport.h>
 #include <linux/percpu-refcount.h>
 
+#include <asm/pgtable.h>
+

So it turns out, over a year later, that this include was a mistake and makes the build fragile.

Quoted 3 lines
include/linux/memremap.h +0 -0
 struct resource;
 struct device;
 

[..]

include/linux/memremap.h static inline struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start) · line 100

Quoted 5 lines
include/linux/memremap.h +5 -0
+typedef int (*dev_page_fault_t)(struct vm_area_struct *vma,
+                               unsigned long addr,
+                               const struct page *page,
+                               unsigned int flags,
+                               pmd_t *pmdp);

I recently included this file somewhere that did not have a pile of other mm headers included and 0day reports:

In file included from arch/m68k/include/asm/pgtable_mm.h:148:0,

                    from arch/m68k/include/asm/pgtable.h:5,
                    from include/linux/memremap.h:7,
                    from drivers//dax/bus.c:3:

arch/m68k/include/asm/motorola_pgtable.h: In function 'pgd_offset':

Quoted 1 line
Quoted 1 line

arch/m68k/include/asm/motorola_pgtable.h:199:11: error: dereferencing pointer to incomplete type 'const struct mm_struct'

     return mm->pgd + pgd_index(address);
              ^~

I assume this pulls in the entirety of pgtable.h just to get the pmd_t definition?

Quoted 12 lines
include/linux/memremap.h +6 -0
+typedef void (*dev_page_free_t)(struct page *page, void *data);
+
 /**
  * struct dev_pagemap - metadata for ZONE_DEVICE mappings
+ * @page_fault: callback when CPU fault on an unaddressable device page
+ * @page_free: free page callback when page refcount reaches 1
  * @altmap: pre-allocated/reserved memory for vmemmap allocations
  * @res: physical address range covered by @ref
  * @ref: reference count that pins the devm_memremap_pages() mapping
  * @dev: host device of the mapping for debug
+ * @data: private data pointer for page_free()
+ * @type: memory type: see MEMORY_* in memory_hotplug.h
Quoted 8 lines
Quoted 3 lines

*/
struct dev_pagemap {
+ dev_page_fault_t page_fault;

Rather than try to figure out how to forward declare pmd_t, how about just move dev_page_fault_t out of the generic dev_pagemap and into the HMM specific container structure? This should be straightfoward on top of the recent refactor.

Fine with me.

Cheers,
J�r�me

#10 lore raw

Re: [HMM-v25 07/19] mm/ZONE_DEVICE: new type of ZONE_DEVICE for unaddressable memory v5

On Thu, Dec 20, 2018 at 8:15 AM Jerome Glisse <[email protected]> wrote:
[..]

Quoted 6 lines
Quoted 4 lines

Rather than try to figure out how to forward declare pmd_t, how about just move dev_page_fault_t out of the generic dev_pagemap and into the HMM specific container structure? This should be straightfoward on top of the recent refactor.

Fine with me.

I was hoping you would reply with a patch. I'll take a look...

#11 lore raw

Re: [HMM-v25 07/19] mm/ZONE_DEVICE: new type of ZONE_DEVICE for unaddressable memory v5

Quoted 10 lines · Dan Williams

On Thu, Dec 20, 2018 at 8:15 AM Jerome Glisse <[email protected]> wrote:
[..]

Quoted 6 lines
Quoted 4 lines

Rather than try to figure out how to forward declare pmd_t, how about just move dev_page_fault_t out of the generic dev_pagemap and into the HMM specific container structure? This should be straightfoward on top of the recent refactor.

Fine with me.

I was hoping you would reply with a patch. I'll take a look...

Bit busy right now but i can do that after new years :)

Cheers,
J�r�me

#12 lore raw

PATCH 8/19 mm/ZONE_DEVICE: special case put_page() for device private pages v4

In reply to Jérôme Glisse

A ZONE_DEVICE page that reach a refcount of 1 is free ie no longer have any user. For device private pages this is important to catch and thus we need to special case put_page() for this.

Changed since v3:
- clear page mapping field
Changed since v2:
- clear page active and waiters
Changed since v1:
- use static key to disable special code path in put_page() by default
- uninline put_zone_device_private_page()
- fix build issues with some kernel config related to header inter-dependency

Cc Kirill A. Shutemov <[email protected]>
Cc Dan Williams <[email protected]>
Cc Ross Zwisler <[email protected]>
---
 include/linux/memremap.h | 13 +++++++++++++
 include/linux/mm.h       | 31 ++++++++++++++++++++++---------
 kernel/memremap.c        | 25 ++++++++++++++++++++++++-
 mm/hmm.c                 |  8 ++++++++
 4 files changed, 67 insertions(+), 10 deletions(-)
include/linux/memremap.h +13 -0
index 8e164ec9eed0..8aa6b82679e2 100644
@@ -126,6 +126,14 @@ struct dev_pagemap {
 void *devm_memremap_pages(struct device *dev, struct resource *res,
 		struct percpu_ref *ref, struct vmem_altmap *altmap);
 struct dev_pagemap *find_dev_pagemap(resource_size_t phys);
+
+static inline bool is_zone_device_page(const struct page *page);
+
+static inline bool is_device_private_page(const struct page *page)
+{
+	return is_zone_device_page(page) &&
+		page->pgmap->type == MEMORY_DEVICE_PRIVATE;
+}
 #else
 static inline void *devm_memremap_pages(struct device *dev,
 		struct resource *res, struct percpu_ref *ref,
@@ -144,6 +152,11 @@ static inline struct dev_pagemap *find_dev_pagemap(resource_size_t phys)
 {
 	return NULL;
 }
+
+static inline bool is_device_private_page(const struct page *page)
+{
+	return false;
+}
 #endif
 
 /**
include/linux/mm.h +22 -9
index a59e149b958a..515d4ae611b2 100644
@@ -23,6 +23,7 @@
 #include <linux/page_ext.h>
 #include <linux/err.h>
 #include <linux/page_ref.h>
+#include <linux/memremap.h>
 
 struct mempolicy;
 struct anon_vma;
@@ -788,25 +789,25 @@ static inline bool is_zone_device_page(const struct page *page)
 {
 	return page_zonenum(page) == ZONE_DEVICE;
 }
-
-static inline bool is_device_private_page(const struct page *page)
-{
-	/* See MEMORY_DEVICE_PRIVATE in include/linux/memory_hotplug.h */
-	return ((page_zonenum(page) == ZONE_DEVICE) &&
-		(page->pgmap->type == MEMORY_DEVICE_PRIVATE));
-}
 #else
 static inline bool is_zone_device_page(const struct page *page)
 {
 	return false;
 }
+#endif
 
-static inline bool is_device_private_page(const struct page *page)
+#ifdef CONFIG_DEVICE_PRIVATE
+void put_zone_device_private_page(struct page *page);
+#else
+static inline void put_zone_device_private_page(struct page *page)
 {
-	return false;
 }
 #endif
 
+static inline bool is_device_private_page(const struct page *page);
+
+DECLARE_STATIC_KEY_FALSE(device_private_key);
+
 static inline void get_page(struct page *page)
 {
 	page = compound_head(page);
@@ -822,6 +823,18 @@ static inline void put_page(struct page *page)
 {
 	page = compound_head(page);
 
+	/*
+	 * For private device pages we need to catch refcount transition from
+	 * 2 to 1, when refcount reach one it means the private device page is
+	 * free and we need to inform the device driver through callback. See
+	 * include/linux/memremap.h and HMM for details.
+	 */
+	if (static_branch_unlikely(&device_private_key) &&
+	    unlikely(is_device_private_page(page))) {
+		put_zone_device_private_page(page);
+		return;
+	}
+
 	if (put_page_testzero(page))
 		__put_page(page);
 }
kernel/memremap.c +24 -1
index d3241f51c1f0..398630c1fba3 100644
@@ -11,7 +11,6 @@
  * General Public License for more details.
  */
 #include <linux/radix-tree.h>
-#include <linux/memremap.h>
 #include <linux/device.h>
 #include <linux/types.h>
 #include <linux/pfn_t.h>
@@ -476,3 +475,27 @@ struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start)
 	return pgmap ? pgmap->altmap : NULL;
 }
 #endif /* CONFIG_ZONE_DEVICE */
+
+
+#ifdef CONFIG_DEVICE_PRIVATE
+void put_zone_device_private_page(struct page *page)
+{
+	int count = page_ref_dec_return(page);
+
+	/*
+	 * If refcount is 1 then page is freed and refcount is stable as nobody
+	 * holds a reference on the page.
+	 */
+	if (count == 1) {
+		/* Clear Active bit in case of parallel mark_page_accessed */
+		__ClearPageActive(page);
+		__ClearPageWaiters(page);
+
+		page->mapping = NULL;
+
+		page->pgmap->page_free(page, page->pgmap->data);
+	} else if (!count)
+		__put_page(page);
+}
+EXPORT_SYMBOL(put_zone_device_private_page);
+#endif /* CONFIG_DEVICE_PRIVATE */
mm/hmm.c +8 -0
index 91592dae364e..e615f337110a 100644
@@ -25,9 +25,17 @@
 #include <linux/sched.h>
 #include <linux/swapops.h>
 #include <linux/hugetlb.h>
+#include <linux/jump_label.h>
 #include <linux/mmu_notifier.h>
 
 
+/*
+ * Device private memory see HMM (Documentation/vm/hmm.txt) or hmm.h
+ */
+DEFINE_STATIC_KEY_FALSE(device_private_key);
+EXPORT_SYMBOL(device_private_key);
+
+
 #ifdef CONFIG_HMM
 static const struct mmu_notifier_ops hmm_mmu_notifier_ops;
 
Signature · 7 lines
2.13.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#13 lore raw

PATCH 9/19 mm/memcontrol: allow to uncharge page without using page->lru field

In reply to Jérôme Glisse

HMM pages (private or public device pages) are ZONE_DEVICE page and thus you can not use page->lru fields of those pages. This patch re-arrange the uncharge to allow single page to be uncharge without modifying the lru field of the struct page.

There is no change to memcontrol logic, it is the same as it was before this patch.

Cc Johannes Weiner <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Vladimir Davydov <[email protected]>
---
 mm/memcontrol.c | 168 +++++++++++++++++++++++++++++++-------------------------
 1 file changed, 92 insertions(+), 76 deletions(-)
mm/memcontrol.c +92 -76
index df6f63ee95d6..604fb3ca8028 100644
@@ -5533,48 +5533,102 @@ void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg,
 	cancel_charge(memcg, nr_pages);
 }
 
-static void uncharge_batch(struct mem_cgroup *memcg, unsigned long pgpgout,
-			   unsigned long nr_anon, unsigned long nr_file,
-			   unsigned long nr_kmem, unsigned long nr_huge,
-			   unsigned long nr_shmem, struct page *dummy_page)
+struct uncharge_gather {
+	struct mem_cgroup *memcg;
+	unsigned long pgpgout;
+	unsigned long nr_anon;
+	unsigned long nr_file;
+	unsigned long nr_kmem;
+	unsigned long nr_huge;
+	unsigned long nr_shmem;
+	struct page *dummy_page;
+};
+
+static inline void uncharge_gather_clear(struct uncharge_gather *ug)
 {
-	unsigned long nr_pages = nr_anon + nr_file + nr_kmem;
+	memset(ug, 0, sizeof(*ug));
+}
+
+static void uncharge_batch(const struct uncharge_gather *ug)
+{
+	unsigned long nr_pages = ug->nr_anon + ug->nr_file + ug->nr_kmem;
 	unsigned long flags;
 
-	if (!mem_cgroup_is_root(memcg)) {
-		page_counter_uncharge(&memcg->memory, nr_pages);
+	if (!mem_cgroup_is_root(ug->memcg)) {
+		page_counter_uncharge(&ug->memcg->memory, nr_pages);
 		if (do_memsw_account())
-			page_counter_uncharge(&memcg->memsw, nr_pages);
-		if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && nr_kmem)
-			page_counter_uncharge(&memcg->kmem, nr_kmem);
-		memcg_oom_recover(memcg);
+			page_counter_uncharge(&ug->memcg->memsw, nr_pages);
+		if (!cgroup_subsys_on_dfl(memory_cgrp_subsys) && ug->nr_kmem)
+			page_counter_uncharge(&ug->memcg->kmem, ug->nr_kmem);
+		memcg_oom_recover(ug->memcg);
 	}
 
 	local_irq_save(flags);
-	__this_cpu_sub(memcg->stat->count[MEMCG_RSS], nr_anon);
-	__this_cpu_sub(memcg->stat->count[MEMCG_CACHE], nr_file);
-	__this_cpu_sub(memcg->stat->count[MEMCG_RSS_HUGE], nr_huge);
-	__this_cpu_sub(memcg->stat->count[NR_SHMEM], nr_shmem);
-	__this_cpu_add(memcg->stat->events[PGPGOUT], pgpgout);
-	__this_cpu_add(memcg->stat->nr_page_events, nr_pages);
-	memcg_check_events(memcg, dummy_page);
+	__this_cpu_sub(ug->memcg->stat->count[MEMCG_RSS], ug->nr_anon);
+	__this_cpu_sub(ug->memcg->stat->count[MEMCG_CACHE], ug->nr_file);
+	__this_cpu_sub(ug->memcg->stat->count[MEMCG_RSS_HUGE], ug->nr_huge);
+	__this_cpu_sub(ug->memcg->stat->count[NR_SHMEM], ug->nr_shmem);
+	__this_cpu_add(ug->memcg->stat->events[PGPGOUT], ug->pgpgout);
+	__this_cpu_add(ug->memcg->stat->nr_page_events, nr_pages);
+	memcg_check_events(ug->memcg, ug->dummy_page);
 	local_irq_restore(flags);
 
-	if (!mem_cgroup_is_root(memcg))
-		css_put_many(&memcg->css, nr_pages);
+	if (!mem_cgroup_is_root(ug->memcg))
+		css_put_many(&ug->memcg->css, nr_pages);
+}
+
+static void uncharge_page(struct page *page, struct uncharge_gather *ug)
+{
+	VM_BUG_ON_PAGE(PageLRU(page), page);
+	VM_BUG_ON_PAGE(!PageHWPoison(page) && page_count(page), page);
+
+	if (!page->mem_cgroup)
+		return;
+
+	/*
+	 * Nobody should be changing or seriously looking at
+	 * page->mem_cgroup at this point, we have fully
+	 * exclusive access to the page.
+	 */
+
+	if (ug->memcg != page->mem_cgroup) {
+		if (ug->memcg) {
+			uncharge_batch(ug);
+			uncharge_gather_clear(ug);
+		}
+		ug->memcg = page->mem_cgroup;
+	}
+
+	if (!PageKmemcg(page)) {
+		unsigned int nr_pages = 1;
+
+		if (PageTransHuge(page)) {
+			nr_pages <<= compound_order(page);
+			ug->nr_huge += nr_pages;
+		}
+		if (PageAnon(page))
+			ug->nr_anon += nr_pages;
+		else {
+			ug->nr_file += nr_pages;
+			if (PageSwapBacked(page))
+				ug->nr_shmem += nr_pages;
+		}
+		ug->pgpgout++;
+	} else {
+		ug->nr_kmem += 1 << compound_order(page);
+		__ClearPageKmemcg(page);
+	}
+
+	ug->dummy_page = page;
+	page->mem_cgroup = NULL;
 }
 
 static void uncharge_list(struct list_head *page_list)
 {
-	struct mem_cgroup *memcg = NULL;
-	unsigned long nr_shmem = 0;
-	unsigned long nr_anon = 0;
-	unsigned long nr_file = 0;
-	unsigned long nr_huge = 0;
-	unsigned long nr_kmem = 0;
-	unsigned long pgpgout = 0;
+	struct uncharge_gather ug;
 	struct list_head *next;
-	struct page *page;
+
+	uncharge_gather_clear(&ug);
 
 	/*
 	 * Note that the list can be a single page->lru; hence the
@@ -5582,57 +5636,16 @@ static void uncharge_list(struct list_head *page_list)
 	 */
 	next = page_list->next;
 	do {
+		struct page *page;
+
 		page = list_entry(next, struct page, lru);
 		next = page->lru.next;
 
-		VM_BUG_ON_PAGE(PageLRU(page), page);
-		VM_BUG_ON_PAGE(!PageHWPoison(page) && page_count(page), page);
-
-		if (!page->mem_cgroup)
-			continue;
-
-		/*
-		 * Nobody should be changing or seriously looking at
-		 * page->mem_cgroup at this point, we have fully
-		 * exclusive access to the page.
-		 */
-
-		if (memcg != page->mem_cgroup) {
-			if (memcg) {
-				uncharge_batch(memcg, pgpgout, nr_anon, nr_file,
-					       nr_kmem, nr_huge, nr_shmem, page);
-				pgpgout = nr_anon = nr_file = nr_kmem = 0;
-				nr_huge = nr_shmem = 0;
-			}
-			memcg = page->mem_cgroup;
-		}
-
-		if (!PageKmemcg(page)) {
-			unsigned int nr_pages = 1;
-
-			if (PageTransHuge(page)) {
-				nr_pages <<= compound_order(page);
-				nr_huge += nr_pages;
-			}
-			if (PageAnon(page))
-				nr_anon += nr_pages;
-			else {
-				nr_file += nr_pages;
-				if (PageSwapBacked(page))
-					nr_shmem += nr_pages;
-			}
-			pgpgout++;
-		} else {
-			nr_kmem += 1 << compound_order(page);
-			__ClearPageKmemcg(page);
-		}
-
-		page->mem_cgroup = NULL;
+		uncharge_page(page, &ug);
 	} while (next != page_list);
 
-	if (memcg)
-		uncharge_batch(memcg, pgpgout, nr_anon, nr_file,
-			       nr_kmem, nr_huge, nr_shmem, page);
+	if (ug.memcg)
+		uncharge_batch(&ug);
 }
 
 /**
@@ -5644,6 +5657,8 @@ static void uncharge_list(struct list_head *page_list)
  */
 void mem_cgroup_uncharge(struct page *page)
 {
+	struct uncharge_gather ug;
+
 	if (mem_cgroup_disabled())
 		return;
 
@@ -5651,8 +5666,9 @@ void mem_cgroup_uncharge(struct page *page)
 	if (!page->mem_cgroup)
 		return;
 
-	INIT_LIST_HEAD(&page->lru);
-	uncharge_list(&page->lru);
+	uncharge_gather_clear(&ug);
+	uncharge_page(page, &ug);
+	uncharge_batch(&ug);
 }
 
 /**
Signature · 7 lines
2.13.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#14 lore raw

PATCH 10/19 mm/memcontrol: support MEMORY_DEVICE_PRIVATE v4

In reply to Jérôme Glisse

HMM pages (private or public device pages) are ZONE_DEVICE page and thus need special handling when it comes to lru or refcount. This patch make sure that memcontrol properly handle those when it face them. Those pages are use like regular pages in a process address space either as anonymous page or as file back page. So from memcg point of view we want to handle them like regular page for now at least.

Changed since v3:
- remove public support and move those chunk to separate patch
Changed since v2:
- s/host/public
Changed since v1:
- s/public/host
- add comments explaining how device memory behave and why

---
 kernel/memremap.c |  1 +
 mm/memcontrol.c   | 52 ++++++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 49 insertions(+), 4 deletions(-)
kernel/memremap.c +1 -0
index 398630c1fba3..f42d7483e886 100644
@@ -492,6 +492,7 @@ void put_zone_device_private_page(struct page *page)
 		__ClearPageWaiters(page);
 
 		page->mapping = NULL;
+		mem_cgroup_uncharge(page);
 
 		page->pgmap->page_free(page, page->pgmap->data);
 	} else if (!count)
mm/memcontrol.c +48 -4

123 lines not on this page · raw message

Signature · 1 line
2.13.4
#15 lore raw

Re: [HMM-v25 10/19] mm/memcontrol: support MEMORY_DEVICE_PRIVATE v4

Quoted 45 lines · Jérôme Glisse

HMM pages (private or public device pages) are ZONE_DEVICE page and thus need special handling when it comes to lru or refcount. This patch make sure that memcontrol properly handle those when it face them. Those pages are use like regular pages in a process address space either as anonymous page or as file back page. So from memcg point of view we want to handle them like regular page for now at least.

Changed since v3:
- remove public support and move those chunk to separate patch
Changed since v2:
- s/host/public
Changed since v1:
- s/public/host
- add comments explaining how device memory behave and why

---
 kernel/memremap.c |  1 +
 mm/memcontrol.c   | 52 ++++++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 49 insertions(+), 4 deletions(-)
kernel/memremap.c +1 -0
index 398630c1fba3..f42d7483e886 100644
@@ -492,6 +492,7 @@ void put_zone_device_private_page(struct page *page)
 		__ClearPageWaiters(page);
 
 		page->mapping = NULL;
+		mem_cgroup_uncharge(page);
 
 		page->pgmap->page_free(page, page->pgmap->data);
 	} else if (!count)
mm/memcontrol.c +0 -0
index 604fb3ca8028..977d1cf3493a 100644
@@ -4407,12 +4407,13 @@ enum mc_target_type {
 	MC_TARGET_NONE = 0,

mm/memcontrol.c enum mc_target_type { · line 4416

Quoted 10 lines
mm/memcontrol.c +2 -1
 	MC_TARGET_PAGE,
 	MC_TARGET_SWAP,
+	MC_TARGET_DEVICE,
 };
 
 static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
 						unsigned long addr, pte_t ptent)
 {
-	struct page *page = vm_normal_page(vma, addr, ptent);
+	struct page *page = _vm_normal_page(vma, addr, ptent, true);

Hi Jérôme,

As _vm_normal_page() is defined later in the patch 18, so this patch should
break the bisectability.

Cheers,
Laurent.

Quoted 122 lines
mm/memcontrol.c +46 -3
 	if (!page || !page_mapped(page))
 		return NULL;
@@ -4429,7 +4430,7 @@ static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
 	return page;
 }
 
-#ifdef CONFIG_SWAP
+#if defined(CONFIG_SWAP) || defined(CONFIG_DEVICE_PRIVATE)
 static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
 			pte_t ptent, swp_entry_t *entry)
 {
@@ -4438,6 +4439,23 @@ static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
 
 	if (!(mc.flags & MOVE_ANON) || non_swap_entry(ent))
 		return NULL;
+
+	/*
+	 * Handle MEMORY_DEVICE_PRIVATE which are ZONE_DEVICE page belonging to
+	 * a device and because they are not accessible by CPU they are store
+	 * as special swap entry in the CPU page table.
+	 */
+	if (is_device_private_entry(ent)) {
+		page = device_private_entry_to_page(ent);
+		/*
+		 * MEMORY_DEVICE_PRIVATE means ZONE_DEVICE page and which have
+		 * a refcount of 1 when free (unlike normal page)
+		 */
+		if (!page_ref_add_unless(page, 1, 1))
+			return NULL;
+		return page;
+	}
+
 	/*
 	 * Because lookup_swap_cache() updates some statistics counter,
 	 * we call find_get_page() with swapper_space directly.
@@ -4598,6 +4616,12 @@ static int mem_cgroup_move_account(struct page *page,
  *   2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
  *     target for charge migration. if @target is not NULL, the entry is stored
  *     in target->ent.
+ *   3(MC_TARGET_DEVICE): like MC_TARGET_PAGE  but page is MEMORY_DEVICE_PRIVATE
+ *     (so ZONE_DEVICE page and thus not on the lru). For now we such page is
+ *     charge like a regular page would be as for all intent and purposes it is
+ *     just special memory taking the place of a regular page.
+ *
+ *     See Documentations/vm/hmm.txt and include/linux/hmm.h
  *
  * Called with pte lock held.
  */
@@ -4626,6 +4650,8 @@ static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
 		 */
 		if (page->mem_cgroup == mc.from) {
 			ret = MC_TARGET_PAGE;
+			if (is_device_private_page(page))
+				ret = MC_TARGET_DEVICE;
 			if (target)
 				target->page = page;
 		}
@@ -4693,6 +4719,11 @@ static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
 
 	ptl = pmd_trans_huge_lock(pmd, vma);
 	if (ptl) {
+		/*
+		 * Note their can not be MC_TARGET_DEVICE for now as we do not
+		 * support transparent huge page with MEMORY_DEVICE_PUBLIC or
+		 * MEMORY_DEVICE_PRIVATE but this might change.
+		 */
 		if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
 			mc.precharge += HPAGE_PMD_NR;
 		spin_unlock(ptl);
@@ -4908,6 +4939,14 @@ static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
 				putback_lru_page(page);
 			}
 			put_page(page);
+		} else if (target_type == MC_TARGET_DEVICE) {
+			page = target.page;
+			if (!mem_cgroup_move_account(page, true,
+						     mc.from, mc.to)) {
+				mc.precharge -= HPAGE_PMD_NR;
+				mc.moved_charge += HPAGE_PMD_NR;
+			}
+			put_page(page);
 		}
 		spin_unlock(ptl);
 		return 0;
@@ -4919,12 +4958,16 @@ static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
 	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
 	for (; addr != end; addr += PAGE_SIZE) {
 		pte_t ptent = *(pte++);
+		bool device = false;
 		swp_entry_t ent;
 
 		if (!mc.precharge)
 			break;
 
 		switch (get_mctgt_type(vma, addr, ptent, &target)) {
+		case MC_TARGET_DEVICE:
+			device = true;
+			/* fall through */
 		case MC_TARGET_PAGE:
 			page = target.page;
 			/*
@@ -4935,7 +4978,7 @@ static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
 			 */
 			if (PageTransCompound(page))
 				goto put;
-			if (isolate_lru_page(page))
+			if (!device && isolate_lru_page(page))
 				goto put;
 			if (!mem_cgroup_move_account(page, false,
 						mc.from, mc.to)) {
@@ -4943,7 +4986,8 @@ static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
 				/* we uncharge from mc.from later. */
 				mc.moved_charge++;
 			}
-			putback_lru_page(page);
+			if (!device)
+				putback_lru_page(page);
 put:			/* get_mctgt_type() gets the page */
 			put_page(page);
 			break;
#16 lore raw

Re: [HMM-v25 10/19] mm/memcontrol: support MEMORY_DEVICE_PRIVATE v4

Quoted 59 lines · Laurent Dufour
Quoted 45 lines · Jérôme Glisse

HMM pages (private or public device pages) are ZONE_DEVICE page and thus need special handling when it comes to lru or refcount. This patch make sure that memcontrol properly handle those when it face them. Those pages are use like regular pages in a process address space either as anonymous page or as file back page. So from memcg point of view we want to handle them like regular page for now at least.

Changed since v3:
- remove public support and move those chunk to separate patch
Changed since v2:
- s/host/public
Changed since v1:
- s/public/host
- add comments explaining how device memory behave and why

---
 kernel/memremap.c |  1 +
 mm/memcontrol.c   | 52 ++++++++++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 49 insertions(+), 4 deletions(-)
kernel/memremap.c +1 -0
index 398630c1fba3..f42d7483e886 100644
@@ -492,6 +492,7 @@ void put_zone_device_private_page(struct page *page)
 		__ClearPageWaiters(page);
 
 		page->mapping = NULL;
+		mem_cgroup_uncharge(page);
 
 		page->pgmap->page_free(page, page->pgmap->data);
 	} else if (!count)
mm/memcontrol.c +0 -0
index 604fb3ca8028..977d1cf3493a 100644
@@ -4407,12 +4407,13 @@ enum mc_target_type {
 	MC_TARGET_NONE = 0,

mm/memcontrol.c enum mc_target_type { · line 4416

Quoted 10 lines
mm/memcontrol.c +2 -1
 	MC_TARGET_PAGE,
 	MC_TARGET_SWAP,
+	MC_TARGET_DEVICE,
 };
 
 static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
 						unsigned long addr, pte_t ptent)
 {
-	struct page *page = vm_normal_page(vma, addr, ptent);
+	struct page *page = _vm_normal_page(vma, addr, ptent, true);

Hi Jérôme,

Quoted 2 lines

As _vm_normal_page() is defined later in the patch 18, so this patch should
break the bisectability.

Correct, it seems i miss that when re-org patch order. The vm_normal_page() changes can be move to patch 18 as it would be more logical to change call site with the patch that adds the new special function.

Dunno if patch can be edited now ?

Cheers,
Jérôme

#17 lore raw

PATCH 11/19 mm/hmm/devmem: device memory hotplug using ZONE_DEVICE v7

In reply to Jérôme Glisse

This introduce a simple struct and associated helpers for device driver to use when hotpluging un-addressable device memory as ZONE_DEVICE. It will find a unuse physical address range and trigger memory hotplug for it which allocates and initialize struct page for the device memory.

Device driver should use this helper during device initialization to hotplug the device memory. It should only need to remove the memory once the device is going offline (shutdown or hotremove). There should not be any userspace API to hotplug memory expect maybe for host device driver to allow to add more memory to a guest device driver.

Device's memory is manage by the device driver and HMM only provides helpers to that effect.

Changed since v6:
- fix start address calculation (Balbir Singh)
- more comments and updated commit message.
Changed since v5:
- kernel configuration simplification
- remove now unuse device driver helper
Changed since v4:
- enable device_private_key static key when adding device memory
Changed since v3:
- s/device unaddressable/device private/
Changed since v2:
- s/SECTION_SIZE/PA_SECTION_SIZE
Changed since v1:
- change to adapt to new add_pages() helper
- make this x86-64 only for now

---
 include/linux/hmm.h | 155 +++++++++++++++++++++
 mm/hmm.c            | 379 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 533 insertions(+), 1 deletion(-)
include/linux/hmm.h +155 -0
index 79f5d4fa2d4f..0a8e5e3688e8 100644
@@ -72,6 +72,11 @@
 
 #if IS_ENABLED(CONFIG_HMM)
 
+#include <linux/migrate.h>
+#include <linux/memremap.h>
+#include <linux/completion.h>
+
+
 struct hmm;
 
 /*
@@ -322,6 +327,156 @@ int hmm_vma_fault(struct vm_area_struct *vma,
 #endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
 
 
+#if IS_ENABLED(CONFIG_DEVICE_PRIVATE)
+struct hmm_devmem;
+
+struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma,
+				       unsigned long addr);
+
+/*
+ * struct hmm_devmem_ops - callback for ZONE_DEVICE memory events
+ *
+ * @free: call when refcount on page reach 1 and thus is no longer use
+ * @fault: call when there is a page fault to unaddressable memory
+ *
+ * Both callback happens from page_free() and page_fault() callback of struct
+ * dev_pagemap respectively. See include/linux/memremap.h for more details on
+ * those.
+ *
+ * The hmm_devmem_ops callback are just here to provide a coherent and
+ * uniq API to device driver and device driver should not register their
+ * own page_free() or page_fault() but rely on the hmm_devmem_ops call-
+ * back.
+ */
+struct hmm_devmem_ops {
+	/*
+	 * free() - free a device page
+	 * @devmem: device memory structure (see struct hmm_devmem)
+	 * @page: pointer to struct page being freed
+	 *
+	 * Call back occurs whenever a device page refcount reach 1 which
+	 * means that no one is holding any reference on the page anymore
+	 * (ZONE_DEVICE page have an elevated refcount of 1 as default so
+	 * that they are not release to the general page allocator).
+	 *
+	 * Note that callback has exclusive ownership of the page (as no
+	 * one is holding any reference).
+	 */
+	void (*free)(struct hmm_devmem *devmem, struct page *page);
+	/*
+	 * fault() - CPU page fault or get user page (GUP)
+	 * @devmem: device memory structure (see struct hmm_devmem)
+	 * @vma: virtual memory area containing the virtual address
+	 * @addr: virtual address that faulted or for which there is a GUP
+	 * @page: pointer to struct page backing virtual address (unreliable)
+	 * @flags: FAULT_FLAG_* (see include/linux/mm.h)
+	 * @pmdp: page middle directory
+	 * Returns: VM_FAULT_MINOR/MAJOR on success or one of VM_FAULT_ERROR
+	 *   on error
+	 *
+	 * The callback occurs whenever there is a CPU page fault or GUP on a
+	 * virtual address. This means that the device driver must migrate the
+	 * page back to regular memory (CPU accessible).
+	 *
+	 * The device driver is free to migrate more than one page from the
+	 * fault() callback as an optimization. However if device decide to
+	 * migrate more than one page it must always priotirize the faulting
+	 * address over the others.
+	 *
+	 * The struct page pointer is only given as an hint to allow quick
+	 * lookup of internal device driver data. A concurrent migration
+	 * might have already free that page and the virtual address might
+	 * not longer be back by it. So it should not be modified by the
+	 * callback.
+	 *
+	 * Note that mmap semaphore is held in read mode at least when this
+	 * callback occurs, hence the vma is valid upon callback entry.
+	 */
+	int (*fault)(struct hmm_devmem *devmem,
+		     struct vm_area_struct *vma,
+		     unsigned long addr,
+		     const struct page *page,
+		     unsigned int flags,
+		     pmd_t *pmdp);
+};
+
+/*
+ * struct hmm_devmem - track device memory
+ *
+ * @completion: completion object for device memory
+ * @pfn_first: first pfn for this resource (set by hmm_devmem_add())
+ * @pfn_last: last pfn for this resource (set by hmm_devmem_add())
+ * @resource: IO resource reserved for this chunk of memory
+ * @pagemap: device page map for that chunk
+ * @device: device to bind resource to
+ * @ops: memory operations callback
+ * @ref: per CPU refcount
+ *
+ * This an helper structure for device drivers that do not wish to implement
+ * the gory details related to hotplugging new memoy and allocating struct
+ * pages.
+ *
+ * Device drivers can directly use ZONE_DEVICE memory on their own if they
+ * wish to do so.
+ */
+struct hmm_devmem {
+	struct completion		completion;
+	unsigned long			pfn_first;
+	unsigned long			pfn_last;
+	struct resource			*resource;
+	struct device			*device;
+	struct dev_pagemap		pagemap;
+	const struct hmm_devmem_ops	*ops;
+	struct percpu_ref		ref;
+};
+
+/*
+ * To add (hotplug) device memory, HMM assumes that there is no real resource
+ * that reserves a range in the physical address space (this is intended to be
+ * use by unaddressable device memory). It will reserve a physical range big
+ * enough and allocate struct page for it.
+ *
+ * The device driver can wrap the hmm_devmem struct inside a private device
+ * driver struct. The device driver must call hmm_devmem_remove() before the
+ * device goes away and before freeing the hmm_devmem struct memory.
+ */
+struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
+				  struct device *device,
+				  unsigned long size);
+void hmm_devmem_remove(struct hmm_devmem *devmem);
+
+/*
+ * hmm_devmem_page_set_drvdata - set per-page driver data field
+ *
+ * @page: pointer to struct page
+ * @data: driver data value to set
+ *
+ * Because page can not be on lru we have an unsigned long that driver can use
+ * to store a per page field. This just a simple helper to do that.
+ */
+static inline void hmm_devmem_page_set_drvdata(struct page *page,
+					       unsigned long data)
+{
+	unsigned long *drvdata = (unsigned long *)&page->pgmap;
+
+	drvdata[1] = data;
+}
+
+/*
+ * hmm_devmem_page_get_drvdata - get per page driver data field
+ *
+ * @page: pointer to struct page
+ * Return: driver data value
+ */
+static inline unsigned long hmm_devmem_page_get_drvdata(struct page *page)
+{
+	unsigned long *drvdata = (unsigned long *)&page->pgmap;
+
+	return drvdata[1];
+}
+#endif /* IS_ENABLED(CONFIG_DEVICE_PRIVATE) */
+
+
 /* Below are for HMM internal use only! Not to be used by device driver! */
 void hmm_mm_destroy(struct mm_struct *mm);
 
mm/hmm.c +378 -1

398 lines not on this page · raw message

Signature · 7 lines
2.13.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#18 lore raw

PATCH 12/19 mm/hmm/devmem: dummy HMM device for ZONE_DEVICE memory v3

In reply to Jérôme Glisse

This introduce a dummy HMM device class so device driver can use it to create hmm_device for the sole purpose of registering device memory. It is useful to device driver that want to manage multiple physical device memory under same struct device umbrella.

Changed since v2:
- use device_initcall() and drop everything that is module specific
Changed since v1:
- Improve commit message
- Add drvdata parameter to set on struct device

---
 include/linux/hmm.h | 22 ++++++++++++++-
 mm/hmm.c            | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 102 insertions(+), 1 deletion(-)
include/linux/hmm.h +21 -1
index 0a8e5e3688e8..aeba696a4385 100644
@@ -72,11 +72,11 @@
 
 #if IS_ENABLED(CONFIG_HMM)
 
+#include <linux/device.h>
 #include <linux/migrate.h>
 #include <linux/memremap.h>
 #include <linux/completion.h>
 
-
 struct hmm;
 
 /*
@@ -474,6 +474,26 @@ static inline unsigned long hmm_devmem_page_get_drvdata(struct page *page)
 
 	return drvdata[1];
 }
+
+
+/*
+ * struct hmm_device - fake device to hang device memory onto
+ *
+ * @device: device struct
+ * @minor: device minor number
+ */
+struct hmm_device {
+	struct device		device;
+	unsigned int		minor;
+};
+
+/*
+ * A device driver that wants to handle multiple devices memory through a
+ * single fake device can use hmm_device to do so. This is purely a helper and
+ * it is not strictly needed, in order to make use of any HMM functionality.
+ */
+struct hmm_device *hmm_device_new(void *drvdata);
+void hmm_device_put(struct hmm_device *hmm_device);
 #endif /* IS_ENABLED(CONFIG_DEVICE_PRIVATE) */
 
 
mm/hmm.c +81 -0

91 lines not on this page · raw message

Signature · 7 lines
2.13.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#19 lore raw

PATCH 13/19 mm/migrate: new migrate mode MIGRATE_SYNC_NO_COPY

In reply to Jérôme Glisse

Introduce a new migration mode that allow to offload the copy to a device DMA engine. This changes the workflow of migration and not all address_space migratepage callback can support this. So it needs to be tested in those cases.

This is intended to be use by migrate_vma() which itself is use for thing like HMM (see include/linux/hmm.h).

---
 fs/aio.c                     |  8 +++++++
 fs/f2fs/data.c               |  5 ++++-
 fs/hugetlbfs/inode.c         |  5 ++++-
 fs/ubifs/file.c              |  5 ++++-
 include/linux/migrate.h      |  5 +++++
 include/linux/migrate_mode.h |  5 +++++
 mm/balloon_compaction.c      |  8 +++++++
 mm/migrate.c                 | 52 ++++++++++++++++++++++++++++++++++----------
 mm/zsmalloc.c                |  8 +++++++
 9 files changed, 86 insertions(+), 15 deletions(-)
fs/aio.c +8 -0
index 8f0127526299..b5d69f28d8b1 100644
@@ -373,6 +373,14 @@ static int aio_migratepage(struct address_space *mapping, struct page *new,
 	pgoff_t idx;
 	int rc;
 
+	/*
+	 * We cannot support the _NO_COPY case here, because copy needs to
+	 * happen under the ctx->completion_lock. That does not work with the
+	 * migration workflow of MIGRATE_SYNC_NO_COPY.
+	 */
+	if (mode == MIGRATE_SYNC_NO_COPY)
+		return -EINVAL;
+
 	rc = 0;
 
 	/* mapping->private_lock here protects against the kioctx teardown.  */
fs/f2fs/data.c +4 -1
index aefc2a5745d3..cf7930daa10e 100644
@@ -2257,7 +2257,10 @@ int f2fs_migrate_page(struct address_space *mapping,
 		SetPagePrivate(newpage);
 	set_page_private(newpage, page_private(page));
 
-	migrate_page_copy(newpage, page);
+	if (mode != MIGRATE_SYNC_NO_COPY)
+		migrate_page_copy(newpage, page);
+	else
+		migrate_page_states(newpage, page);
 
 	return MIGRATEPAGE_SUCCESS;
 }
fs/hugetlbfs/inode.c +4 -1
index 33961b35007b..59073e9f01a4 100644
@@ -830,7 +830,10 @@ static int hugetlbfs_migrate_page(struct address_space *mapping,
 	rc = migrate_huge_page_move_mapping(mapping, newpage, page);
 	if (rc != MIGRATEPAGE_SUCCESS)
 		return rc;
-	migrate_page_copy(newpage, page);
+	if (mode != MIGRATE_SYNC_NO_COPY)
+		migrate_page_copy(newpage, page);
+	else
+		migrate_page_states(newpage, page);
 
 	return MIGRATEPAGE_SUCCESS;
 }
fs/ubifs/file.c +4 -1
index f90a466ea5db..a02aa59d1e24 100644
@@ -1490,7 +1490,10 @@ static int ubifs_migrate_page(struct address_space *mapping,
 		SetPagePrivate(newpage);
 	}
 
-	migrate_page_copy(newpage, page);
+	if (mode != MIGRATE_SYNC_NO_COPY)
+		migrate_page_copy(newpage, page);
+	else
+		migrate_page_states(newpage, page);
 	return MIGRATEPAGE_SUCCESS;
 }
 #endif
include/linux/migrate.h +5 -0
index ce15989521a1..7db4c812a2a6 100644
@@ -72,6 +72,7 @@ extern void putback_movable_page(struct page *page);
 
 extern int migrate_prep(void);
 extern int migrate_prep_local(void);
+extern void migrate_page_states(struct page *newpage, struct page *page);
 extern void migrate_page_copy(struct page *newpage, struct page *page);
 extern int migrate_huge_page_move_mapping(struct address_space *mapping,
 				  struct page *newpage, struct page *page);
@@ -92,6 +93,10 @@ static inline int isolate_movable_page(struct page *page, isolate_mode_t mode)
 static inline int migrate_prep(void) { return -ENOSYS; }
 static inline int migrate_prep_local(void) { return -ENOSYS; }
 
+static inline void migrate_page_states(struct page *newpage, struct page *page)
+{
+}
+
 static inline void migrate_page_copy(struct page *newpage,
 				     struct page *page) {}
 
include/linux/migrate_mode.h +5 -0
index ebf3d89a3919..bdf66af9b937 100644
@@ -6,11 +6,16 @@
  *	on most operations but not ->writepage as the potential stall time
  *	is too significant
  * MIGRATE_SYNC will block when migrating pages
+ * MIGRATE_SYNC_NO_COPY will block when migrating pages but will not copy pages
+ *	with the CPU. Instead, page copy happens outside the migratepage()
+ *	callback and is likely using a DMA engine. See migrate_vma() and HMM
+ *	(mm/hmm.c) for users of this mode.
  */
 enum migrate_mode {
 	MIGRATE_ASYNC,
 	MIGRATE_SYNC_LIGHT,
 	MIGRATE_SYNC,
+	MIGRATE_SYNC_NO_COPY,
 };
 
 #endif		/* MIGRATE_MODE_H_INCLUDED */
mm/balloon_compaction.c +8 -0
index 9075aa54e955..e78ae1255cb3 100644
@@ -139,6 +139,14 @@ int balloon_page_migrate(struct address_space *mapping,
 {
 	struct balloon_dev_info *balloon = balloon_page_device(page);
 
+	/*
+	 * We can not easily support the no copy case here so ignore it as it
+	 * is unlikely to be use with ballon pages. See include/linux/hmm.h for
+	 * user of the MIGRATE_SYNC_NO_COPY mode.
+	 */
+	if (mode == MIGRATE_SYNC_NO_COPY)
+		return -EINVAL;
+
 	VM_BUG_ON_PAGE(!PageLocked(page), page);
 	VM_BUG_ON_PAGE(!PageLocked(newpage), newpage);
 
mm/migrate.c +40 -12

103 lines not on this page · raw message

mm/zsmalloc.c +8 -0
index 62457eb82330..5ad75ec4151c 100644
@@ -1969,6 +1969,14 @@ int zs_page_migrate(struct address_space *mapping, struct page *newpage,
 	unsigned int obj_idx;
 	int ret = -EAGAIN;
 
+	/*
+	 * We cannot support the _NO_COPY case here, because copy needs to
+	 * happen under the zs lock, which does not work with
+	 * MIGRATE_SYNC_NO_COPY workflow.
+	 */
+	if (mode == MIGRATE_SYNC_NO_COPY)
+		return -EINVAL;
+
 	VM_BUG_ON_PAGE(!PageMovable(page), page);
 	VM_BUG_ON_PAGE(!PageIsolated(page), page);
 
Signature · 7 lines
2.13.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
maintainer
#20 lore raw

Re: [HMM-v25 13/19] mm/migrate: new migrate mode MIGRATE_SYNC_NO_COPY

Quoted 4 lines · On Wed, 16 Aug 2017 20:05:42 -0400 J__r__me Glisse <[email protected]> wrote:

Introduce a new migration mode that allow to offload the copy to a device DMA engine. This changes the workflow of migration and not all address_space migratepage callback can support this. So it needs to be tested in those cases.

Can you please expand on this? What additional testing must be performed before we are able to merge this into mainline?

#21 lore raw

Re: [HMM-v25 13/19] mm/migrate: new migrate mode MIGRATE_SYNC_NO_COPY

Quoted 10 lines · Andrew Morton
Quoted 4 lines · On Wed, 16 Aug 2017 20:05:42 -0400 J__r__me Glisse <[email protected]> wrote:

Introduce a new migration mode that allow to offload the copy to a device DMA engine. This changes the workflow of migration and not all address_space migratepage callback can support this. So it needs to be tested in those cases.

Can you please expand on this? What additional testing must be performed before we are able to merge this into mainline?

No additional testing needed. I disable MIGRATE_SYNC_NO_COPY in all problematic migratepage() callback and i added comment in those to explain why (part of this patch). The commit message is unclear it should say that any callback that wish to support this new mode need to be aware of the difference in the migration flow from other mode.

Some of this callback do extra locking while copying (aio, zsmalloc, balloon, ...) and for DMA to be effective you want to copy multiple pages in one DMA operations. But in the problematic case you can not easily hold the extra lock accross multiple call to this callback.

Usual flow is:

For each page {
1 - lock page
2 - call migratepage() callback
3 - (extra locking in some migratepage() callback)
4 - migrate page state (freeze refcount, update page cache, buffer

     head, ...)

5 - copy page
6 - (unlock any extra lock of migratepage() callback) 7 - return from migratepage() callback
8 - unlock page
}

The new mode MIGRATE_SYNC_NO_COPY:
1 - lock multiple pages
For each page {
2 - call migratepage() callback
3 - abort in all problematic migratepage() callback
4 - migrate page state (freeze refcount, update page cache, buffer

     head, ...)
} // finished all calls to migratepage() callback

5 - DMA copy multiple pages
6 - unlock all the pages

To support MIGRATE_SYNC_NO_COPY in the problematic case we would need a new callback migratepages() (for instance) that deals with multiple pages in one transaction.

Because the problematic cases are not important for current usage i did not wanted to complexify this patchset even more for no good reasons.

I hope this clarify, the commit message and comment in migrate.h can probably use this extra description i just gave.

Cheers,
Jerome

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#22 lore raw

PATCH 14/19 mm/migrate: new memory migration helper for use with device memory v5

In reply to Jérôme Glisse

This patch add a new memory migration helpers, which migrate memory backing a range of virtual address of a process to different memory (which can be allocated through special allocator). It differs from numa migration by working on a range of virtual address and thus by doing migration in chunk that can be large enough to use DMA engine or special copy offloading engine.

Expected users are any one with heterogeneous memory where different memory have different characteristics (latency, bandwidth, ...). As an example IBM platform with CAPI bus can make use of this feature to migrate between regular memory and CAPI device memory. New CPU architecture with a pool of high performance memory not manage as cache but presented as regular memory (while being faster and with lower latency than DDR) will also be prime user of this patch.

Migration to private device memory will be useful for device that have large pool of such like GPU, NVidia plans to use HMM for that.

Changed since v4:
- split THP instead of skipping them
Changes since v3:
- Rebase
Changes since v2:
- droped HMM prefix and HMM specific code
Changes since v1:
- typos fix
- split early unmap optimization for page with single mapping

---
 include/linux/migrate.h | 104 ++++++++++
 mm/migrate.c            | 492 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 596 insertions(+)
include/linux/migrate.h +104 -0
index 7db4c812a2a6..8f73cebfc3f5 100644
@@ -156,4 +156,108 @@ static inline int migrate_misplaced_transhuge_page(struct mm_struct *mm,
 }
 #endif /* CONFIG_NUMA_BALANCING && CONFIG_TRANSPARENT_HUGEPAGE*/
 
+
+#ifdef CONFIG_MIGRATION
+
+#define MIGRATE_PFN_VALID	(1UL << 0)
+#define MIGRATE_PFN_MIGRATE	(1UL << 1)
+#define MIGRATE_PFN_LOCKED	(1UL << 2)
+#define MIGRATE_PFN_WRITE	(1UL << 3)
+#define MIGRATE_PFN_ERROR	(1UL << 4)
+#define MIGRATE_PFN_SHIFT	5
+
+static inline struct page *migrate_pfn_to_page(unsigned long mpfn)
+{
+	if (!(mpfn & MIGRATE_PFN_VALID))
+		return NULL;
+	return pfn_to_page(mpfn >> MIGRATE_PFN_SHIFT);
+}
+
+static inline unsigned long migrate_pfn(unsigned long pfn)
+{
+	return (pfn << MIGRATE_PFN_SHIFT) | MIGRATE_PFN_VALID;
+}
+
+/*
+ * struct migrate_vma_ops - migrate operation callback
+ *
+ * @alloc_and_copy: alloc destination memory and copy source memory to it
+ * @finalize_and_map: allow caller to map the successfully migrated pages
+ *
+ *
+ * The alloc_and_copy() callback happens once all source pages have been locked,
+ * unmapped and checked (checked whether pinned or not). All pages that can be
+ * migrated will have an entry in the src array set with the pfn value of the
+ * page and with the MIGRATE_PFN_VALID and MIGRATE_PFN_MIGRATE flag set (other
+ * flags might be set but should be ignored by the callback).
+ *
+ * The alloc_and_copy() callback can then allocate destination memory and copy
+ * source memory to it for all those entries (ie with MIGRATE_PFN_VALID and
+ * MIGRATE_PFN_MIGRATE flag set). Once these are allocated and copied, the
+ * callback must update each corresponding entry in the dst array with the pfn
+ * value of the destination page and with the MIGRATE_PFN_VALID and
+ * MIGRATE_PFN_LOCKED flags set (destination pages must have their struct pages
+ * locked, via lock_page()).
+ *
+ * At this point the alloc_and_copy() callback is done and returns.
+ *
+ * Note that the callback does not have to migrate all the pages that are
+ * marked with MIGRATE_PFN_MIGRATE flag in src array unless this is a migration
+ * from device memory to system memory (ie the MIGRATE_PFN_DEVICE flag is also
+ * set in the src array entry). If the device driver cannot migrate a device
+ * page back to system memory, then it must set the corresponding dst array
+ * entry to MIGRATE_PFN_ERROR. This will trigger a SIGBUS if CPU tries to
+ * access any of the virtual addresses originally backed by this page. Because
+ * a SIGBUS is such a severe result for the userspace process, the device
+ * driver should avoid setting MIGRATE_PFN_ERROR unless it is really in an
+ * unrecoverable state.
+ *
+ * THE alloc_and_copy() CALLBACK MUST NOT CHANGE ANY OF THE SRC ARRAY ENTRIES
+ * OR BAD THINGS WILL HAPPEN !
+ *
+ *
+ * The finalize_and_map() callback happens after struct page migration from
+ * source to destination (destination struct pages are the struct pages for the
+ * memory allocated by the alloc_and_copy() callback).  Migration can fail, and
+ * thus the finalize_and_map() allows the driver to inspect which pages were
+ * successfully migrated, and which were not. Successfully migrated pages will
+ * have the MIGRATE_PFN_MIGRATE flag set for their src array entry.
+ *
+ * It is safe to update device page table from within the finalize_and_map()
+ * callback because both destination and source page are still locked, and the
+ * mmap_sem is held in read mode (hence no one can unmap the range being
+ * migrated).
+ *
+ * Once callback is done cleaning up things and updating its page table (if it
+ * chose to do so, this is not an obligation) then it returns. At this point,
+ * the HMM core will finish up the final steps, and the migration is complete.
+ *
+ * THE finalize_and_map() CALLBACK MUST NOT CHANGE ANY OF THE SRC OR DST ARRAY
+ * ENTRIES OR BAD THINGS WILL HAPPEN !
+ */
+struct migrate_vma_ops {
+	void (*alloc_and_copy)(struct vm_area_struct *vma,
+			       const unsigned long *src,
+			       unsigned long *dst,
+			       unsigned long start,
+			       unsigned long end,
+			       void *private);
+	void (*finalize_and_map)(struct vm_area_struct *vma,
+				 const unsigned long *src,
+				 const unsigned long *dst,
+				 unsigned long start,
+				 unsigned long end,
+				 void *private);
+};
+
+int migrate_vma(const struct migrate_vma_ops *ops,
+		struct vm_area_struct *vma,
+		unsigned long start,
+		unsigned long end,
+		unsigned long *src,
+		unsigned long *dst,
+		void *private);
+
+#endif /* CONFIG_MIGRATION */
+
 #endif /* _LINUX_MIGRATE_H */
mm/migrate.c +492 -0

501 lines not on this page · raw message

Signature · 7 lines
2.13.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#23 lore raw

PATCH 15/19 mm/migrate: migrate_vma() unmap page from vma while collecting pages

In reply to Jérôme Glisse

Common case for migration of virtual address range is page are map only once inside the vma in which migration is taking place. Because we already walk the CPU page table for that range we can directly do the unmap there and setup special migration swap entry.

---
 mm/migrate.c | 141 +++++++++++++++++++++++++++++++++++++++++++++++------------
 1 file changed, 112 insertions(+), 29 deletions(-)
mm/migrate.c +112 -29
index 60e2f8369cd7..57d1fa7a8e62 100644
@@ -2160,7 +2160,7 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
 	struct migrate_vma *migrate = walk->private;
 	struct vm_area_struct *vma = walk->vma;
 	struct mm_struct *mm = vma->vm_mm;
-	unsigned long addr = start;
+	unsigned long addr = start, unmapped = 0;
 	spinlock_t *ptl;
 	pte_t *ptep;
 
@@ -2205,9 +2205,12 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
 		return migrate_vma_collect_hole(start, end, walk);
 
 	ptep = pte_offset_map_lock(mm, pmdp, addr, &ptl);
+	arch_enter_lazy_mmu_mode();
+
 	for (; addr < end; addr += PAGE_SIZE, ptep++) {
 		unsigned long mpfn, pfn;
 		struct page *page;
+		swp_entry_t entry;
 		pte_t pte;
 
 		pte = *ptep;
@@ -2239,11 +2242,44 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,
 		mpfn = migrate_pfn(pfn) | MIGRATE_PFN_MIGRATE;
 		mpfn |= pte_write(pte) ? MIGRATE_PFN_WRITE : 0;
 
+		/*
+		 * Optimize for the common case where page is only mapped once
+		 * in one process. If we can lock the page, then we can safely
+		 * set up a special migration page table entry now.
+		 */
+		if (trylock_page(page)) {
+			pte_t swp_pte;
+
+			mpfn |= MIGRATE_PFN_LOCKED;
+			ptep_get_and_clear(mm, addr, ptep);
+
+			/* Setup special migration page table entry */
+			entry = make_migration_entry(page, pte_write(pte));
+			swp_pte = swp_entry_to_pte(entry);
+			if (pte_soft_dirty(pte))
+				swp_pte = pte_swp_mksoft_dirty(swp_pte);
+			set_pte_at(mm, addr, ptep, swp_pte);
+
+			/*
+			 * This is like regular unmap: we remove the rmap and
+			 * drop page refcount. Page won't be freed, as we took
+			 * a reference just above.
+			 */
+			page_remove_rmap(page, false);
+			put_page(page);
+			unmapped++;
+		}
+
 next:
 		migrate->src[migrate->npages++] = mpfn;
 	}
+	arch_leave_lazy_mmu_mode();
 	pte_unmap_unlock(ptep - 1, ptl);
 
+	/* Only flush the TLB if we actually modified any entries */
+	if (unmapped)
+		flush_tlb_range(walk->vma, start, end);
+
 	return 0;
 }
 
@@ -2268,7 +2304,13 @@ static void migrate_vma_collect(struct migrate_vma *migrate)
 	mm_walk.mm = migrate->vma->vm_mm;
 	mm_walk.private = migrate;
 
+	mmu_notifier_invalidate_range_start(mm_walk.mm,
+					    migrate->start,
+					    migrate->end);
 	walk_page_range(migrate->start, migrate->end, &mm_walk);
+	mmu_notifier_invalidate_range_end(mm_walk.mm,
+					  migrate->start,
+					  migrate->end);
 
 	migrate->end = migrate->start + (migrate->npages << PAGE_SHIFT);
 }
@@ -2316,32 +2358,37 @@ static bool migrate_vma_check_page(struct page *page)
 static void migrate_vma_prepare(struct migrate_vma *migrate)
 {
 	const unsigned long npages = migrate->npages;
+	const unsigned long start = migrate->start;
+	unsigned long addr, i, restore = 0;
 	bool allow_drain = true;
-	unsigned long i;
 
 	lru_add_drain();
 
 	for (i = 0; (i < npages) && migrate->cpages; i++) {
 		struct page *page = migrate_pfn_to_page(migrate->src[i]);
+		bool remap = true;
 
 		if (!page)
 			continue;
 
-		/*
-		 * Because we are migrating several pages there can be
-		 * a deadlock between 2 concurrent migration where each
-		 * are waiting on each other page lock.
-		 *
-		 * Make migrate_vma() a best effort thing and backoff
-		 * for any page we can not lock right away.
-		 */
-		if (!trylock_page(page)) {
-			migrate->src[i] = 0;
-			migrate->cpages--;
-			put_page(page);
-			continue;
+		if (!(migrate->src[i] & MIGRATE_PFN_LOCKED)) {
+			/*
+			 * Because we are migrating several pages there can be
+			 * a deadlock between 2 concurrent migration where each
+			 * are waiting on each other page lock.
+			 *
+			 * Make migrate_vma() a best effort thing and backoff
+			 * for any page we can not lock right away.
+			 */
+			if (!trylock_page(page)) {
+				migrate->src[i] = 0;
+				migrate->cpages--;
+				put_page(page);
+				continue;
+			}
+			remap = false;
+			migrate->src[i] |= MIGRATE_PFN_LOCKED;
 		}
-		migrate->src[i] |= MIGRATE_PFN_LOCKED;
 
 		if (!PageLRU(page) && allow_drain) {
 			/* Drain CPU's pagevec */
@@ -2350,21 +2397,50 @@ static void migrate_vma_prepare(struct migrate_vma *migrate)
 		}
 
 		if (isolate_lru_page(page)) {
-			migrate->src[i] = 0;
-			unlock_page(page);
-			migrate->cpages--;
-			put_page(page);
+			if (remap) {
+				migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
+				migrate->cpages--;
+				restore++;
+			} else {
+				migrate->src[i] = 0;
+				unlock_page(page);
+				migrate->cpages--;
+				put_page(page);
+			}
 			continue;
 		}
 
 		if (!migrate_vma_check_page(page)) {
-			migrate->src[i] = 0;
-			unlock_page(page);
-			migrate->cpages--;
+			if (remap) {
+				migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
+				migrate->cpages--;
+				restore++;
 
-			putback_lru_page(page);
+				get_page(page);
+				putback_lru_page(page);
+			} else {
+				migrate->src[i] = 0;
+				unlock_page(page);
+				migrate->cpages--;
+
+				putback_lru_page(page);
+			}
 		}
 	}
+
+	for (i = 0, addr = start; i < npages && restore; i++, addr += PAGE_SIZE) {
+		struct page *page = migrate_pfn_to_page(migrate->src[i]);
+
+		if (!page || (migrate->src[i] & MIGRATE_PFN_MIGRATE))
+			continue;
+
+		remove_migration_pte(page, migrate->vma, addr, page);
+
+		migrate->src[i] = 0;
+		unlock_page(page);
+		put_page(page);
+		restore--;
+	}
 }
 
 /*
@@ -2391,12 +2467,19 @@ static void migrate_vma_unmap(struct migrate_vma *migrate)
 		if (!page || !(migrate->src[i] & MIGRATE_PFN_MIGRATE))
 			continue;
 
-		try_to_unmap(page, flags);
-		if (page_mapped(page) || !migrate_vma_check_page(page)) {
-			migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
-			migrate->cpages--;
-			restore++;
+		if (page_mapped(page)) {
+			try_to_unmap(page, flags);
+			if (page_mapped(page))
+				goto restore;
 		}
+
+		if (migrate_vma_check_page(page))
+			continue;
+
+restore:
+		migrate->src[i] &= ~MIGRATE_PFN_MIGRATE;
+		migrate->cpages--;
+		restore++;
 	}
 
 	for (addr = start, i = 0; i < npages && restore; addr += PAGE_SIZE, i++) {
Signature · 7 lines
2.13.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#24 lore raw

PATCH 16/19 mm/migrate: support un-addressable ZONE_DEVICE page in migration v3

In reply to Jérôme Glisse

Allow to unmap and restore special swap entry of un-addressable ZONE_DEVICE memory.

Changed since v2:
- un-conditionaly allow device private memory to be migrated (it can not be pin to pointless to check reference count).
Changed since v1:
- s/device unaddressable/device private/

Cc Kirill A. Shutemov <[email protected]>
---
 include/linux/migrate.h |  10 +++-
 mm/migrate.c            | 149 +++++++++++++++++++++++++++++++++++++++---------
 mm/page_vma_mapped.c    |  10 ++++
 mm/rmap.c               |  25 ++++++++
 4 files changed, 164 insertions(+), 30 deletions(-)
include/linux/migrate.h +8 -2
index 8f73cebfc3f5..8dc8f0a3f1af 100644
@@ -159,12 +159,18 @@ static inline int migrate_misplaced_transhuge_page(struct mm_struct *mm,
 
 #ifdef CONFIG_MIGRATION
 
+/*
+ * Watch out for PAE architecture, which has an unsigned long, and might not
+ * have enough bits to store all physical address and flags. So far we have
+ * enough room for all our flags.
+ */
 #define MIGRATE_PFN_VALID	(1UL << 0)
 #define MIGRATE_PFN_MIGRATE	(1UL << 1)
 #define MIGRATE_PFN_LOCKED	(1UL << 2)
 #define MIGRATE_PFN_WRITE	(1UL << 3)
-#define MIGRATE_PFN_ERROR	(1UL << 4)
-#define MIGRATE_PFN_SHIFT	5
+#define MIGRATE_PFN_DEVICE	(1UL << 4)
+#define MIGRATE_PFN_ERROR	(1UL << 5)
+#define MIGRATE_PFN_SHIFT	6
 
 static inline struct page *migrate_pfn_to_page(unsigned long mpfn)
 {
mm/migrate.c +121 -28

237 lines not on this page · raw message

mm/page_vma_mapped.c +10 -0
index 3bd3008db4cb..6a03946469a9 100644
@@ -48,6 +48,7 @@ static bool check_pte(struct page_vma_mapped_walk *pvmw)
 		if (!is_swap_pte(*pvmw->pte))
 			return false;
 		entry = pte_to_swp_entry(*pvmw->pte);
+
 		if (!is_migration_entry(entry))
 			return false;
 		if (migration_entry_to_page(entry) - pvmw->page >=
@@ -60,6 +61,15 @@ static bool check_pte(struct page_vma_mapped_walk *pvmw)
 		WARN_ON_ONCE(1);
 #endif
 	} else {
+		if (is_swap_pte(*pvmw->pte)) {
+			swp_entry_t entry;
+
+			entry = pte_to_swp_entry(*pvmw->pte);
+			if (is_device_private_entry(entry) &&
+			    device_private_entry_to_page(entry) == pvmw->page)
+				return true;
+		}
+
 		if (!pte_present(*pvmw->pte))
 			return false;
 
mm/rmap.c +25 -0
index 2c55e7b8f8f4..60e47a96cdbd 100644
@@ -63,6 +63,7 @@
 #include <linux/hugetlb.h>
 #include <linux/backing-dev.h>
 #include <linux/page_idle.h>
+#include <linux/memremap.h>
 
 #include <asm/tlbflush.h>
 
@@ -1330,6 +1331,10 @@ static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
 	if ((flags & TTU_MUNLOCK) && !(vma->vm_flags & VM_LOCKED))
 		return true;
 
+	if (IS_ENABLED(CONFIG_MIGRATION) && (flags & TTU_MIGRATION) &&
+	    is_zone_device_page(page) && !is_device_private_page(page))
+		return true;
+
 	if (flags & TTU_SPLIT_HUGE_PMD) {
 		split_huge_pmd_address(vma, address,
 				flags & TTU_SPLIT_FREEZE, page);
@@ -1378,6 +1383,26 @@ static bool try_to_unmap_one(struct page *page, struct vm_area_struct *vma,
 		subpage = page - page_to_pfn(page) + pte_pfn(*pvmw.pte);
 		address = pvmw.address;
 
+		if (IS_ENABLED(CONFIG_MIGRATION) &&
+		    (flags & TTU_MIGRATION) &&
+		    is_zone_device_page(page)) {
+			swp_entry_t entry;
+			pte_t swp_pte;
+
+			pteval = ptep_get_and_clear(mm, address, pvmw.pte);
+
+			/*
+			 * Store the pfn of the page in a special migration
+			 * pte. do_swap_page() will wait until the migration
+			 * pte is removed and then restart fault handling.
+			 */
+			entry = make_migration_entry(page, 0);
+			swp_pte = swp_entry_to_pte(entry);
+			if (pte_soft_dirty(pteval))
+				swp_pte = pte_swp_mksoft_dirty(swp_pte);
+			set_pte_at(mm, address, pvmw.pte, swp_pte);
+			goto discard;
+		}
 
 		if (!(flags & TTU_IGNORE_ACCESS)) {
 			if (ptep_clear_flush_young_notify(vma, address,
Signature · 7 lines
2.13.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#25 lore raw

PATCH 17/19 mm/migrate: allow migrate_vma() to alloc new page on empty entry v4

In reply to Jérôme Glisse

This allow caller of migrate_vma() to allocate new page for empty CPU page table entry (pte_none or back by zero page). This is only for anonymous memory and it won't allow new page to be instanced if the userfaultfd is armed.

This is useful to device driver that want to migrate a range of virtual address and would rather allocate new memory than having to fault later on.

Changed since v3:
- support zero pfn entry
- improve commit message
Changed sinve v2:
- differentiate between empty CPU page table entry and non empty
- improve code comments explaining how this works
Changed since v1:
- 5 level page table fix

---
 include/linux/migrate.h |   9 +++
 mm/migrate.c            | 205 +++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 205 insertions(+), 9 deletions(-)
include/linux/migrate.h +9 -0
index 8dc8f0a3f1af..d4e6d12a0b40 100644
@@ -218,6 +218,15 @@ static inline unsigned long migrate_pfn(unsigned long pfn)
  * driver should avoid setting MIGRATE_PFN_ERROR unless it is really in an
  * unrecoverable state.
  *
+ * For empty entry inside CPU page table (pte_none() or pmd_none() is true) we
+ * do set MIGRATE_PFN_MIGRATE flag inside the corresponding source array thus
+ * allowing device driver to allocate device memory for those unback virtual
+ * address. For this the device driver simply have to allocate device memory
+ * and properly set the destination entry like for regular migration. Note that
+ * this can still fails and thus inside the device driver must check if the
+ * migration was successful for those entry inside the finalize_and_map()
+ * callback just like for regular migration.
+ *
  * THE alloc_and_copy() CALLBACK MUST NOT CHANGE ANY OF THE SRC ARRAY ENTRIES
  * OR BAD THINGS WILL HAPPEN !
  *
mm/migrate.c +196 -9

284 lines not on this page · raw message

Signature · 7 lines
2.13.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#26 lore raw

PATCH 18/19 mm/device-public-memory: device memory cache coherent with CPU v5

In reply to Jérôme Glisse

Platform with advance system bus (like CAPI or CCIX) allow device memory to be accessible from CPU in a cache coherent fashion. Add a new type of ZONE_DEVICE to represent such memory. The use case are the same as for the un-addressable device memory but without all the corners cases.

Changed since v4:
- added memory cgroup change to this patch
Changed since v3:
- s/public/public (going back)
Changed since v2:
- s/public/public
- add proper include in migrate.c and drop useless #if/#endif
Changed since v1:
- Kconfig and #if/#else cleanup

Cc Aneesh Kumar <[email protected]>
Cc Paul E. McKenney <[email protected]>
Cc Benjamin Herrenschmidt <[email protected]>
Cc Dan Williams <[email protected]>
Cc Ross Zwisler <[email protected]>
---
 fs/proc/task_mmu.c       |  2 +-
 include/linux/hmm.h      |  4 ++--
 include/linux/ioport.h   |  1 +
 include/linux/memremap.h | 21 ++++++++++++++++++
 include/linux/mm.h       | 20 ++++++++++-------
 kernel/memremap.c        |  8 +++----
 mm/Kconfig               | 11 ++++++++++
 mm/gup.c                 |  7 ++++++
 mm/hmm.c                 |  4 ++--
 mm/madvise.c             |  2 +-
 mm/memcontrol.c          | 12 +++++-----
 mm/memory.c              | 46 +++++++++++++++++++++++++++++++++-----
 mm/migrate.c             | 57 ++++++++++++++++++++++++++++++++----------------
 mm/swap.c                | 11 ++++++++++
 14 files changed, 159 insertions(+), 47 deletions(-)
fs/proc/task_mmu.c +1 -1
index e3ba93b35925..d3a57b43f9f1 100644
@@ -1189,7 +1189,7 @@ static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
 		if (pm->show_pfn)
 			frame = pte_pfn(pte);
 		flags |= PM_PRESENT;
-		page = vm_normal_page(vma, addr, pte);
+		page = _vm_normal_page(vma, addr, pte, true);
 		if (pte_soft_dirty(pte))
 			flags |= PM_SOFT_DIRTY;
 	} else if (is_swap_pte(pte)) {
include/linux/hmm.h +2 -2
index aeba696a4385..79e63178fd87 100644
@@ -327,7 +327,7 @@ int hmm_vma_fault(struct vm_area_struct *vma,
 #endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
 
 
-#if IS_ENABLED(CONFIG_DEVICE_PRIVATE)
+#if IS_ENABLED(CONFIG_DEVICE_PRIVATE) ||  IS_ENABLED(CONFIG_DEVICE_PUBLIC)
 struct hmm_devmem;
 
 struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma,
@@ -494,7 +494,7 @@ struct hmm_device {
  */
 struct hmm_device *hmm_device_new(void *drvdata);
 void hmm_device_put(struct hmm_device *hmm_device);
-#endif /* IS_ENABLED(CONFIG_DEVICE_PRIVATE) */
+#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */
 
 
 /* Below are for HMM internal use only! Not to be used by device driver! */
include/linux/ioport.h +1 -0
index 3a4f69137bc2..f5cf32e80041 100644
@@ -131,6 +131,7 @@ enum {
 	IORES_DESC_PERSISTENT_MEMORY		= 4,
 	IORES_DESC_PERSISTENT_MEMORY_LEGACY	= 5,
 	IORES_DESC_DEVICE_PRIVATE_MEMORY	= 6,
+	IORES_DESC_DEVICE_PUBLIC_MEMORY		= 7,
 };
 
 /* helpers to define resources */
include/linux/memremap.h +21 -0
index 8aa6b82679e2..f8ee1c73ad2d 100644
@@ -57,10 +57,18 @@ static inline struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start)
  *
  * A more complete discussion of unaddressable memory may be found in
  * include/linux/hmm.h and Documentation/vm/hmm.txt.
+ *
+ * MEMORY_DEVICE_PUBLIC:
+ * Device memory that is cache coherent from device and CPU point of view. This
+ * is use on platform that have an advance system bus (like CAPI or CCIX). A
+ * driver can hotplug the device memory using ZONE_DEVICE and with that memory
+ * type. Any page of a process can be migrated to such memory. However no one
+ * should be allow to pin such memory so that it can always be evicted.
  */
 enum memory_type {
 	MEMORY_DEVICE_HOST = 0,
 	MEMORY_DEVICE_PRIVATE,
+	MEMORY_DEVICE_PUBLIC,
 };
 
 /*
@@ -92,6 +100,8 @@ enum memory_type {
  * The page_free() callback is called once the page refcount reaches 1
  * (ZONE_DEVICE pages never reach 0 refcount unless there is a refcount bug.
  * This allows the device driver to implement its own memory management.)
+ *
+ * For MEMORY_DEVICE_PUBLIC only the page_free() callback matter.
  */
 typedef int (*dev_page_fault_t)(struct vm_area_struct *vma,
 				unsigned long addr,
@@ -134,6 +144,12 @@ static inline bool is_device_private_page(const struct page *page)
 	return is_zone_device_page(page) &&
 		page->pgmap->type == MEMORY_DEVICE_PRIVATE;
 }
+
+static inline bool is_device_public_page(const struct page *page)
+{
+	return is_zone_device_page(page) &&
+		page->pgmap->type == MEMORY_DEVICE_PUBLIC;
+}
 #else
 static inline void *devm_memremap_pages(struct device *dev,
 		struct resource *res, struct percpu_ref *ref,
@@ -157,6 +173,11 @@ static inline bool is_device_private_page(const struct page *page)
 {
 	return false;
 }
+
+static inline bool is_device_public_page(const struct page *page)
+{
+	return false;
+}
 #endif
 
 /**
include/linux/mm.h +12 -8
index 515d4ae611b2..d1d161efefa0 100644
@@ -796,15 +796,16 @@ static inline bool is_zone_device_page(const struct page *page)
 }
 #endif
 
-#ifdef CONFIG_DEVICE_PRIVATE
-void put_zone_device_private_page(struct page *page);
+#if IS_ENABLED(CONFIG_DEVICE_PRIVATE) ||  IS_ENABLED(CONFIG_DEVICE_PUBLIC)
+void put_zone_device_private_or_public_page(struct page *page);
 #else
-static inline void put_zone_device_private_page(struct page *page)
+static inline void put_zone_device_private_or_public_page(struct page *page)
 {
 }
-#endif
+#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */
 
 static inline bool is_device_private_page(const struct page *page);
+static inline bool is_device_public_page(const struct page *page);
 
 DECLARE_STATIC_KEY_FALSE(device_private_key);
 
@@ -830,8 +831,9 @@ static inline void put_page(struct page *page)
 	 * include/linux/memremap.h and HMM for details.
 	 */
 	if (static_branch_unlikely(&device_private_key) &&
-	    unlikely(is_device_private_page(page))) {
-		put_zone_device_private_page(page);
+	    unlikely(is_device_private_page(page) ||
+		     is_device_public_page(page))) {
+		put_zone_device_private_or_public_page(page);
 		return;
 	}
 
@@ -1220,8 +1222,10 @@ struct zap_details {
 	pgoff_t last_index;			/* Highest page->index to unmap */
 };
 
-struct page *vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
-		pte_t pte);
+struct page *_vm_normal_page(struct vm_area_struct *vma, unsigned long addr,
+			     pte_t pte, bool with_public_device);
+#define vm_normal_page(vma, addr, pte) _vm_normal_page(vma, addr, pte, false)
+
 struct page *vm_normal_page_pmd(struct vm_area_struct *vma, unsigned long addr,
 				pmd_t pmd);
 
kernel/memremap.c +4 -4
index f42d7483e886..d5ce737af5d7 100644
@@ -477,8 +477,8 @@ struct vmem_altmap *to_vmem_altmap(unsigned long memmap_start)
 #endif /* CONFIG_ZONE_DEVICE */
 
 
-#ifdef CONFIG_DEVICE_PRIVATE
-void put_zone_device_private_page(struct page *page)
+#if IS_ENABLED(CONFIG_DEVICE_PRIVATE) ||  IS_ENABLED(CONFIG_DEVICE_PUBLIC)
+void put_zone_device_private_or_public_page(struct page *page)
 {
 	int count = page_ref_dec_return(page);
 
@@ -498,5 +498,5 @@ void put_zone_device_private_page(struct page *page)
 	} else if (!count)
 		__put_page(page);
 }
-EXPORT_SYMBOL(put_zone_device_private_page);
-#endif /* CONFIG_DEVICE_PRIVATE */
+EXPORT_SYMBOL(put_zone_device_private_or_public_page);
+#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */
mm/Kconfig +11 -0
index c07cb590b2d6..5152959e1912 100644
@@ -719,12 +719,23 @@ config HMM_MIRROR
 config DEVICE_PRIVATE
 	bool "Unaddressable device memory (GPU memory, ...)"
 	depends on ARCH_HAS_HMM
+	select HMM
 
 	help
 	  Allows creation of struct pages to represent unaddressable device
 	  memory; i.e., memory that is only accessible from the device (or
 	  group of devices). You likely also want to select HMM_MIRROR.
 
+config DEVICE_PUBLIC
+	bool "Addressable device memory (like GPU memory)"
+	depends on ARCH_HAS_HMM
+	select HMM
+
+	help
+	  Allows creation of struct pages to represent addressable device
+	  memory; i.e., memory that is accessible from both the device and
+	  the CPU
+
 config FRAME_VECTOR
 	bool
 
mm/gup.c +7 -0
index 76fd199aaae2..b2b4d4263768 100644
@@ -456,6 +456,13 @@ static int get_gate_page(struct mm_struct *mm, unsigned long address,
 		if ((gup_flags & FOLL_DUMP) || !is_zero_pfn(pte_pfn(*pte)))
 			goto unmap;
 		*page = pte_page(*pte);
+
+		/*
+		 * This should never happen (a device public page in the gate
+		 * area).
+		 */
+		if (is_device_public_page(*page))
+			goto unmap;
 	}
 	get_page(*page);
 out:
mm/hmm.c +2 -2
index 8c5ce5f76b2e..1a1e79d390c1 100644
@@ -752,7 +752,7 @@ EXPORT_SYMBOL(hmm_vma_fault);
 #endif /* IS_ENABLED(CONFIG_HMM_MIRROR) */
 
 
-#if IS_ENABLED(CONFIG_DEVICE_PRIVATE)
+#if IS_ENABLED(CONFIG_DEVICE_PRIVATE) ||  IS_ENABLED(CONFIG_DEVICE_PUBLIC)
 struct page *hmm_vma_alloc_locked_page(struct vm_area_struct *vma,
 				       unsigned long addr)
 {
@@ -1192,4 +1192,4 @@ static int __init hmm_init(void)
 }
 
 device_initcall(hmm_init);
-#endif /* IS_ENABLED(CONFIG_DEVICE_PRIVATE) */
+#endif /* CONFIG_DEVICE_PRIVATE || CONFIG_DEVICE_PUBLIC */
mm/madvise.c +1 -1
index 47d8d8a25eae..6cf68d5c49f1 100644
@@ -344,7 +344,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,
 			continue;
 		}
 
-		page = vm_normal_page(vma, addr, ptent);
+		page = _vm_normal_page(vma, addr, ptent, true);
 		if (!page)
 			continue;
 
mm/memcontrol.c +7 -5
index 977d1cf3493a..504eb015f3c4 100644
@@ -4616,10 +4616,11 @@ static int mem_cgroup_move_account(struct page *page,
  *   2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
  *     target for charge migration. if @target is not NULL, the entry is stored
  *     in target->ent.
- *   3(MC_TARGET_DEVICE): like MC_TARGET_PAGE  but page is MEMORY_DEVICE_PRIVATE
- *     (so ZONE_DEVICE page and thus not on the lru). For now we such page is
- *     charge like a regular page would be as for all intent and purposes it is
- *     just special memory taking the place of a regular page.
+ *   3(MC_TARGET_DEVICE): like MC_TARGET_PAGE  but page is MEMORY_DEVICE_PUBLIC
+ *     or MEMORY_DEVICE_PRIVATE (so ZONE_DEVICE page and thus not on the lru).
+ *     For now we such page is charge like a regular page would be as for all
+ *     intent and purposes it is just special memory taking the place of a
+ *     regular page.
  *
  *     See Documentations/vm/hmm.txt and include/linux/hmm.h
  *
@@ -4650,7 +4651,8 @@ static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
 		 */
 		if (page->mem_cgroup == mc.from) {
 			ret = MC_TARGET_PAGE;
-			if (is_device_private_page(page))
+			if (is_device_private_page(page) ||
+			    is_device_public_page(page))
 				ret = MC_TARGET_DEVICE;
 			if (target)
 				target->page = page;
mm/memory.c +41 -5

70 lines not on this page · raw message

mm/migrate.c +38 -19

108 lines not on this page · raw message

mm/swap.c +11 -0
index 62d96b8e5eb3..9295ae960d66 100644
@@ -765,6 +765,17 @@ void release_pages(struct page **pages, int nr, bool cold)
 		if (is_huge_zero_page(page))
 			continue;
 
+		/* Device public page can not be huge page */
+		if (is_device_public_page(page)) {
+			if (locked_pgdat) {
+				spin_unlock_irqrestore(&locked_pgdat->lru_lock,
+						       flags);
+				locked_pgdat = NULL;
+			}
+			put_zone_device_private_or_public_page(page);
+			continue;
+		}
+
 		page = compound_head(page);
 		if (!put_page_testzero(page))
 			continue;
Signature · 7 lines
2.13.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#27 lore raw

PATCH 19/19 mm/hmm: add new helper to hotplug CDM memory region v3

In reply to Jérôme Glisse

Unlike unaddressable memory, coherent device memory has a real resource associated with it on the system (as CPU can address it). Add a new helper to hotplug such memory within the HMM framework.

Changed since v2:
- s/host/public
Changed since v1:
- s/public/host

---
 include/linux/hmm.h |  3 ++
 mm/hmm.c            | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 86 insertions(+), 5 deletions(-)
include/linux/hmm.h +3 -0
index 79e63178fd87..5866f3194c26 100644
@@ -443,6 +443,9 @@ struct hmm_devmem {
 struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
 				  struct device *device,
 				  unsigned long size);
+struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops,
+					   struct device *device,
+					   struct resource *res);
 void hmm_devmem_remove(struct hmm_devmem *devmem);
 
 /*
mm/hmm.c +83 -5

129 lines not on this page · raw message

Signature · 7 lines
2.13.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#28 lore raw

Re: [HMM-v25 19/19] mm/hmm: add new helper to hotplug CDM memory region v3

Quoted 5 lines · Jérôme Glisse

Unlike unaddressable memory, coherent device memory has a real resource associated with it on the system (as CPU can address it). Add a new helper to hotplug such memory within the HMM framework.

Got an new question, coherent device( e.g CCIX) memory are likely reported to OS through ACPI and recognized as NUMA memory node.
Then how can their memory be captured and managed by HMM framework?

--
Regards,
Bob Liu

Quoted 166 lines

Changed since v2:
- s/host/public
Changed since v1:
- s/public/host

---
 include/linux/hmm.h |  3 ++
 mm/hmm.c            | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 86 insertions(+), 5 deletions(-)
include/linux/hmm.h +3 -0
index 79e63178fd87..5866f3194c26 100644
@@ -443,6 +443,9 @@ struct hmm_devmem {
 struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
 				  struct device *device,
 				  unsigned long size);
+struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops,
+					   struct device *device,
+					   struct resource *res);
 void hmm_devmem_remove(struct hmm_devmem *devmem);
 
 /*
mm/hmm.c +83 -5
index 1a1e79d390c1..3faa4d40295e 100644
@@ -854,7 +854,11 @@ static void hmm_devmem_release(struct device *dev, void *data)
 	zone = page_zone(page);
 
 	mem_hotplug_begin();
-	__remove_pages(zone, start_pfn, npages);
+	if (resource->desc == IORES_DESC_DEVICE_PRIVATE_MEMORY)
+		__remove_pages(zone, start_pfn, npages);
+	else
+		arch_remove_memory(start_pfn << PAGE_SHIFT,
+				   npages << PAGE_SHIFT);
 	mem_hotplug_done();
 
 	hmm_devmem_radix_release(resource);
@@ -890,7 +894,11 @@ static int hmm_devmem_pages_create(struct hmm_devmem *devmem)
 	if (is_ram == REGION_INTERSECTS)
 		return -ENXIO;
 
-	devmem->pagemap.type = MEMORY_DEVICE_PRIVATE;
+	if (devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY)
+		devmem->pagemap.type = MEMORY_DEVICE_PUBLIC;
+	else
+		devmem->pagemap.type = MEMORY_DEVICE_PRIVATE;
+
 	devmem->pagemap.res = devmem->resource;
 	devmem->pagemap.page_fault = hmm_devmem_fault;
 	devmem->pagemap.page_free = hmm_devmem_free;
@@ -935,9 +943,15 @@ static int hmm_devmem_pages_create(struct hmm_devmem *devmem)
 	 * over the device memory is un-accessible thus we do not want to
 	 * create a linear mapping for the memory like arch_add_memory()
 	 * would do.
+	 *
+	 * For device public memory, which is accesible by the CPU, we do
+	 * want the linear mapping and thus use arch_add_memory().
 	 */
-	ret = add_pages(nid, align_start >> PAGE_SHIFT,
-			align_size >> PAGE_SHIFT, false);
+	if (devmem->pagemap.type == MEMORY_DEVICE_PUBLIC)
+		ret = arch_add_memory(nid, align_start, align_size, false);
+	else
+		ret = add_pages(nid, align_start >> PAGE_SHIFT,
+				align_size >> PAGE_SHIFT, false);
 	if (ret) {
 		mem_hotplug_done();
 		goto error_add_memory;
@@ -1084,6 +1098,67 @@ struct hmm_devmem *hmm_devmem_add(const struct hmm_devmem_ops *ops,
 }
 EXPORT_SYMBOL(hmm_devmem_add);
 
+struct hmm_devmem *hmm_devmem_add_resource(const struct hmm_devmem_ops *ops,
+					   struct device *device,
+					   struct resource *res)
+{
+	struct hmm_devmem *devmem;
+	int ret;
+
+	if (res->desc != IORES_DESC_DEVICE_PUBLIC_MEMORY)
+		return ERR_PTR(-EINVAL);
+
+	static_branch_enable(&device_private_key);
+
+	devmem = devres_alloc_node(&hmm_devmem_release, sizeof(*devmem),
+				   GFP_KERNEL, dev_to_node(device));
+	if (!devmem)
+		return ERR_PTR(-ENOMEM);
+
+	init_completion(&devmem->completion);
+	devmem->pfn_first = -1UL;
+	devmem->pfn_last = -1UL;
+	devmem->resource = res;
+	devmem->device = device;
+	devmem->ops = ops;
+
+	ret = percpu_ref_init(&devmem->ref, &hmm_devmem_ref_release,
+			      0, GFP_KERNEL);
+	if (ret)
+		goto error_percpu_ref;
+
+	ret = devm_add_action(device, hmm_devmem_ref_exit, &devmem->ref);
+	if (ret)
+		goto error_devm_add_action;
+
+
+	devmem->pfn_first = devmem->resource->start >> PAGE_SHIFT;
+	devmem->pfn_last = devmem->pfn_first +
+			   (resource_size(devmem->resource) >> PAGE_SHIFT);
+
+	ret = hmm_devmem_pages_create(devmem);
+	if (ret)
+		goto error_devm_add_action;
+
+	devres_add(device, devmem);
+
+	ret = devm_add_action(device, hmm_devmem_ref_kill, &devmem->ref);
+	if (ret) {
+		hmm_devmem_remove(devmem);
+		return ERR_PTR(ret);
+	}
+
+	return devmem;
+
+error_devm_add_action:
+	hmm_devmem_ref_kill(&devmem->ref);
+	hmm_devmem_ref_exit(&devmem->ref);
+error_percpu_ref:
+	devres_free(devmem);
+	return ERR_PTR(ret);
+}
+EXPORT_SYMBOL(hmm_devmem_add_resource);
+
 /*
  * hmm_devmem_remove() - remove device memory (kill and free ZONE_DEVICE)
  *
@@ -1097,6 +1172,7 @@ void hmm_devmem_remove(struct hmm_devmem *devmem)
 {
 	resource_size_t start, size;
 	struct device *device;
+	bool cdm = false;
 
 	if (!devmem)
 		return;
@@ -1105,11 +1181,13 @@ void hmm_devmem_remove(struct hmm_devmem *devmem)
 	start = devmem->resource->start;
 	size = resource_size(devmem->resource);
 
+	cdm = devmem->resource->desc == IORES_DESC_DEVICE_PUBLIC_MEMORY;
 	hmm_devmem_ref_kill(&devmem->ref);
 	hmm_devmem_ref_exit(&devmem->ref);
 	hmm_devmem_pages_remove(devmem);
 
-	devm_release_mem_region(device, start, size);
+	if (!cdm)
+		devm_release_mem_region(device, start, size);
 }
 EXPORT_SYMBOL(hmm_devmem_remove);
 
#29 lore raw

Re: [HMM-v25 19/19] mm/hmm: add new helper to hotplug CDM memory region v3

Quoted 11 lines · Bob Liu
Quoted 5 lines · Jérôme Glisse

Unlike unaddressable memory, coherent device memory has a real resource associated with it on the system (as CPU can address it). Add a new helper to hotplug such memory within the HMM framework.

Got an new question, coherent device( e.g CCIX) memory are likely reported to OS through ACPI and recognized as NUMA memory node.
Then how can their memory be captured and managed by HMM framework?

Only platform that has such memory today is powerpc and it is not reported as regular memory by the firmware hence why they need this helper.

I don't think anyone has defined anything yet for x86 and acpi. As this is memory on PCIE like interface then i don't expect it to be reported as NUMA memory node but as io range like any regular PCIE resources. Device driver through capabilities flags would then figure out if the link between the device and CPU is CCIX capable if so it can use this helper to hotplug it as device memory.

Jérôme

#30 lore raw

Re: [HMM-v25 19/19] mm/hmm: add new helper to hotplug CDM memory region v3

Quoted 13 lines · Jerome Glisse
Quoted 11 lines · Bob Liu
Quoted 5 lines · Jerome Glisse

Unlike unaddressable memory, coherent device memory has a real resource associated with it on the system (as CPU can address it). Add a new helper to hotplug such memory within the HMM framework.

Got an new question, coherent device( e.g CCIX) memory are likely reported to OS through ACPI and recognized as NUMA memory node.
Then how can their memory be captured and managed by HMM framework?

Quoted 4 lines

Only platform that has such memory today is powerpc and it is not reported as regular memory by the firmware hence why they need this helper.

I don't think anyone has defined anything yet for x86 and acpi. As this is

Not yet, but now the ACPI spec has Heterogeneous Memory Attribute
Table (HMAT) table defined in ACPI 6.2.
The HMAT can cover CPU-addressable memory types(though not non-cache coherent on-device memory).

Ross from Intel already done some work on this, see:
https://lwn.net/Articles/724562/

arm64 supports APCI also, there is likely more this kind of device when CCIX is out (should be very soon if on schedule).

Quoted 8 lines

memory on PCIE like interface then i don't expect it to be reported as NUMA memory node but as io range like any regular PCIE resources. Device driver through capabilities flags would then figure out if the link between the device and CPU is CCIX capable if so it can use this helper to hotplug it as device memory.

From my point of view, Cache coherent device memory will popular soon and reported through ACPI/UEFI.

Extending NUMA policy still sounds more reasonable to me.

--
Thanks,
Bob Liu

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#31 lore raw

Re: [HMM-v25 19/19] mm/hmm: add new helper to hotplug CDM memory region v3

Quoted 24 lines · Bob Liu
Quoted 13 lines · Jerome Glisse
Quoted 11 lines · Bob Liu
Quoted 5 lines · Jerome Glisse

Unlike unaddressable memory, coherent device memory has a real resource associated with it on the system (as CPU can address it). Add a new helper to hotplug such memory within the HMM framework.

Got an new question, coherent device( e.g CCIX) memory are likely reported to OS through ACPI and recognized as NUMA memory node.
Then how can their memory be captured and managed by HMM framework?

Quoted 4 lines

Only platform that has such memory today is powerpc and it is not reported as regular memory by the firmware hence why they need this helper.

I don't think anyone has defined anything yet for x86 and acpi. As this is

Not yet, but now the ACPI spec has Heterogeneous Memory Attribute Table (HMAT) table defined in ACPI 6.2.
The HMAT can cover CPU-addressable memory types(though not non-cache coherent on-device memory).

Quoted 5 lines

Ross from Intel already done some work on this, see:
https://lwn.net/Articles/724562/

arm64 supports APCI also, there is likely more this kind of device when CCIX is out (should be very soon if on schedule).

HMAT is not for the same thing, AFAIK HMAT is for deep "hierarchy" memory ie when you have several kind of memory each with different characteristics:
- HBM very fast (latency) and high bandwidth, non persistent, somewhat small (ie few giga bytes)
- Persistent memory, slower (both latency and bandwidth) big (tera bytes)
- DDR (good old memory) well characteristics are between HBM and persistent

So AFAICT this has nothing to do with what HMM is for, ie device memory. Note that device memory can have a hierarchy of memory themself (HBM, GDDR and in maybe even persistent memory).

Quoted 10 lines
Quoted 6 lines

memory on PCIE like interface then i don't expect it to be reported as NUMA memory node but as io range like any regular PCIE resources. Device driver through capabilities flags would then figure out if the link between the device and CPU is CCIX capable if so it can use this helper to hotplug it as device memory.

From my point of view, Cache coherent device memory will popular soon and reported through ACPI/UEFI. Extending NUMA policy still sounds more reasonable to me.

Cache coherent device will be reported through standard mecanisms defined by the bus standard they are using. To my knowledge all the standard are either on top of PCIE or are similar to PCIE.

It is true that on many platform PCIE resource is manage/initialize by the bios (UEFI) but it is platform specific. In some case we reprogram what the bios pick.

So like i was saying i don't expect the BIOS/UEFI to report device memory as regular memory. It will be reported as a regular PCIE resources and then the device driver will be able to determine through some flags if the link between the CPU(s) and the device is cache coherent or not. At that point the device driver can use register it with HMM helper.

The whole NUMA discussion happen several time in the past i suggest looking on mm list archive for them. But it was rule out for several reasons. Top of my head:
- people hate CPU less node and device memory is inherently CPU less
- device driver want total control over memory and thus to be isolated from mm mecanism and doing all those special cases was not welcome
- existing NUMA migration mecanism are ill suited for this memory as access by the device to the memory is unknown to core mm and there is no easy way to report it or track it (this kind of depends on the platform and hardware)

I am likely missing other big points.

Cheers,
Jerome

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#32 lore raw

Re: [HMM-v25 19/19] mm/hmm: add new helper to hotplug CDM memory region v3

Quoted 31 lines · Jerome Glisse
Quoted 24 lines · Bob Liu
Quoted 13 lines · Jerome Glisse
Quoted 11 lines · Bob Liu
Quoted 5 lines · Jérôme Glisse

Unlike unaddressable memory, coherent device memory has a real resource associated with it on the system (as CPU can address it). Add a new helper to hotplug such memory within the HMM framework.

Got an new question, coherent device( e.g CCIX) memory are likely reported to OS through ACPI and recognized as NUMA memory node.
Then how can their memory be captured and managed by HMM framework?

Quoted 4 lines

Only platform that has such memory today is powerpc and it is not reported as regular memory by the firmware hence why they need this helper.

I don't think anyone has defined anything yet for x86 and acpi. As this is

Not yet, but now the ACPI spec has Heterogeneous Memory Attribute Table (HMAT) table defined in ACPI 6.2.
The HMAT can cover CPU-addressable memory types(though not non-cache coherent on-device memory).

Quoted 5 lines

Ross from Intel already done some work on this, see:
https://lwn.net/Articles/724562/

arm64 supports APCI also, there is likely more this kind of device when CCIX is out (should be very soon if on schedule).

Quoted 7 lines

HMAT is not for the same thing, AFAIK HMAT is for deep "hierarchy" memory ie when you have several kind of memory each with different characteristics:
- HBM very fast (latency) and high bandwidth, non persistent, somewhat small (ie few giga bytes)
- Persistent memory, slower (both latency and bandwidth) big (tera bytes)
- DDR (good old memory) well characteristics are between HBM and persistent

Okay, then how the kernel handle the situation of "kind of memory each with different characteristics"? Does someone have any suggestion? I thought HMM can do this.
Numa policy/node distance is good but perhaps require a few extending, e.g a HBM node can't be swap, can't accept DDR fallback allocation.

Quoted 4 lines

So AFAICT this has nothing to do with what HMM is for, ie device memory. Note that device memory can have a hierarchy of memory themself (HBM, GDDR and in maybe even persistent memory).

This looks like a subset of HMAT when CPU can address device memory directly in cache-coherent way.

Quoted 15 lines
Quoted 10 lines
Quoted 6 lines

memory on PCIE like interface then i don't expect it to be reported as NUMA memory node but as io range like any regular PCIE resources. Device driver through capabilities flags would then figure out if the link between the device and CPU is CCIX capable if so it can use this helper to hotplug it as device memory.

From my point of view, Cache coherent device memory will popular soon and reported through ACPI/UEFI. Extending NUMA policy still sounds more reasonable to me.

Cache coherent device will be reported through standard mecanisms defined by the bus standard they are using. To my knowledge all the standard are either on top of PCIE or are similar to PCIE.

Quoted 5 lines

It is true that on many platform PCIE resource is manage/initialize by the bios (UEFI) but it is platform specific. In some case we reprogram what the bios pick.

So like i was saying i don't expect the BIOS/UEFI to report device memory as

But it's happening.
In my understanding, that's why HMAT was introduced.
For reporting device memory as regular memory(with different characteristics).

--
Regards,
Bob Liu

Quoted 24 lines

regular memory. It will be reported as a regular PCIE resources and then the device driver will be able to determine through some flags if the link between the CPU(s) and the device is cache coherent or not. At that point the device driver can use register it with HMM helper.

The whole NUMA discussion happen several time in the past i suggest looking on mm list archive for them. But it was rule out for several reasons. Top of my head:
- people hate CPU less node and device memory is inherently CPU less
- device driver want total control over memory and thus to be isolated from mm mecanism and doing all those special cases was not welcome
- existing NUMA migration mecanism are ill suited for this memory as access by the device to the memory is unknown to core mm and there is no easy way to report it or track it (this kind of depends on the platform and hardware)

I am likely missing other big points.

Cheers,
Jérôme

.

#33 lore raw

Re: [HMM-v25 19/19] mm/hmm: add new helper to hotplug CDM memory region v3

Quoted 40 lines · Bob Liu
Quoted 31 lines · Jerome Glisse
Quoted 24 lines · Bob Liu
Quoted 13 lines · Jerome Glisse
Quoted 11 lines · Bob Liu
Quoted 5 lines · Jerome Glisse

Unlike unaddressable memory, coherent device memory has a real resource associated with it on the system (as CPU can address it). Add a new helper to hotplug such memory within the HMM framework.

Got an new question, coherent device( e.g CCIX) memory are likely reported to OS through ACPI and recognized as NUMA memory node.
Then how can their memory be captured and managed by HMM framework?

Quoted 4 lines

Only platform that has such memory today is powerpc and it is not reported as regular memory by the firmware hence why they need this helper.

I don't think anyone has defined anything yet for x86 and acpi. As this is

Not yet, but now the ACPI spec has Heterogeneous Memory Attribute Table (HMAT) table defined in ACPI 6.2.
The HMAT can cover CPU-addressable memory types(though not non-cache coherent on-device memory).

Quoted 5 lines

Ross from Intel already done some work on this, see:
https://lwn.net/Articles/724562/

arm64 supports APCI also, there is likely more this kind of device when CCIX is out (should be very soon if on schedule).

Quoted 7 lines

HMAT is not for the same thing, AFAIK HMAT is for deep "hierarchy" memory ie when you have several kind of memory each with different characteristics:
- HBM very fast (latency) and high bandwidth, non persistent, somewhat small (ie few giga bytes)
- Persistent memory, slower (both latency and bandwidth) big (tera bytes)
- DDR (good old memory) well characteristics are between HBM and persistent

Quoted 4 lines

Okay, then how the kernel handle the situation of "kind of memory each with different characteristics"? Does someone have any suggestion? I thought HMM can do this.
Numa policy/node distance is good but perhaps require a few extending, e.g a HBM node can't be swap, can't accept DDR fallback allocation.

I don't think there is any consensus for this. I put forward the idea that NUMA needed to be extended as with deep hierarchy it is not only the distance between two nodes but also others factors like persistency, bandwidth, latency ...

Quoted 6 lines
Quoted 4 lines

So AFAICT this has nothing to do with what HMM is for, ie device memory. Note that device memory can have a hierarchy of memory themself (HBM, GDDR and in maybe even persistent memory).

This looks like a subset of HMAT when CPU can address device memory directly in cache-coherent way.

It is not, it is much more complex than that. Linux kernel has no idea on what is going on a device and thus do not have any usefull informations to make proper decission regarding device memory. Here device is real device ie something with processing capability, not something like HBM or persistent memory even if the latter is associated with a struct device inside linux kernel.

Quoted 21 lines
Quoted 21 lines
Quoted 9 lines
Quoted 8 lines

memory on PCIE like interface then i don't expect it to be reported as NUMA memory node but as io range like any regular PCIE resources. Device driver through capabilities flags would then figure out if the link between the device and CPU is CCIX capable if so it can use this helper to hotplug it as device memory.

Quoted 3 lines

From my point of view, Cache coherent device memory will popular soon and reported through ACPI/UEFI. Extending NUMA policy still sounds more reasonable to me.

Cache coherent device will be reported through standard mecanisms defined by the bus standard they are using. To my knowledge all the standard are either on top of PCIE or are similar to PCIE.

It is true that on many platform PCIE resource is manage/initialize by the bios (UEFI) but it is platform specific. In some case we reprogram what the bios pick.

Quoted 5 lines
Quoted 1 line

So like i was saying i don't expect the BIOS/UEFI to report device memory as

But it's happening.
In my understanding, that's why HMAT was introduced.
For reporting device memory as regular memory(with different characteristics).

That is not my understanding but only Intel can confirm. HMAT was introduced for things like HBM or persistent memory. Which i do not consider as device memory. Sure persistent memory is assign a device struct because it is easier for integration with the block system i assume. But it does not make it a device in my view. For me a device is a piece of hardware that has some processing capabilities (network adapter, sound card, GPU, ...)

But we can argue about semantic and what a device is. For all intent and purposes device in HMM context is some piece of hardware with processing capabilities and local device memory.

Cheers,
Jerome

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#34 lore raw

Re: [HMM-v25 19/19] mm/hmm: add new helper to hotplug CDM memory region v3

Quoted 98 lines · Jerome Glisse
Quoted 40 lines · Bob Liu
Quoted 31 lines · Jerome Glisse
Quoted 24 lines · Bob Liu
Quoted 13 lines · Jerome Glisse
Quoted 11 lines · Bob Liu
Quoted 5 lines · Jérôme Glisse

Unlike unaddressable memory, coherent device memory has a real resource associated with it on the system (as CPU can address it). Add a new helper to hotplug such memory within the HMM framework.

Got an new question, coherent device( e.g CCIX) memory are likely reported to OS through ACPI and recognized as NUMA memory node.
Then how can their memory be captured and managed by HMM framework?

Quoted 4 lines

Only platform that has such memory today is powerpc and it is not reported as regular memory by the firmware hence why they need this helper.

I don't think anyone has defined anything yet for x86 and acpi. As this is

Not yet, but now the ACPI spec has Heterogeneous Memory Attribute Table (HMAT) table defined in ACPI 6.2.
The HMAT can cover CPU-addressable memory types(though not non-cache coherent on-device memory).

Quoted 5 lines

Ross from Intel already done some work on this, see:
https://lwn.net/Articles/724562/

arm64 supports APCI also, there is likely more this kind of device when CCIX is out (should be very soon if on schedule).

Quoted 7 lines

HMAT is not for the same thing, AFAIK HMAT is for deep "hierarchy" memory ie when you have several kind of memory each with different characteristics:
- HBM very fast (latency) and high bandwidth, non persistent, somewhat small (ie few giga bytes)
- Persistent memory, slower (both latency and bandwidth) big (tera bytes)
- DDR (good old memory) well characteristics are between HBM and persistent

Quoted 4 lines

Okay, then how the kernel handle the situation of "kind of memory each with different characteristics"? Does someone have any suggestion? I thought HMM can do this.
Numa policy/node distance is good but perhaps require a few extending, e.g a HBM node can't be swap, can't accept DDR fallback allocation.

I don't think there is any consensus for this. I put forward the idea that NUMA needed to be extended as with deep hierarchy it is not only the distance between two nodes but also others factors like persistency, bandwidth, latency ...

Quoted 6 lines
Quoted 4 lines

So AFAICT this has nothing to do with what HMM is for, ie device memory. Note that device memory can have a hierarchy of memory themself (HBM, GDDR and in maybe even persistent memory).

This looks like a subset of HMAT when CPU can address device memory directly in cache-coherent way.

It is not, it is much more complex than that. Linux kernel has no idea on what is going on a device and thus do not have any usefull informations to make proper decission regarding device memory. Here device is real device ie something with processing capability, not something like HBM or persistent memory even if the latter is associated with a struct device inside linux kernel.

Quoted 21 lines
Quoted 21 lines
Quoted 9 lines
Quoted 8 lines

memory on PCIE like interface then i don't expect it to be reported as NUMA memory node but as io range like any regular PCIE resources. Device driver through capabilities flags would then figure out if the link between the device and CPU is CCIX capable if so it can use this helper to hotplug it as device memory.

Quoted 3 lines

From my point of view, Cache coherent device memory will popular soon and reported through ACPI/UEFI. Extending NUMA policy still sounds more reasonable to me.

Cache coherent device will be reported through standard mecanisms defined by the bus standard they are using. To my knowledge all the standard are either on top of PCIE or are similar to PCIE.

It is true that on many platform PCIE resource is manage/initialize by the bios (UEFI) but it is platform specific. In some case we reprogram what the bios pick.

Quoted 5 lines
Quoted 1 line

So like i was saying i don't expect the BIOS/UEFI to report device memory as

But it's happening.
In my understanding, that's why HMAT was introduced.
For reporting device memory as regular memory(with different characteristics).

That is not my understanding but only Intel can confirm. HMAT was introduced for things like HBM or persistent memory. Which i do not consider as device memory. Sure persistent memory is assign a device struct because it is easier for integration with the block system i assume. But it does not make it a device in my view. For me a device is a piece of hardware that has some processing capabilities (network adapter, sound card, GPU, ...)

Quoted 3 lines

But we can argue about semantic and what a device is. For all intent and purposes device in HMM context is some piece of hardware with processing capabilities and local device memory.

I would say that device memory at its base-level is a memory range whose availability is dependent on a device-driver. HMM layers some additional functionality on top, but ZONE_DEVICE should only be seen as the device-driver controlled lifetime and not conflated with the incremental HMM functionality.

HMAT simply allows you to associate a memory range with a numa-node / proximity-domain number that represents a set of performance / feature characteristics.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#35 lore raw

Re: [HMM-v25 19/19] mm/hmm: add new helper to hotplug CDM memory region v3

In reply to Jerome Glisse
Quoted 98 lines · Jerome Glisse
Quoted 40 lines · Bob Liu
Quoted 31 lines · Jerome Glisse
Quoted 24 lines · Bob Liu
Quoted 13 lines · Jerome Glisse
Quoted 11 lines · Bob Liu
Quoted 5 lines · Jerome Glisse

Unlike unaddressable memory, coherent device memory has a real resource associated with it on the system (as CPU can address it). Add a new helper to hotplug such memory within the HMM framework.

Got an new question, coherent device( e.g CCIX) memory are likely reported to OS through ACPI and recognized as NUMA memory node.
Then how can their memory be captured and managed by HMM framework?

Quoted 4 lines

Only platform that has such memory today is powerpc and it is not reported as regular memory by the firmware hence why they need this helper.

I don't think anyone has defined anything yet for x86 and acpi. As this is

Not yet, but now the ACPI spec has Heterogeneous Memory Attribute Table (HMAT) table defined in ACPI 6.2.
The HMAT can cover CPU-addressable memory types(though not non-cache coherent on-device memory).

Quoted 5 lines

Ross from Intel already done some work on this, see:
https://lwn.net/Articles/724562/

arm64 supports APCI also, there is likely more this kind of device when CCIX is out (should be very soon if on schedule).

Quoted 7 lines

HMAT is not for the same thing, AFAIK HMAT is for deep "hierarchy" memory ie when you have several kind of memory each with different characteristics:
- HBM very fast (latency) and high bandwidth, non persistent, somewhat small (ie few giga bytes)
- Persistent memory, slower (both latency and bandwidth) big (tera bytes)
- DDR (good old memory) well characteristics are between HBM and persistent

Quoted 4 lines

Okay, then how the kernel handle the situation of "kind of memory each with different characteristics"? Does someone have any suggestion? I thought HMM can do this.
Numa policy/node distance is good but perhaps require a few extending, e.g a HBM node can't be swap, can't accept DDR fallback allocation.

I don't think there is any consensus for this. I put forward the idea that NUMA needed to be extended as with deep hierarchy it is not only the distance between two nodes but also others factors like persistency, bandwidth, latency ...

Quoted 6 lines
Quoted 4 lines

So AFAICT this has nothing to do with what HMM is for, ie device memory. Note that device memory can have a hierarchy of memory themself (HBM, GDDR and in maybe even persistent memory).

This looks like a subset of HMAT when CPU can address device memory directly in cache-coherent way.

It is not, it is much more complex than that. Linux kernel has no idea on what is going on a device and thus do not have any usefull informations to make proper decission regarding device memory. Here device is real device ie something with processing capability, not something like HBM or persistent memory even if the latter is associated with a struct device inside linux kernel.

Quoted 21 lines
Quoted 21 lines
Quoted 9 lines
Quoted 8 lines

memory on PCIE like interface then i don't expect it to be reported as NUMA memory node but as io range like any regular PCIE resources. Device driver through capabilities flags would then figure out if the link between the device and CPU is CCIX capable if so it can use this helper to hotplug it as device memory.

Quoted 3 lines

From my point of view, Cache coherent device memory will popular soon and reported through ACPI/UEFI. Extending NUMA policy still sounds more reasonable to me.

Cache coherent device will be reported through standard mecanisms defined by the bus standard they are using. To my knowledge all the standard are either on top of PCIE or are similar to PCIE.

It is true that on many platform PCIE resource is manage/initialize by the bios (UEFI) but it is platform specific. In some case we reprogram what the bios pick.

Quoted 5 lines
Quoted 1 line

So like i was saying i don't expect the BIOS/UEFI to report device memory as

But it's happening.
In my understanding, that's why HMAT was introduced.
For reporting device memory as regular memory(with different characteristics).

That is not my understanding but only Intel can confirm. HMAT was introduced for things like HBM or persistent memory. Which i do not consider as device memory. Sure persistent memory is assign a device struct because it is easier for integration with the block system i assume. But it does not make it a device in my view. For me a device is a piece of hardware that has some processing capabilities (network adapter, sound card, GPU, ...)

Quoted 3 lines

But we can argue about semantic and what a device is. For all intent and purposes device in HMM context is some piece of hardware with processing capabilities and local device memory.

I personally don't see a reason why we couldn't use the HMAT to describe device memory. The idea of having memory-only NUMA nodes is already a realty post-HMAT, and the HMAT is just there to give you information on the memory ranges in the system. I realize that you may need a different device driver to set the memory up, but once you do set it up and it's cache coherent, doesn't it just look like any other memory range where you can say things like:

My memory starts at X
My memory has size Y
My memory's performance from CPU Z is XXX (latency, bandwidth, read & write) etc?

- Ross

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#36 lore raw

Re: [HMM-v25 19/19] mm/hmm: add new helper to hotplug CDM memory region v3

Quoted 111 lines · Ross Zwisler
Quoted 98 lines · Jerome Glisse
Quoted 40 lines · Bob Liu
Quoted 31 lines · Jerome Glisse
Quoted 24 lines · Bob Liu
Quoted 13 lines · Jerome Glisse
Quoted 11 lines · Bob Liu
Quoted 5 lines · Jérôme Glisse

Unlike unaddressable memory, coherent device memory has a real resource associated with it on the system (as CPU can address it). Add a new helper to hotplug such memory within the HMM framework.

Got an new question, coherent device( e.g CCIX) memory are likely reported to OS through ACPI and recognized as NUMA memory node.
Then how can their memory be captured and managed by HMM framework?

Quoted 4 lines

Only platform that has such memory today is powerpc and it is not reported as regular memory by the firmware hence why they need this helper.

I don't think anyone has defined anything yet for x86 and acpi. As this is

Not yet, but now the ACPI spec has Heterogeneous Memory Attribute Table (HMAT) table defined in ACPI 6.2.
The HMAT can cover CPU-addressable memory types(though not non-cache coherent on-device memory).

Quoted 5 lines

Ross from Intel already done some work on this, see:
https://lwn.net/Articles/724562/

arm64 supports APCI also, there is likely more this kind of device when CCIX is out (should be very soon if on schedule).

Quoted 7 lines

HMAT is not for the same thing, AFAIK HMAT is for deep "hierarchy" memory ie when you have several kind of memory each with different characteristics:
- HBM very fast (latency) and high bandwidth, non persistent, somewhat small (ie few giga bytes)
- Persistent memory, slower (both latency and bandwidth) big (tera bytes)
- DDR (good old memory) well characteristics are between HBM and persistent

Quoted 4 lines

Okay, then how the kernel handle the situation of "kind of memory each with different characteristics"? Does someone have any suggestion? I thought HMM can do this.
Numa policy/node distance is good but perhaps require a few extending, e.g a HBM node can't be swap, can't accept DDR fallback allocation.

I don't think there is any consensus for this. I put forward the idea that NUMA needed to be extended as with deep hierarchy it is not only the distance between two nodes but also others factors like persistency, bandwidth, latency ...

Quoted 6 lines
Quoted 4 lines

So AFAICT this has nothing to do with what HMM is for, ie device memory. Note that device memory can have a hierarchy of memory themself (HBM, GDDR and in maybe even persistent memory).

This looks like a subset of HMAT when CPU can address device memory directly in cache-coherent way.

It is not, it is much more complex than that. Linux kernel has no idea on what is going on a device and thus do not have any usefull informations to make proper decission regarding device memory. Here device is real device ie something with processing capability, not something like HBM or persistent memory even if the latter is associated with a struct device inside linux kernel.

Quoted 21 lines
Quoted 21 lines
Quoted 9 lines
Quoted 8 lines

memory on PCIE like interface then i don't expect it to be reported as NUMA memory node but as io range like any regular PCIE resources. Device driver through capabilities flags would then figure out if the link between the device and CPU is CCIX capable if so it can use this helper to hotplug it as device memory.

Quoted 3 lines

From my point of view, Cache coherent device memory will popular soon and reported through ACPI/UEFI. Extending NUMA policy still sounds more reasonable to me.

Cache coherent device will be reported through standard mecanisms defined by the bus standard they are using. To my knowledge all the standard are either on top of PCIE or are similar to PCIE.

It is true that on many platform PCIE resource is manage/initialize by the bios (UEFI) but it is platform specific. In some case we reprogram what the bios pick.

Quoted 5 lines
Quoted 1 line

So like i was saying i don't expect the BIOS/UEFI to report device memory as

But it's happening.
In my understanding, that's why HMAT was introduced.
For reporting device memory as regular memory(with different characteristics).

That is not my understanding but only Intel can confirm. HMAT was introduced for things like HBM or persistent memory. Which i do not consider as device memory. Sure persistent memory is assign a device struct because it is easier for integration with the block system i assume. But it does not make it a device in my view. For me a device is a piece of hardware that has some processing capabilities (network adapter, sound card, GPU, ...)

Quoted 3 lines

But we can argue about semantic and what a device is. For all intent and purposes device in HMM context is some piece of hardware with processing capabilities and local device memory.

I personally don't see a reason why we couldn't use the HMAT to describe device memory. The idea of having memory-only NUMA nodes is already a realty post-HMAT, and the HMAT is just there to give you information on the memory ranges in the system. I realize that you may need a different device driver to set the memory up, but once you do set it up and it's cache coherent, doesn't it just look like any other memory range where you can say things like:

Quoted 4 lines

My memory starts at X
My memory has size Y
My memory's performance from CPU Z is XXX (latency, bandwidth, read & write) etc?

Does HMAT support device hotplug ? I am unfamiliar with the whole inner working of ACPI versus PCIE. Anyway i don't see any issue with device memory also showing through HMAT but like i said device driver for the device will want to be in total control of that memory.

Like i said issue here is that core kernel is unaware of the device activity ie on what part of memory the device is actively working. So core mm can not make inform decision on what should be migrated to device memory. Also we do not want regular memory allocation to end in device memory unless explicitly ask for. Few reasons for that. First this memory might not only be use for compute task but also for graphic and in that case they are hard constraint on physically contiguous memory allocation that require the GPU to move thing around to make room for graphic object (can't allow GUP).

Second reasons, the device memory is inherently unreliable. If there is a bug in the device driver or the user manage to trigger a faulty condition on GPU the device might need a hard reset (ie cut PCIE power to device) which leads to loss of memory content. While GPU are becoming more and more resilient they are still prone to lockup.

Finaly for GPU there is a common pattern of memory over-commit. You pretend to each application as if they were the only one and allow each of them to allocate all of the device memory or more than could with strict sharing. As GPU have long timeslice between switching to different context/application they can easily move out and in large chunk of the process memory at context/application switching. This is have proven to be a key aspect to allow maximum performances accross several concurrent application/context.

To implement this easiest solution is for the device to lie about how much memory it has and use the system memory as an overflow.

I am not saying that NUMA is not the way forward, i am saying that as it is today it is not suited for this. It is lacking metric, it is lacking logic, it is lacking features. We could add all this but it is a lot of work and i don't feel that we have enough real world experience to do so now. I would rather have each devices grow proper infrastructure in their driver through device specific API.

Then identify common pattern and from there try to build a sane API (if any such thing exist :)) rather than trying today to build the whole house from the ground up with just a foggy idea of how it should looks in the end.

Cheers,
Jérôme

#37 lore raw

Re: [HMM-v25 19/19] mm/hmm: add new helper to hotplug CDM memory region v3

On Tue, Sep 05, 2017 at 03:20:50PM -0400, Jerome Glisse wrote:
<>

Quoted 4 lines

Does HMAT support device hotplug ? I am unfamiliar with the whole inner working of ACPI versus PCIE. Anyway i don't see any issue with device memory also showing through HMAT but like i said device driver for the device will want to be in total control of that memory.

Yep, the HMAT will support device hotplug via the _HMA method (section 6.2.18 of ACPI 6.2). This basically supplies an entirely new HMAT that the system will use to replace the current one.

I don't yet have support for _HMA in my enabling, but I do intend to add support for it once we settle on a sysfs API for the regular boot-time case.

Quoted 23 lines

Like i said issue here is that core kernel is unaware of the device activity ie on what part of memory the device is actively working. So core mm can not make inform decision on what should be migrated to device memory. Also we do not want regular memory allocation to end in device memory unless explicitly ask for. Few reasons for that. First this memory might not only be use for compute task but also for graphic and in that case they are hard constraint on physically contiguous memory allocation that require the GPU to move thing around to make room for graphic object (can't allow GUP).

Second reasons, the device memory is inherently unreliable. If there is a bug in the device driver or the user manage to trigger a faulty condition on GPU the device might need a hard reset (ie cut PCIE power to device) which leads to loss of memory content. While GPU are becoming more and more resilient they are still prone to lockup.

Finaly for GPU there is a common pattern of memory over-commit. You pretend to each application as if they were the only one and allow each of them to allocate all of the device memory or more than could with strict sharing. As GPU have long timeslice between switching to different context/application they can easily move out and in large chunk of the process memory at context/application switching. This is have proven to be a key aspect to allow maximum performances accross several concurrent application/context.

Quoted 2 lines

To implement this easiest solution is for the device to lie about how much memory it has and use the system memory as an overflow.

I don't think any of this precludes the HMAT being involved. This is all very similar to what I think we need to do for high bandwidth memory, for example. We don't want the OS to use it for anything, and we want all of it to be available for applications to allocate and use for their specific workload. We don't want to make any assumptions about how it can or should be used.

The HMAT is just there to give us a few things:

1) It provides us with an explicit way of telling the OS not to use the memory, in the form of the "Reservation hint" flag in the Memory Subsystem Address Range Structure (ACPI 6.2 section 5.2.27.3). I expect that this will be set for persistent memory and HBM, and it sounds like you'd expect it to be set for your device memory as well.

2) It provides us with a way of telling userspace "hey, I know about some memory, and I can tell you its performance characteristics". All control of how this memory is allocated and used is still left to userspace.

Quoted 5 lines

I am not saying that NUMA is not the way forward, i am saying that as it is today it is not suited for this. It is lacking metric, it is lacking logic, it is lacking features. We could add all this but it is a lot of work and i don't feel that we have enough real world experience to do so now. I would rather have each devices grow proper infrastructure in their driver through device specific API.

To be clear, I'm not proposing that we teach the NUMA code how to automatically allocate for a given numa node, balance, etc. memory described by the HMAT. All I want is an API that says "here is some memory, I'll tell you all I can about it and let you do with it what you will", and perhaps a way to manually allocate what you want.

And yes, this is very hand-wavy at this point. :) After I get the sysfs portion sussed out the next step is to work on enabling something like libnuma to allow the memory to be manually allocated.

I think this works for both my use case and yours, correct?

Quoted 3 lines

Then identify common pattern and from there try to build a sane API (if any such thing exist :)) rather than trying today to build the whole house from the ground up with just a foggy idea of how it should looks in the end.

Yea, I do see your point. My worry is that if I define an API, and you define an API, we'll end up in two different places with people using our different APIs, then:

https://xkcd.com/927/

:)

The HMAT enabling I'm trying to do is very passive - it doesn't actively do *anything* with the memory, it's entire purpose is to give userspace more information about the memory so userspace can make informed decisions.

Would you be willing to look at the sysfs API I have defined, and see if it would work for you?

https://lkml.org/lkml/2017/7/6/749

I'll look harder at your enabling and see if we can figure out some common ground.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#38 lore raw

Re: [HMM-v25 19/19] mm/hmm: add new helper to hotplug CDM memory region v3

Quoted 70 lines · Ross Zwisler

On Tue, Sep 05, 2017 at 03:20:50PM -0400, Jerome Glisse wrote:
<>

Quoted 4 lines

Does HMAT support device hotplug ? I am unfamiliar with the whole inner working of ACPI versus PCIE. Anyway i don't see any issue with device memory also showing through HMAT but like i said device driver for the device will want to be in total control of that memory.

Yep, the HMAT will support device hotplug via the _HMA method (section 6.2.18 of ACPI 6.2). This basically supplies an entirely new HMAT that the system will use to replace the current one.

I don't yet have support for _HMA in my enabling, but I do intend to add support for it once we settle on a sysfs API for the regular boot-time case.

Quoted 23 lines

Like i said issue here is that core kernel is unaware of the device activity ie on what part of memory the device is actively working. So core mm can not make inform decision on what should be migrated to device memory. Also we do not want regular memory allocation to end in device memory unless explicitly ask for. Few reasons for that. First this memory might not only be use for compute task but also for graphic and in that case they are hard constraint on physically contiguous memory allocation that require the GPU to move thing around to make room for graphic object (can't allow GUP).

Second reasons, the device memory is inherently unreliable. If there is a bug in the device driver or the user manage to trigger a faulty condition on GPU the device might need a hard reset (ie cut PCIE power to device) which leads to loss of memory content. While GPU are becoming more and more resilient they are still prone to lockup.

Finaly for GPU there is a common pattern of memory over-commit. You pretend to each application as if they were the only one and allow each of them to allocate all of the device memory or more than could with strict sharing. As GPU have long timeslice between switching to different context/application they can easily move out and in large chunk of the process memory at context/application switching. This is have proven to be a key aspect to allow maximum performances accross several concurrent application/context.

Quoted 2 lines

To implement this easiest solution is for the device to lie about how much memory it has and use the system memory as an overflow.

I don't think any of this precludes the HMAT being involved. This is all very similar to what I think we need to do for high bandwidth memory, for example. We don't want the OS to use it for anything, and we want all of it to be available for applications to allocate and use for their specific workload. We don't want to make any assumptions about how it can or should be used.

The HMAT is just there to give us a few things:

1) It provides us with an explicit way of telling the OS not to use the memory, in the form of the "Reservation hint" flag in the Memory Subsystem Address Range Structure (ACPI 6.2 section 5.2.27.3). I expect that this will be set for persistent memory and HBM, and it sounds like you'd expect it to be set for your device memory as well.

2) It provides us with a way of telling userspace "hey, I know about some memory, and I can tell you its performance characteristics". All control of how this memory is allocated and used is still left to userspace.

Quoted 5 lines

I am not saying that NUMA is not the way forward, i am saying that as it is today it is not suited for this. It is lacking metric, it is lacking logic, it is lacking features. We could add all this but it is a lot of work and i don't feel that we have enough real world experience to do so now. I would rather have each devices grow proper infrastructure in their driver through device specific API.

To be clear, I'm not proposing that we teach the NUMA code how to automatically allocate for a given numa node, balance, etc. memory described by the HMAT. All I want is an API that says "here is some memory, I'll tell you all I can about it and let you do with it what you will", and perhaps a way to manually allocate what you want.

Quoted 5 lines

And yes, this is very hand-wavy at this point. :) After I get the sysfs portion sussed out the next step is to work on enabling something like libnuma to allow the memory to be manually allocated.

I think this works for both my use case and yours, correct?

Depend what you mean. Using NUMA as it is today no. Growing new API on the side of libnuma maybe. It is hard to say. Right now the GPU do have a very reach API see OpenCL or CUDA API. Anything less expressive than what they offer would not work.

Existing libnuma API is illsuited. It is too static. GPU workload are more dynamic as a result the virtual address space and the type of memory that is backing a range of virtual address is manage in isolation. You move range of virtual address to different memory following application programmer advice and overall resource usage.

libnuma is much more alloc some memory on some node and that's where that memory should stay. At least this is my feeling from the API (here i am not talking about mbind syscall but about userspace libnuma).

But it can grow to something more in the line of OpenCL/CUDA.

Quoted 11 lines
Quoted 3 lines

Then identify common pattern and from there try to build a sane API (if any such thing exist :)) rather than trying today to build the whole house from the ground up with just a foggy idea of how it should looks in the end.

Yea, I do see your point. My worry is that if I define an API, and you define an API, we'll end up in two different places with people using our different APIs, then:

https://xkcd.com/927/

:)

I am well aware of that :) The thing is, that API already exist and there is large chunk of what we could call legacy application that rely on those (again OpenCL and CUDA API). The easiest way is to evolve from this existing API and factor out what is constant accross devices.

Note that while the userspace API of the OpenCL/CUDA API is standardize and well define. The actual kernel API that the OpenCL/CUDA runtime use to talk with the kernel vary from one device to the others. That is why i say there is no consensus on what that API would look like. From high level (OpenCL/CUDA) sure but how this could translate into device agnostic kernel API is more fuzzy at this point.

Easiest thing would be to create kernel API with all the information that those high API offer but this would mean moving large chunk of device specific user space code into the kernel. Given that GPU device driver are already bigger on their own that all the rest of the kernel taken together, i am not sure this would be welcome (also some of this code rely on floating point operation though that can be work around).

Quoted 9 lines

The HMAT enabling I'm trying to do is very passive - it doesn't actively do *anything* with the memory, it's entire purpose is to give userspace more information about the memory so userspace can make informed decisions.

Would you be willing to look at the sysfs API I have defined, and see if it would work for you?

https://lkml.org/lkml/2017/7/6/749

For now the same kind of information about GPU memory is reported through API like OpenCL/CUDA but having one place to report all might appeal to developer. Issue becomes how to match that sysfs information with an OpenCL/CUDA device as those API are operating system agnostic.

Nonetheless i will go over the sysfs see how it could fit.

Cheers,
Jérôme

#39 lore raw

Re: [HMM-v25 19/19] mm/hmm: add new helper to hotplug CDM memory region v3

In reply to Jerome Glisse
Quoted 68 lines · Jerome Glisse
Quoted 24 lines · Bob Liu
Quoted 13 lines · Jerome Glisse
Quoted 11 lines · Bob Liu
Quoted 5 lines · Jerome Glisse

Unlike unaddressable memory, coherent device memory has a real resource associated with it on the system (as CPU can address it). Add a new helper to hotplug such memory within the HMM framework.

Got an new question, coherent device( e.g CCIX) memory are likely reported to OS through ACPI and recognized as NUMA memory node.
Then how can their memory be captured and managed by HMM framework?

Quoted 4 lines

Only platform that has such memory today is powerpc and it is not reported as regular memory by the firmware hence why they need this helper.

I don't think anyone has defined anything yet for x86 and acpi. As this is

Not yet, but now the ACPI spec has Heterogeneous Memory Attribute Table (HMAT) table defined in ACPI 6.2.
The HMAT can cover CPU-addressable memory types(though not non-cache coherent on-device memory).

Quoted 5 lines

Ross from Intel already done some work on this, see:
https://lwn.net/Articles/724562/

arm64 supports APCI also, there is likely more this kind of device when CCIX is out (should be very soon if on schedule).

HMAT is not for the same thing, AFAIK HMAT is for deep "hierarchy" memory ie when you have several kind of memory each with different characteristics:
- HBM very fast (latency) and high bandwidth, non persistent, somewhat small (ie few giga bytes)
- Persistent memory, slower (both latency and bandwidth) big (tera bytes)
- DDR (good old memory) well characteristics are between HBM and persistent

So AFAICT this has nothing to do with what HMM is for, ie device memory. Note that device memory can have a hierarchy of memory themself (HBM, GDDR and in maybe even persistent memory).

Quoted 10 lines
Quoted 6 lines

memory on PCIE like interface then i don't expect it to be reported as NUMA memory node but as io range like any regular PCIE resources. Device driver through capabilities flags would then figure out if the link between the device and CPU is CCIX capable if so it can use this helper to hotplug it as device memory.

From my point of view, Cache coherent device memory will popular soon and reported through ACPI/UEFI. Extending NUMA policy still sounds more reasonable to me.

Cache coherent device will be reported through standard mecanisms defined by the bus standard they are using. To my knowledge all the standard are either on top of PCIE or are similar to PCIE.

It is true that on many platform PCIE resource is manage/initialize by the bios (UEFI) but it is platform specific. In some case we reprogram what the bios pick.

So like i was saying i don't expect the BIOS/UEFI to report device memory as regular memory. It will be reported as a regular PCIE resources and then the device driver will be able to determine through some flags if the link between the CPU(s) and the device is cache coherent or not. At that point the device driver can use register it with HMM helper.

Quoted 4 lines

The whole NUMA discussion happen several time in the past i suggest looking on mm list archive for them. But it was rule out for several reasons. Top of my head:
- people hate CPU less node and device memory is inherently CPU less

With the introduction of the HMAT in ACPI 6.2 one of the things that was added was the ability to have an ACPI proximity domain that isn't associated with a CPU. This can be seen in the changes in the text of the "Proximity Domain" field in table 5-73 which describes the "Memory Affinity Structure". One of the major features of the HMAT was the separation of "Initiator" proximity domains (CPUs, devices that initiate memory transfers), and "target" proximity domains (memory regions, be they attached to a CPU or some other device).

ACPI proximity domains map directly to Linux NUMA nodes, so I think we're already in a place where we have to support CPU-less NUMA nodes.

Quoted 2 lines

- device driver want total control over memory and thus to be isolated from mm mecanism and doing all those special cases was not welcome

I agree that the kernel doesn't have enough information to be able to accurately handle all the use cases for the various types of heterogeneous
memory. The goal of my HMAT enabling is to allow that memory to be reserved
from kernel use via the "Reservation Hint" in the HMAT's Memory Subsystem Address Range Structure, then provide userspace with enough information to be able to distinguish between the various types of memory in the system so it can allocate & utilize it appropriately.

Quoted 9 lines

- existing NUMA migration mecanism are ill suited for this memory as access by the device to the memory is unknown to core mm and there is no easy way to report it or track it (this kind of depends on the platform and hardware)

I am likely missing other big points.

Cheers,
Jerome

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#40 lore raw

Re: [HMM-v25 19/19] mm/hmm: add new helper to hotplug CDM memory region v3

Quoted 88 lines · Ross Zwisler
Quoted 68 lines · Jerome Glisse
Quoted 24 lines · Bob Liu
Quoted 13 lines · Jerome Glisse
Quoted 11 lines · Bob Liu
Quoted 5 lines · Jerome Glisse

Unlike unaddressable memory, coherent device memory has a real resource associated with it on the system (as CPU can address it). Add a new helper to hotplug such memory within the HMM framework.

Got an new question, coherent device( e.g CCIX) memory are likely reported to OS through ACPI and recognized as NUMA memory node.
Then how can their memory be captured and managed by HMM framework?

Quoted 4 lines

Only platform that has such memory today is powerpc and it is not reported as regular memory by the firmware hence why they need this helper.

I don't think anyone has defined anything yet for x86 and acpi. As this is

Not yet, but now the ACPI spec has Heterogeneous Memory Attribute Table (HMAT) table defined in ACPI 6.2.
The HMAT can cover CPU-addressable memory types(though not non-cache coherent on-device memory).

Quoted 5 lines

Ross from Intel already done some work on this, see:
https://lwn.net/Articles/724562/

arm64 supports APCI also, there is likely more this kind of device when CCIX is out (should be very soon if on schedule).

HMAT is not for the same thing, AFAIK HMAT is for deep "hierarchy" memory ie when you have several kind of memory each with different characteristics:
- HBM very fast (latency) and high bandwidth, non persistent, somewhat small (ie few giga bytes)
- Persistent memory, slower (both latency and bandwidth) big (tera bytes)
- DDR (good old memory) well characteristics are between HBM and persistent

So AFAICT this has nothing to do with what HMM is for, ie device memory. Note that device memory can have a hierarchy of memory themself (HBM, GDDR and in maybe even persistent memory).

Quoted 10 lines
Quoted 6 lines

memory on PCIE like interface then i don't expect it to be reported as NUMA memory node but as io range like any regular PCIE resources. Device driver through capabilities flags would then figure out if the link between the device and CPU is CCIX capable if so it can use this helper to hotplug it as device memory.

From my point of view, Cache coherent device memory will popular soon and reported through ACPI/UEFI. Extending NUMA policy still sounds more reasonable to me.

Cache coherent device will be reported through standard mecanisms defined by the bus standard they are using. To my knowledge all the standard are either on top of PCIE or are similar to PCIE.

It is true that on many platform PCIE resource is manage/initialize by the bios (UEFI) but it is platform specific. In some case we reprogram what the bios pick.

So like i was saying i don't expect the BIOS/UEFI to report device memory as regular memory. It will be reported as a regular PCIE resources and then the device driver will be able to determine through some flags if the link between the CPU(s) and the device is cache coherent or not. At that point the device driver can use register it with HMM helper.

Quoted 4 lines

The whole NUMA discussion happen several time in the past i suggest looking on mm list archive for them. But it was rule out for several reasons. Top of my head:
- people hate CPU less node and device memory is inherently CPU less

With the introduction of the HMAT in ACPI 6.2 one of the things that was added was the ability to have an ACPI proximity domain that isn't associated with a CPU. This can be seen in the changes in the text of the "Proximity Domain" field in table 5-73 which describes the "Memory Affinity Structure". One of the major features of the HMAT was the separation of "Initiator" proximity domains (CPUs, devices that initiate memory transfers), and "target" proximity domains (memory regions, be they attached to a CPU or some other device).

ACPI proximity domains map directly to Linux NUMA nodes, so I think we're already in a place where we have to support CPU-less NUMA nodes.

Quoted 2 lines

- device driver want total control over memory and thus to be isolated from mm mecanism and doing all those special cases was not welcome

Quoted 8 lines

I agree that the kernel doesn't have enough information to be able to accurately handle all the use cases for the various types of heterogeneous
memory. The goal of my HMAT enabling is to allow that memory to be reserved
from kernel use via the "Reservation Hint" in the HMAT's Memory Subsystem Address Range Structure, then provide userspace with enough information to be able to distinguish between the various types of memory in the system so it can allocate & utilize it appropriately.

Does this mean require an user space memory management library to deal with all alloc/free/defragment.. But how to do with virtual <-> physical address mapping from userspace?

--
Regards,
Bob Liu

Quoted 9 lines
Quoted 9 lines

- existing NUMA migration mecanism are ill suited for this memory as access by the device to the memory is unknown to core mm and there is no easy way to report it or track it (this kind of depends on the platform and hardware)

I am likely missing other big points.

Cheers,
Jerome

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#41 lore raw

Re: [HMM-v25 19/19] mm/hmm: add new helper to hotplug CDM memory region v3

Quoted 98 lines · Bob Liu
Quoted 88 lines · Ross Zwisler
Quoted 68 lines · Jerome Glisse
Quoted 24 lines · Bob Liu
Quoted 13 lines · Jerome Glisse
Quoted 11 lines · Bob Liu
Quoted 5 lines · Jerome Glisse

Unlike unaddressable memory, coherent device memory has a real resource associated with it on the system (as CPU can address it). Add a new helper to hotplug such memory within the HMM framework.

Got an new question, coherent device( e.g CCIX) memory are likely reported to OS through ACPI and recognized as NUMA memory node.
Then how can their memory be captured and managed by HMM framework?

Quoted 4 lines

Only platform that has such memory today is powerpc and it is not reported as regular memory by the firmware hence why they need this helper.

I don't think anyone has defined anything yet for x86 and acpi. As this is

Not yet, but now the ACPI spec has Heterogeneous Memory Attribute Table (HMAT) table defined in ACPI 6.2.
The HMAT can cover CPU-addressable memory types(though not non-cache coherent on-device memory).

Quoted 5 lines

Ross from Intel already done some work on this, see:
https://lwn.net/Articles/724562/

arm64 supports APCI also, there is likely more this kind of device when CCIX is out (should be very soon if on schedule).

HMAT is not for the same thing, AFAIK HMAT is for deep "hierarchy" memory ie when you have several kind of memory each with different characteristics:
- HBM very fast (latency) and high bandwidth, non persistent, somewhat small (ie few giga bytes)
- Persistent memory, slower (both latency and bandwidth) big (tera bytes)
- DDR (good old memory) well characteristics are between HBM and persistent

So AFAICT this has nothing to do with what HMM is for, ie device memory. Note that device memory can have a hierarchy of memory themself (HBM, GDDR and in maybe even persistent memory).

Quoted 10 lines
Quoted 6 lines

memory on PCIE like interface then i don't expect it to be reported as NUMA memory node but as io range like any regular PCIE resources. Device driver through capabilities flags would then figure out if the link between the device and CPU is CCIX capable if so it can use this helper to hotplug it as device memory.

From my point of view, Cache coherent device memory will popular soon and reported through ACPI/UEFI. Extending NUMA policy still sounds more reasonable to me.

Cache coherent device will be reported through standard mecanisms defined by the bus standard they are using. To my knowledge all the standard are either on top of PCIE or are similar to PCIE.

It is true that on many platform PCIE resource is manage/initialize by the bios (UEFI) but it is platform specific. In some case we reprogram what the bios pick.

So like i was saying i don't expect the BIOS/UEFI to report device memory as regular memory. It will be reported as a regular PCIE resources and then the device driver will be able to determine through some flags if the link between the CPU(s) and the device is cache coherent or not. At that point the device driver can use register it with HMM helper.

Quoted 4 lines

The whole NUMA discussion happen several time in the past i suggest looking on mm list archive for them. But it was rule out for several reasons. Top of my head:
- people hate CPU less node and device memory is inherently CPU less

With the introduction of the HMAT in ACPI 6.2 one of the things that was added was the ability to have an ACPI proximity domain that isn't associated with a CPU. This can be seen in the changes in the text of the "Proximity Domain" field in table 5-73 which describes the "Memory Affinity Structure". One of the major features of the HMAT was the separation of "Initiator" proximity domains (CPUs, devices that initiate memory transfers), and "target" proximity domains (memory regions, be they attached to a CPU or some other device).

ACPI proximity domains map directly to Linux NUMA nodes, so I think we're already in a place where we have to support CPU-less NUMA nodes.

Quoted 2 lines

- device driver want total control over memory and thus to be isolated from mm mecanism and doing all those special cases was not welcome

Quoted 8 lines

I agree that the kernel doesn't have enough information to be able to accurately handle all the use cases for the various types of heterogeneous
memory. The goal of my HMAT enabling is to allow that memory to be reserved
from kernel use via the "Reservation Hint" in the HMAT's Memory Subsystem Address Range Structure, then provide userspace with enough information to be able to distinguish between the various types of memory in the system so it can allocate & utilize it appropriately.

Quoted 3 lines

Does this mean require an user space memory management library to deal with all alloc/free/defragment.. But how to do with virtual <-> physical address mapping from userspace?

For HMM each process give hint (somewhat similar to mbind) for range of virtual address to the device kernel driver (through some API like OpenCL or CUDA for GPU for instance). All this being device driver specific ioctl.

The kernel device driver have an overall view of all the process that use the device and each of the memory advise they gave. From that informations the kernel device driver decide what part of each process address space to migrate to device memory. This obviously dynamic and likely to change over the process lifetime.

My understanding is that HMAT want similar API to allow process to give direction on where each range of virtual address should be allocated. It is expected that most software can easily infer what part of its address will need more bandwidth, smaller latency versus what part is sparsely accessed ...

For HMAT i think first target is HBM and persistent memory and device memory might be added latter if that make sense.

Cheers,
Jerome

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#42 lore raw

Re: [HMM-v25 19/19] mm/hmm: add new helper to hotplug CDM memory region v3

Quoted 103 lines · Jerome Glisse
Quoted 98 lines · Bob Liu
Quoted 88 lines · Ross Zwisler
Quoted 68 lines · Jerome Glisse
Quoted 24 lines · Bob Liu
Quoted 13 lines · Jerome Glisse
Quoted 11 lines · Bob Liu
Quoted 5 lines · Jerome Glisse

Unlike unaddressable memory, coherent device memory has a real resource associated with it on the system (as CPU can address it). Add a new helper to hotplug such memory within the HMM framework.

Got an new question, coherent device( e.g CCIX) memory are likely reported to OS through ACPI and recognized as NUMA memory node.
Then how can their memory be captured and managed by HMM framework?

Quoted 4 lines

Only platform that has such memory today is powerpc and it is not reported as regular memory by the firmware hence why they need this helper.

I don't think anyone has defined anything yet for x86 and acpi. As this is

Not yet, but now the ACPI spec has Heterogeneous Memory Attribute Table (HMAT) table defined in ACPI 6.2.
The HMAT can cover CPU-addressable memory types(though not non-cache coherent on-device memory).

Quoted 5 lines

Ross from Intel already done some work on this, see:
https://lwn.net/Articles/724562/

arm64 supports APCI also, there is likely more this kind of device when CCIX is out (should be very soon if on schedule).

HMAT is not for the same thing, AFAIK HMAT is for deep "hierarchy" memory ie when you have several kind of memory each with different characteristics:
- HBM very fast (latency) and high bandwidth, non persistent, somewhat small (ie few giga bytes)
- Persistent memory, slower (both latency and bandwidth) big (tera bytes)
- DDR (good old memory) well characteristics are between HBM and persistent

So AFAICT this has nothing to do with what HMM is for, ie device memory. Note that device memory can have a hierarchy of memory themself (HBM, GDDR and in maybe even persistent memory).

Quoted 10 lines
Quoted 6 lines

memory on PCIE like interface then i don't expect it to be reported as NUMA memory node but as io range like any regular PCIE resources. Device driver through capabilities flags would then figure out if the link between the device and CPU is CCIX capable if so it can use this helper to hotplug it as device memory.

From my point of view, Cache coherent device memory will popular soon and reported through ACPI/UEFI. Extending NUMA policy still sounds more reasonable to me.

Cache coherent device will be reported through standard mecanisms defined by the bus standard they are using. To my knowledge all the standard are either on top of PCIE or are similar to PCIE.

It is true that on many platform PCIE resource is manage/initialize by the bios (UEFI) but it is platform specific. In some case we reprogram what the bios pick.

So like i was saying i don't expect the BIOS/UEFI to report device memory as regular memory. It will be reported as a regular PCIE resources and then the device driver will be able to determine through some flags if the link between the CPU(s) and the device is cache coherent or not. At that point the device driver can use register it with HMM helper.

Quoted 4 lines

The whole NUMA discussion happen several time in the past i suggest looking on mm list archive for them. But it was rule out for several reasons. Top of my head:
- people hate CPU less node and device memory is inherently CPU less

With the introduction of the HMAT in ACPI 6.2 one of the things that was added was the ability to have an ACPI proximity domain that isn't associated with a CPU. This can be seen in the changes in the text of the "Proximity Domain" field in table 5-73 which describes the "Memory Affinity Structure". One of the major features of the HMAT was the separation of "Initiator" proximity domains (CPUs, devices that initiate memory transfers), and "target" proximity domains (memory regions, be they attached to a CPU or some other device).

ACPI proximity domains map directly to Linux NUMA nodes, so I think we're already in a place where we have to support CPU-less NUMA nodes.

Quoted 2 lines

- device driver want total control over memory and thus to be isolated from mm mecanism and doing all those special cases was not welcome

Quoted 8 lines

I agree that the kernel doesn't have enough information to be able to accurately handle all the use cases for the various types of heterogeneous
memory. The goal of my HMAT enabling is to allow that memory to be reserved
from kernel use via the "Reservation Hint" in the HMAT's Memory Subsystem Address Range Structure, then provide userspace with enough information to be able to distinguish between the various types of memory in the system so it can allocate & utilize it appropriately.

Quoted 3 lines

Does this mean require an user space memory management library to deal with all alloc/free/defragment.. But how to do with virtual <-> physical address mapping from userspace?

Quoted 7 lines

For HMM each process give hint (somewhat similar to mbind) for range of virtual address to the device kernel driver (through some API like OpenCL or CUDA for GPU for instance). All this being device driver specific ioctl.

The kernel device driver have an overall view of all the process that use the device and each of the memory advise they gave. From that informations the kernel device driver decide what part of each process address space to migrate to device memory.

Oh, I mean CDM-HMM. I'm fine with HMM.

Quoted 5 lines

This obviously dynamic and likely to change over the process lifetime.

My understanding is that HMAT want similar API to allow process to give direction on where each range of virtual address should be allocated. It is expected that most

Right, but not clear who should manage the physical memory allocation and setup the pagetable mapping. An new driver or the kernel?

Quoted 6 lines

software can easily infer what part of its address will need more bandwidth, smaller latency versus what part is sparsely accessed ...

For HMAT i think first target is HBM and persistent memory and device memory might be added latter if that make sense.

Okay, so there are two potential ways for CPU-addressable cache-coherent device memory (or cpu-less numa memory or "target domain" memory in ACPI spec )?
1. CDM-HMM
2. HMAT

--
Regards,
Bob Liu

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#43 lore raw

Re: [HMM-v25 19/19] mm/hmm: add new helper to hotplug CDM memory region v3

Quoted 190 lines · Jerome Glisse
Quoted 148 lines · Bob Liu
Quoted 135 lines · Ross Zwisler
Quoted 121 lines · Jerome Glisse
Quoted 92 lines · Bob Liu
Quoted 27 lines · Jerome Glisse
Quoted 14 lines · Bob Liu
Quoted 7 lines · Jérôme Glisse
Quoted 5 lines

Unlike unaddressable memory, coherent device memory has a real resource associated with it on the system (as CPU can address it). Add a new helper to hotplug such memory within the HMM framework.

Quoted 5 lines

Got an new question, coherent device( e.g CCIX) memory are likely reported to OS
through ACPI and recognized as NUMA memory node.
Then how can their memory be captured and managed by HMM framework?

Quoted 6 lines

Only platform that has such memory today is powerpc and it is not reported
as regular memory by the firmware hence why they need this helper.

I don't think anyone has defined anything yet for x86 and acpi. As this is

Not yet, but now the ACPI spec has Heterogeneous Memory Attribute Table (HMAT) table defined in ACPI 6.2.
The HMAT can cover CPU-addressable memory types(though not non-cache coherent on-device memory).

Quoted 6 lines

Ross from Intel already done some work on this, see:
https://lwn.net/Articles/724562/

arm64 supports APCI also, there is likely more this kind of device when CCIX
is out (should be very soon if on schedule).

HMAT is not for the same thing, AFAIK HMAT is for deep "hierarchy" memory ie
when you have several kind of memory each with different characteristics:
- HBM very fast (latency) and high bandwidth, non persistent, somewhat small (ie few giga bytes)
- Persistent memory, slower (both latency and bandwidth) big (tera bytes)
- DDR (good old memory) well characteristics are between HBM and persistent

So AFAICT this has nothing to do with what HMM is for, ie device memory. Note
that device memory can have a hierarchy of memory themself (HBM, GDDR and in
maybe even persistent memory).

Quoted 11 lines
Quoted 10 lines

memory on PCIE like interface then i don't expect it to be reported as NUMA
memory node but as io range like any regular PCIE resources. Device driver
through capabilities flags would then figure out if the link between the
device and CPU is CCIX capable if so it can use this helper to hotplug it
as device memory.

Quoted 5 lines

From my point of view, Cache coherent device memory will popular soon and
reported through ACPI/UEFI. Extending NUMA policy still sounds more reasonable
to me.

Cache coherent device will be reported through standard mecanisms defined by
the bus standard they are using. To my knowledge all the standard are either
on top of PCIE or are similar to PCIE.

It is true that on many platform PCIE resource is manage/initialize by the
bios (UEFI) but it is platform specific. In some case we reprogram what the
bios pick.

So like i was saying i don't expect the BIOS/UEFI to report device memory as
regular memory. It will be reported as a regular PCIE resources and then the
device driver will be able to determine through some flags if the link between
the CPU(s) and the device is cache coherent or not. At that point the device
driver can use register it with HMM helper.

Quoted 6 lines

The whole NUMA discussion happen several time in the past i suggest looking
on mm list archive for them. But it was rule out for several reasons. Top of
my head:
- people hate CPU less node and device memory is inherently CPU less

With the introduction of the HMAT in ACPI 6.2 one of the things that was added
was the ability to have an ACPI proximity domain that isn't associated with a
CPU. This can be seen in the changes in the text of the "Proximity Domain"
field in table 5-73 which describes the "Memory Affinity Structure". One of
the major features of the HMAT was the separation of "Initiator" proximity
domains (CPUs, devices that initiate memory transfers), and "target" proximity
domains (memory regions, be they attached to a CPU or some other device).

ACPI proximity domains map directly to Linux NUMA nodes, so I think we're already in a place where we have to support CPU-less NUMA nodes.

Quoted 3 lines

- device driver want total control over memory and thus to be isolated from
mm mecanism and doing all those special cases was not welcome

Quoted 12 lines

I agree that the kernel doesn't have enough information to be able to accurately handle all the use cases for the various types of heterogeneous
memory. The goal of my HMAT enabling is to allow that memory to be
reserved
from kernel use via the "Reservation Hint" in the HMAT's Memory Subsystem Address Range Structure, then provide userspace with enough information to be
able to distinguish between the various types of memory in the system so it
can allocate & utilize it appropriately.

Quoted 5 lines

Does this mean require an user space memory management library to deal with all
alloc/free/defragment.. But how to do with virtual <-> physical address mapping
from userspace?

For HMM each process give hint (somewhat similar to mbind) for range of virtual
address to the device kernel driver (through some API like OpenCL or CUDA for GPU
for instance). All this being device driver specific ioctl.

Quoted 6 lines

The kernel device driver have an overall view of all the process that use the device
and each of the memory advise they gave. From that informations the kernel device
driver decide what part of each process address space to migrate to device memory.

Oh, I mean CDM-HMM. I'm fine with HMM.

Quoted 7 lines

This obviously dynamic and likely to change over the process lifetime.

My understanding is that HMAT want similar API to allow process to give direction on
where each range of virtual address should be allocated. It is expected that most

Right, but not clear who should manage the physical memory allocation and setup the
pagetable mapping. An new driver or the kernel?

Quoted 8 lines

software can easily infer what part of its address will need more bandwidth, smaller
latency versus what part is sparsely accessed ...

For HMAT i think first target is HBM and persistent memory and device memory might
be added latter if that make sense.

Okay, so there are two potential ways for CPU-addressable cache-coherent device memory
(or cpu-less numa memory or "target domain" memory in ACPI spec )?
1. CDM-HMM
2. HMAT

--
Regards,
Bob Liu

#44 lore raw

Re: [HMM-v25 19/19] mm/hmm: add new helper to hotplug CDM memory region v3

In reply to Bob Liu
Quoted 8 lines · Jerome Glisse
Quoted 7 lines · Bob Liu
Quoted 6 lines · Ross Zwisler
Quoted 5 lines · Jerome Glisse
Quoted 4 lines · Bob Liu
Quoted 3 lines · Jerome Glisse
Quoted 2 lines · Bob Liu
Quoted 1 line · Jérôme Glisse

[...]

Quoted 10 lines
Quoted 8 lines

For HMM each process give hint (somewhat similar to mbind) for range of virtual address to the device kernel driver (through some API like OpenCL or CUDA for GPU for instance). All this being device driver specific ioctl.

The kernel device driver have an overall view of all the process that use the device and each of the memory advise they gave. From that informations the kernel device driver decide what part of each process address space to migrate to device memory.

Oh, I mean CDM-HMM. I'm fine with HMM.

They are one and the same really. In both cases HMM is just a set of helpers for device driver.

Quoted 9 lines
Quoted 6 lines

This obviously dynamic and likely to change over the process lifetime.

My understanding is that HMAT want similar API to allow process to give direction on
where each range of virtual address should be allocated. It is expected that most

Right, but not clear who should manage the physical memory allocation and setup the pagetable mapping. An new driver or the kernel?

Physical device memory is manage by the kernel device driver as it is today and has it will be tomorrow. HMM does not change that, nor does it requires any change to that.

Migrating process memory to or from device is done by the kernel through the regular page migration. HMM provides new helper for device driver to initiate such migration. There is no mechanisms like auto numa migration for the reasons i explain previously.

Kernel device driver use all knowledge it has to decide what to migrate to device memory. Nothing new here either, it is what happens today for special allocated device object and it will just happen all the same for regular mmap memory (private anonymous or mmap of a regular file of a filesystem).

So every low level thing happen in the kernel. Userspace only provides directive to the kernel device driver through device specific API. But the kernel device driver can ignore or override those directive.

Quoted 9 lines
Quoted 8 lines

software can easily infer what part of its address will need more bandwidth, smaller
latency versus what part is sparsely accessed ...

For HMAT i think first target is HBM and persistent memory and device memory might
be added latter if that make sense.

Quoted 5 lines

Okay, so there are two potential ways for CPU-addressable cache-coherent device memory
(or cpu-less numa memory or "target domain" memory in ACPI spec )?
1. CDM-HMM
2. HMAT

No this are 2 orthogonal thing, they do not conflict with each others quite the contrary. HMM (the CDM part is no different) is a set of helpers, see it as a toolbox, for device driver.

HMAT is a way for firmware to report memory resources with more informations that just range of physical address. HMAT is specific to platform that rely on ACPI. HMAT does not provide any helpers to manage these memory.

So a device driver can get informations about device memory from HMAT and then use HMM to help in managing and using this memory.

Jérôme

#45 lore raw

Re: [HMM-v25 19/19] mm/hmm: add new helper to hotplug CDM memory region v3

Quoted 32 lines · Jerome Glisse
Quoted 8 lines · Jerome Glisse
Quoted 7 lines · Bob Liu
Quoted 6 lines · Ross Zwisler
Quoted 5 lines · Jerome Glisse
Quoted 4 lines · Bob Liu
Quoted 3 lines · Jerome Glisse
Quoted 2 lines · Bob Liu
Quoted 1 line · JA(C)rA'me Glisse

[...]

Quoted 10 lines
Quoted 8 lines

For HMM each process give hint (somewhat similar to mbind) for range of virtual address to the device kernel driver (through some API like OpenCL or CUDA for GPU for instance). All this being device driver specific ioctl.

The kernel device driver have an overall view of all the process that use the device and each of the memory advise they gave. From that informations the kernel device driver decide what part of each process address space to migrate to device memory.

Oh, I mean CDM-HMM. I'm fine with HMM.

They are one and the same really. In both cases HMM is just a set of helpers for device driver.

Quoted 7 lines
Quoted 6 lines

This obviously dynamic and likely to change over the process lifetime.

My understanding is that HMAT want similar API to allow process to give direction on
where each range of virtual address should be allocated. It is expected that most

Quoted 7 lines
Quoted 2 lines

Right, but not clear who should manage the physical memory allocation and setup the pagetable mapping. An new driver or the kernel?

Physical device memory is manage by the kernel device driver as it is today and has it will be tomorrow. HMM does not change that, nor does it requires any change to that.

Can someone from Intel give more information about the plan of managing HMAT reported memory?

Quoted 35 lines

Migrating process memory to or from device is done by the kernel through the regular page migration. HMM provides new helper for device driver to initiate such migration. There is no mechanisms like auto numa migration for the reasons i explain previously.

Kernel device driver use all knowledge it has to decide what to migrate to device memory. Nothing new here either, it is what happens today for special allocated device object and it will just happen all the same for regular mmap memory (private anonymous or mmap of a regular file of a filesystem).

So every low level thing happen in the kernel. Userspace only provides directive to the kernel device driver through device specific API. But the kernel device driver can ignore or override those directive.

Quoted 9 lines
Quoted 8 lines

software can easily infer what part of its address will need more bandwidth, smaller
latency versus what part is sparsely accessed ...

For HMAT i think first target is HBM and persistent memory and device memory might
be added latter if that make sense.

Quoted 5 lines

Okay, so there are two potential ways for CPU-addressable cache-coherent device memory
(or cpu-less numa memory or "target domain" memory in ACPI spec )?
1. CDM-HMM
2. HMAT

No this are 2 orthogonal thing, they do not conflict with each others quite the contrary. HMM (the CDM part is no different) is a set of helpers, see it as a toolbox, for device driver.

Quoted 7 lines

HMAT is a way for firmware to report memory resources with more informations that just range of physical address. HMAT is specific to platform that rely on ACPI. HMAT does not provide any helpers to manage these memory.

So a device driver can get informations about device memory from HMAT and then use HMM to help in managing and using this memory.

Yes, but as Balbir mentioned requires :
1. Don't online the memory as a NUMA node
2. Use the HMM-CDM API's to map the memory to ZONE DEVICE via the driver

And I'm not sure whether Intel going to use this HMM-CDM based method for their "target domain" memory ?
Or they prefer to NUMA approach? Rossi 1/4 ? Dan?

--
Thanks,
Bob Liu

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#46 lore raw

Re: [HMM-v25 19/19] mm/hmm: add new helper to hotplug CDM memory region v3

Quoted 1 line · Bob Liu

On 2017/9/8 1:27, Jerome Glisse wrote:

[..]

Quoted 12 lines
Quoted 11 lines

No this are 2 orthogonal thing, they do not conflict with each others quite the contrary. HMM (the CDM part is no different) is a set of helpers, see it as a toolbox, for device driver.

HMAT is a way for firmware to report memory resources with more informations that just range of physical address. HMAT is specific to platform that rely on ACPI. HMAT does not provide any helpers to manage these memory.

So a device driver can get informations about device memory from HMAT and then use HMM to help in managing and using this memory.

Quoted 6 lines

Yes, but as Balbir mentioned requires :
1. Don't online the memory as a NUMA node
2. Use the HMM-CDM API's to map the memory to ZONE DEVICE via the driver

And I'm not sure whether Intel going to use this HMM-CDM based method for their "target domain" memory ?
Or they prefer to NUMA approach? Ross? Dan?

The starting / strawman proposal for performance differentiated memory ranges is to get platform firmware to mark them reserved by default. Then, after we parse the HMAT, make them available via the device-dax mechanism so that applications that need 100% guaranteed access to these potentially high-value / limited-capacity ranges can be sure to get them by default, i.e. before any random kernel objects are placed in them. Otherwise, if there are no dedicated users for the memory ranges via device-dax, or they don't need the total capacity, we want to hotplug that memory into the general purpose memory allocator with a numa node number so typical numactl and memory-management flows are enabled.

Ideally this would not be specific to HMAT and any agent that knows differentiated performance characteristics of a memory range could use this scheme.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#47 lore raw

Re: [HMM-v25 19/19] mm/hmm: add new helper to hotplug CDM memory region v3

Quoted 34 lines · Dan Williams
Quoted 1 line · Bob Liu

On 2017/9/8 1:27, Jerome Glisse wrote:

[..]

Quoted 12 lines
Quoted 11 lines

No this are 2 orthogonal thing, they do not conflict with each others quite the contrary. HMM (the CDM part is no different) is a set of helpers, see it as a toolbox, for device driver.

HMAT is a way for firmware to report memory resources with more informations that just range of physical address. HMAT is specific to platform that rely on ACPI. HMAT does not provide any helpers to manage these memory.

So a device driver can get informations about device memory from HMAT and then use HMM to help in managing and using this memory.

Quoted 6 lines

Yes, but as Balbir mentioned requires :
1. Don't online the memory as a NUMA node
2. Use the HMM-CDM API's to map the memory to ZONE DEVICE via the driver

And I'm not sure whether Intel going to use this HMM-CDM based method for their "target domain" memory ?
Or they prefer to NUMA approach? Ross? Dan?

The starting / strawman proposal for performance differentiated memory ranges is to get platform firmware to mark them reserved by default. Then, after we parse the HMAT, make them available via the device-dax mechanism so that applications that need 100% guaranteed access to these potentially high-value / limited-capacity ranges can be sure to get them by default, i.e. before any random kernel objects are placed in them. Otherwise, if there are no dedicated users for the memory ranges via device-dax, or they don't need the total capacity, we want to hotplug that memory into the general purpose memory allocator with a numa node number so typical numactl and memory-management flows are enabled.

Quoted 3 lines

Ideally this would not be specific to HMAT and any agent that knows differentiated performance characteristics of a memory range could use this scheme.

@Dan/Ross

With this approach, in a SVM environment, if you would want a PRI(page grant) request to get satisfied from this HMAT-indexed memory node, then do you think we could make that happen. If yes, is that something you guys are currently working on.

Chetan

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#48 lore raw

Re: [HMM-v25 19/19] mm/hmm: add new helper to hotplug CDM memory region v3

In reply to Jerome Glisse
Quoted 16 lines · Jerome Glisse
Quoted 11 lines · Bob Liu
Quoted 5 lines · Jérôme Glisse

Unlike unaddressable memory, coherent device memory has a real resource associated with it on the system (as CPU can address it). Add a new helper to hotplug such memory within the HMM framework.

Got an new question, coherent device( e.g CCIX) memory are likely reported to OS through ACPI and recognized as NUMA memory node.
Then how can their memory be captured and managed by HMM framework?

Only platform that has such memory today is powerpc and it is not reported as regular memory by the firmware hence why they need this helper.

Quoted 6 lines

I don't think anyone has defined anything yet for x86 and acpi. As this is memory on PCIE like interface then i don't expect it to be reported as NUMA memory node but as io range like any regular PCIE resources. Device driver through capabilities flags would then figure out if the link between the device and CPU is CCIX capable if so it can use this helper to hotplug it as device memory.

Yep, the arch needs to do the right thing at hotplug time, which is

1. Don't online the memory as a NUMA node
2. Use the HMM-CDM API's to map the memory to ZONE DEVICE via the driver

Like Jerome said and we tried as well, the NUMA approach needs more agreement and discussion and probable extensions

Balbir Singh

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#49 lore raw
In reply to Jérôme Glisse
Quoted 1 line · On Wed, 16 Aug 2017 20:05:29 -0400 J__r__me Glisse <[email protected]> wrote:

Heterogeneous Memory Management (HMM) (description and justification)

The patchset adds 55 kbytes to x86_64's mm/*.o and there doesn't appear to be any way of avoiding this overhead, or of avoiding whatever runtime overheads are added.

It also adds 18k to arm's mm/*.o and arm doesn't support HMM at all.

So that's all quite a lot of bloat for systems which get no benefit from the patchset. What can we do to improve this situation (a lot)?

#50 lore raw
Quoted 7 lines · Andrew Morton
Quoted 1 line · On Wed, 16 Aug 2017 20:05:29 -0400 J__r__me Glisse <[email protected]> wrote:

Heterogeneous Memory Management (HMM) (description and justification)

The patchset adds 55 kbytes to x86_64's mm/*.o and there doesn't appear to be any way of avoiding this overhead, or of avoiding whatever runtime overheads are added.

HMM have already been integrated in couple of Red Hat kernel and AFAIK there is no runtime performance issue reported. Thought the RHEL version does not use static key as Dan asked.

Quoted 5 lines

It also adds 18k to arm's mm/*.o and arm doesn't support HMM at all.

So that's all quite a lot of bloat for systems which get no benefit from the patchset. What can we do to improve this situation (a lot)?

I will look into why object file grow so much on arm. My guess is that the new migrate code is the bulk of that. I can hide the new page migration code behind a kernel configuration flag.

Cheers,
Jerome

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#51 lore raw
Quoted 14 lines · Jerome Glisse
Quoted 7 lines · Andrew Morton
Quoted 1 line · On Wed, 16 Aug 2017 20:05:29 -0400 J__r__me Glisse <[email protected]> wrote:

Heterogeneous Memory Management (HMM) (description and justification)

The patchset adds 55 kbytes to x86_64's mm/*.o and there doesn't appear to be any way of avoiding this overhead, or of avoiding whatever runtime overheads are added.

HMM have already been integrated in couple of Red Hat kernel and AFAIK there is no runtime performance issue reported. Thought the RHEL version does not use static key as Dan asked.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines

It also adds 18k to arm's mm/*.o and arm doesn't support HMM at all.

So that's all quite a lot of bloat for systems which get no benefit from the patchset. What can we do to improve this situation (a lot)?

I will look into why object file grow so much on arm. My guess is that the new migrate code is the bulk of that. I can hide the new page migration code behind a kernel configuration flag.

Shouldn't we completely disable all of it unless there is a driver in the kernel that selects it?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#52 lore raw
Quoted 20 lines · Dan Williams
Quoted 19 lines · Jerome Glisse
Quoted 7 lines · Andrew Morton
Quoted 1 line · On Wed, 16 Aug 2017 20:05:29 -0400 J__r__me Glisse <[email protected]> wrote:

Heterogeneous Memory Management (HMM) (description and justification)

The patchset adds 55 kbytes to x86_64's mm/*.o and there doesn't appear to be any way of avoiding this overhead, or of avoiding whatever runtime overheads are added.

HMM have already been integrated in couple of Red Hat kernel and AFAIK there is no runtime performance issue reported. Thought the RHEL version does not use static key as Dan asked.

Quoted 5 lines

It also adds 18k to arm's mm/*.o and arm doesn't support HMM at all.

So that's all quite a lot of bloat for systems which get no benefit from the patchset. What can we do to improve this situation (a lot)?

Quoted 6 lines
Quoted 3 lines

I will look into why object file grow so much on arm. My guess is that the new migrate code is the bulk of that. I can hide the new page migration code behind a kernel configuration flag.

Shouldn't we completely disable all of it unless there is a driver in the kernel that selects it?

At one point people asked to be able to use the new migrate helper without HMM and hence why it is not behind any HMM kconfig.

IIRC even ARM folks were interested pretty much all SOC have several DMA engine that site idle and i think people where toying with the idea of using this new helper to make use of them. But i can add a different kconfig to hide this code and if people want to use it they will have to select it.

Jerome

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#53 lore raw
Quoted 28 lines · Jerome Glisse
Quoted 20 lines · Dan Williams
Quoted 19 lines · Jerome Glisse
Quoted 7 lines · Andrew Morton
Quoted 1 line · On Wed, 16 Aug 2017 20:05:29 -0400 J__r__me Glisse <[email protected]> wrote:

Heterogeneous Memory Management (HMM) (description and justification)

The patchset adds 55 kbytes to x86_64's mm/*.o and there doesn't appear to be any way of avoiding this overhead, or of avoiding whatever runtime overheads are added.

HMM have already been integrated in couple of Red Hat kernel and AFAIK there is no runtime performance issue reported. Thought the RHEL version does not use static key as Dan asked.

Quoted 5 lines

It also adds 18k to arm's mm/*.o and arm doesn't support HMM at all.

So that's all quite a lot of bloat for systems which get no benefit from the patchset. What can we do to improve this situation (a lot)?

Quoted 6 lines
Quoted 3 lines

I will look into why object file grow so much on arm. My guess is that the new migrate code is the bulk of that. I can hide the new page migration code behind a kernel configuration flag.

Shouldn't we completely disable all of it unless there is a driver in the kernel that selects it?

Quoted 7 lines

At one point people asked to be able to use the new migrate helper without HMM and hence why it is not behind any HMM kconfig.

IIRC even ARM folks were interested pretty much all SOC have several DMA engine that site idle and i think people where toying with the idea of using this new helper to make use of them. But i can add a different kconfig to hide this code and if people want to use it they will have to select it.

If they were interested then I would expect their use case would be included in this patchset so the infrastructure and at least one consumer can land together.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#54 lore raw
In reply to Dan Williams
Quoted 20 lines · On Thu, 17 Aug 2017 14:59:20 -0700 Dan Williams <[email protected]>…
Quoted 19 lines · Jerome Glisse
Quoted 7 lines · Andrew Morton
Quoted 1 line · On Wed, 16 Aug 2017 20:05:29 -0400 J__r__me Glisse <[email protected]> wrote:

Heterogeneous Memory Management (HMM) (description and justification)

The patchset adds 55 kbytes to x86_64's mm/*.o and there doesn't appear to be any way of avoiding this overhead, or of avoiding whatever runtime overheads are added.

HMM have already been integrated in couple of Red Hat kernel and AFAIK there is no runtime performance issue reported. Thought the RHEL version does not use static key as Dan asked.

Quoted 5 lines

It also adds 18k to arm's mm/*.o and arm doesn't support HMM at all.

So that's all quite a lot of bloat for systems which get no benefit from the patchset. What can we do to improve this situation (a lot)?

Quoted 6 lines
Quoted 3 lines

I will look into why object file grow so much on arm. My guess is that the new migrate code is the bulk of that. I can hide the new page migration code behind a kernel configuration flag.

Shouldn't we completely disable all of it unless there is a driver in the kernel that selects it?

That would be typical (and nice).

I'm not sure that Red Hat's decision is a broad enough guide here. Someone who is using Linux to make a cash register or a thermostat faces different tradeoffs...

#55 lore raw
In reply to Jérôme Glisse

it mention that HMM provided two functions:

1. one is mirror the page table between CPU and a device(like GPU, FPGA), so i am confused that: duplicating the CPU page table into a device page table means

that copy the CPU page table entry into device page table? so the device can access the CPU's virtual address? and that device can access the the CPU

allocated physical memory which map into this VMA, right?

for example: VMA -> PA (CPU physical address)

mirror: fill the the PTE entry of this VMA into GPU's page table

so:

For CPU's view: it can access the PA

For GPU's view: it can access the CPU's VMA and PA

right?

2. other function is migrate CPU memory to device memory, what is the application scenario ?

some input data created by GPU and migrate back to CPU memory? use for CPU to access GPU's data?

3. function one is help the GPU to access CPU's VMA and CPU's physical memory, if CPU want to access GPU's memory, still need to

specification device driver API like IOCTL+mmap+DMA?

4. is it any example? i remember it has a dummy driver in older patchset version. i canot find in this version.

2017-08-17 8:05 GMT+08:00 Jérôme Glisse <[email protected]>:

Quoted 273 lines

Patchset is on top of git://git.cmpxchg.org/linux-mmotm.git so i test same kernel as kbuild system, git branch:

https://cgit.freedesktop.org/~glisse/linux/log/?h=hmm-v25

Change since v24 are:
- more comments and more documentations
- fix dumb mistake when registering device
- fix race when multiple concurrent device fault
- merge HMM-CDM patchset with HMM patchset to avoid unecessary code churn

The overall logic is the same. Below is the long description of what HMM is about and why. At the end of this email i describe briefly each patch and suggest reviewers for each of them.

Please consider this patchset for 4.14

Heterogeneous Memory Management (HMM) (description and justification)

Today device driver expose dedicated memory allocation API through their device file, often relying on a combination of IOCTL and mmap calls. The device can only access and use memory allocated through this API. This effectively split the program address space into object allocated for the device and useable by the device and other regular memory (malloc, mmap of a file, share memory, â) only accessible by CPU (or in a very limited way by a device by pinning memory).

Allowing different isolated component of a program to use a device thus require duplication of the input data structure using device memory allocator. This is reasonable for simple data structure (array, grid, image, â) but this get extremely complex with advance data structure (list, tree, graph, â) that rely on a web of memory pointers. This is becoming a serious limitation on the kind of work load that can be offloaded to device like GPU.

New industry standard like C++, OpenCL or CUDA are pushing to remove this barrier. This require a shared address space between GPU device and CPU so that GPU can access any memory of a process (while still obeying memory protection like read only). This kind of feature is also appearing in various other operating systems.

HMM is a set of helpers to facilitate several aspects of address space sharing and device memory management. Unlike existing sharing mechanism that rely on pining pages use by a device, HMM relies on mmu_notifier to propagate CPU page table update to device page table.

Duplicating CPU page table is only one aspect necessary for efficiently using device like GPU. GPU local memory have bandwidth in the TeraBytes/ second range but they are connected to main memory through a system bus like PCIE that is limited to 32GigaBytes/second (PCIE 4.0 16x). Thus it is necessary to allow migration of process memory from main system memory to device memory. Issue is that on platform that only have PCIE the device memory is not accessible by the CPU with the same properties as main memory (cache coherency, atomic operations, ...).

To allow migration from main memory to device memory HMM provides a set of helper to hotplug device memory as a new type of ZONE_DEVICE memory which is un-addressable by CPU but still has struct page representing it. This allow most of the core kernel logic that deals with a process memory to stay oblivious of the peculiarity of device memory.

When page backing an address of a process is migrated to device memory the CPU page table entry is set to a new specific swap entry. CPU access to such address triggers a migration back to system memory, just like if the page was swap on disk. HMM also blocks any one from pinning a ZONE_DEVICE page so that it can always be migrated back to system memory if CPU access it. Conversely HMM does not migrate to device memory any page that is pin in system memory.

To allow efficient migration between device memory and main memory a new migrate_vma() helpers is added with this patchset. It allows to leverage device DMA engine to perform the copy operation.

This feature will be use by upstream driver like nouveau mlx5 and probably other in the future (amdgpu is next suspect in line). We are actively working on nouveau and mlx5 support. To test this patchset we also worked with NVidia close source driver team, they have more resources than us to test this kind of infrastructure and also a bigger and better userspace eco-system with various real industry workload they can be use to test and profile HMM.

The expected workload is a program builds a data set on the CPU (from disk, from network, from sensors, â). Program uses GPU API (OpenCL, CUDA, ...) to give hint on memory placement for the input data and also for the output buffer. Program call GPU API to schedule a GPU job, this happens using device driver specific ioctl. All this is hidden from programmer point of view in case of C++ compiler that transparently offload some part of a program to GPU. Program can keep doing other stuff on the CPU while the GPU is crunching numbers.

It is expected that CPU will not access the same data set as the GPU while GPU is working on it, but this is not mandatory. In fact we expect some small memory object to be actively access by both GPU and CPU concurrently as synchronization channel and/or for monitoring purposes. Such object will stay in system memory and should not be bottlenecked by system bus bandwidth (rare write and read access from both CPU and GPU).

As we are relying on device driver API, HMM does not introduce any new syscall nor does it modify any existing ones. It does not change any POSIX semantics or behaviors. For instance the child after a fork of a process that is using HMM will not be impacted in anyway, nor is there any data hazard between child COW or parent COW of memory that was migrated to device prior to fork.

HMM assume a numbers of hardware features. Device must allow device page table to be updated at any time (ie device job must be preemptable). Device page table must provides memory protection such as read only. Device must track write access (dirty bit). Device must have a minimum granularity that match PAGE_SIZE (ie 4k).

Reviewer (just hint):
Patch 1 HMM documentation
Patch 2 introduce core infrastructure and definition of HMM, pretty
small patch and easy to review
Patch 3 introduce the mirror functionality of HMM, it relies on
mmu_notifier and thus someone familiar with that part would be in better position to review
Patch 4 is an helper to snapshot CPU page table while synchronizing with
concurrent page table update. Understanding mmu_notifier makes review easier.
Patch 5 is mostly a wrapper around handle_mm_fault()
Patch 6 add new add_pages() helper to avoid modifying each arch memory
hot plug function
Patch 7 add a new memory type for ZONE_DEVICE and also add all the logic
in various core mm to support this new type. Dan Williams and any core mm contributor are best people to review each half of this patchset
Patch 8 special case HMM ZONE_DEVICE pages inside put_page() Kirill and
Dan Williams are best person to review this
Patch 9 allow to uncharge a page from memory group without using the lru
list field of struct page (best reviewer: Johannes Weiner or Vladimir Davydov or Michal Hocko)
Patch 10 Add support to uncharge ZONE_DEVICE page from a memory cgroup (best
reviewer: Johannes Weiner or Vladimir Davydov or Michal Hocko)
Patch 11 add helper to hotplug un-addressable device memory as new type
of ZONE_DEVICE memory (new type introducted in patch 3 of this serie). This is boiler plate code around memory hotplug and it also pick a free range of physical address for the device memory. Note that the physical address do not point to anything (at least as far as the kernel knows).
Patch 12 introduce a new hmm_device class as an helper for device driver
that want to expose multiple device memory under a common fake device driver. This is usefull for multi-gpu configuration. Anyone familiar with device driver infrastructure can review this. Boiler plate code really.
Patch 13 add a new migrate mode. Any one familiar with page migration is
welcome to review.
Patch 14 introduce a new migration helper (migrate_vma()) that allow to
migrate a range of virtual address of a process using device DMA engine to perform the copy. It is not limited to do copy from and to device but can also do copy between any kind of source and destination memory. Again anyone familiar with migration code should be able to verify the logic.
Patch 15 optimize the new migrate_vma() by unmapping pages while we are
collecting them. This can be review by any mm folks.
Patch 16 add unaddressable memory migration to helper introduced in patch
7, this can be review by anyone familiar with migration code
Patch 17 add a feature that allow device to allocate non-present page on
the GPU when migrating a range of address to device memory. This is an helper for device driver to avoid having to first allocate system memory before migration to device memory
Patch 18 add a new kind of ZONE_DEVICE memory for cache coherent device
memory (CDM)
Patch 19 add an helper to hotplug CDM memory

Previous patchset posting :
v1 http://lwn.net/Articles/597289/
v2 https://lkml.org/lkml/2014/6/12/559
v3 https://lkml.org/lkml/2014/6/13/633
v4 https://lkml.org/lkml/2014/8/29/423
v5 https://lkml.org/lkml/2014/11/3/759
v6 http://lwn.net/Articles/619737/
v7 http://lwn.net/Articles/627316/
v8 https://lwn.net/Articles/645515/
v9 https://lwn.net/Articles/651553/
v10 https://lwn.net/Articles/654430/
v11 http://www.gossamer-threads.com/lists/linux/kernel/2286424
v12 http://www.kernelhub.org/?msg=972982&p=2
v13 https://lwn.net/Articles/706856/
v14 https://lkml.org/lkml/2016/12/8/344
v15 http://www.mail-archive.com/linux-kernel@xxxxxxxxxxxxxxx/
msg1304107.html
v16 http://www.spinics.net/lists/linux-mm/msg119814.html
v17 https://lkml.org/lkml/2017/1/27/847
v18 https://lkml.org/lkml/2017/3/16/596
v19 https://lkml.org/lkml/2017/4/5/831
v20 https://lwn.net/Articles/720715/
v21 https://lkml.org/lkml/2017/4/24/747
v22 http://lkml.iu.edu/hypermail/linux/kernel/1705.2/05176.html
v23 https://www.mail-archive.com/[email protected]/
msg1404788.html
v24 https://lwn.net/Articles/726691/

Jérôme Glisse (18):
hmm: heterogeneous memory management documentation v3 mm/hmm: heterogeneous memory management (HMM for short) v5 mm/hmm/mirror: mirror process address space on device with HMM helpers

    v3

mm/hmm/mirror: helper to snapshot CPU page table v4 mm/hmm/mirror: device page fault handler
mm/ZONE_DEVICE: new type of ZONE_DEVICE for unaddressable memory v5 mm/ZONE_DEVICE: special case put_page() for device private pages v4 mm/memcontrol: allow to uncharge page without using page->lru field mm/memcontrol: support MEMORY_DEVICE_PRIVATE v4 mm/hmm/devmem: device memory hotplug using ZONE_DEVICE v7 mm/hmm/devmem: dummy HMM device for ZONE_DEVICE memory v3 mm/migrate: new migrate mode MIGRATE_SYNC_NO_COPY mm/migrate: new memory migration helper for use with device memory v5 mm/migrate: migrate_vma() unmap page from vma while collecting pages mm/migrate: support un-addressable ZONE_DEVICE page in migration v3 mm/migrate: allow migrate_vma() to alloc new page on empty entry v4 mm/device-public-memory: device memory cache coherent with CPU v5 mm/hmm: add new helper to hotplug CDM memory region v3

Michal Hocko (1):
mm/memory_hotplug: introduce add_pages

 Documentation/vm/hmm.txt       |  384 ++++++++++++
 MAINTAINERS                    |    7 +
 arch/x86/Kconfig               |    4 +
 arch/x86/mm/init_64.c          |   22 +-
 fs/aio.c                       |    8 +
 fs/f2fs/data.c                 |    5 +-
 fs/hugetlbfs/inode.c           |    5 +-
 fs/proc/task_mmu.c             |    9 +-
 fs/ubifs/file.c                |    5 +-
 include/linux/hmm.h            |  518 ++++++++++++++++
 include/linux/ioport.h         |    2 +
 include/linux/memory_hotplug.h |   11 +
 include/linux/memremap.h       |  107 ++++
 include/linux/migrate.h        |  124 ++++
 include/linux/migrate_mode.h   |    5 +
 include/linux/mm.h             |   33 +-
 include/linux/mm_types.h       |    6 +
 include/linux/swap.h           |   24 +-
 include/linux/swapops.h        |   68 +++
 kernel/fork.c                  |    3 +
 kernel/memremap.c              |   60 +-
 mm/Kconfig                     |   47 +-
 mm/Makefile                    |    2 +-
 mm/balloon_compaction.c        |    8 +
 mm/gup.c                       |    7 +
 mm/hmm.c                       | 1273 ++++++++++++++++++++++++++++++
++++++++++
 mm/madvise.c                   |    2 +-
 mm/memcontrol.c                |  222 ++++---
 mm/memory.c                    |  107 +++-
 mm/memory_hotplug.c            |   10 +-
 mm/migrate.c                   |  928 ++++++++++++++++++++++++++++-
 mm/mprotect.c                  |   14 +
 mm/page_vma_mapped.c           |   10 +
 mm/rmap.c                      |   25 +
 mm/swap.c                      |   11 +
 mm/zsmalloc.c                  |    8 +
 36 files changed, 3964 insertions(+), 120 deletions(-)
 create mode 100644 Documentation/vm/hmm.txt
 create mode 100644 include/linux/hmm.h
 create mode 100644 mm/hmm.c

--
2.13.4

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#56 lore raw
Quoted 14 lines · Figo.zhang

it mention that HMM provided two functions:

1. one is mirror the page table between CPU and a device(like GPU, FPGA), so i am confused that: duplicating the CPU page table into a device page table means

that copy the CPU page table entry into device page table? so the device can access the CPU's virtual address? and that device can access the the CPU allocated physical memory which map into this VMA, right?

for example: VMA -> PA (CPU physical address)

mirror: fill the the PTE entry of this VMA into GPU's page table

Quoted 7 lines

so:

For CPU's view: it can access the PA

For GPU's view: it can access the CPU's VMA and PA

right?

Correct. This is for platform/device without ATS/PASID. Note that HMM only provide helpers to snapshot the CPU page table and properly synchronize with concurrent CPU page table update. Most of the code is really inside the device driver as each device driver has its own architecture and its own page table format.

Quoted 2 lines

2. other function is migrate CPU memory to device memory, what is the application scenario ?

Second part of HMM is to allow to register "special" struct page (they are not on the LRU and are associated with a device). Having struct page allow most of the kernel memory management to be oblivous to the underlying memory type (regular DDR memort or device memory).

The migrate helper introduced with HMM is to allow to migrate to and from device memory using DMA engine and not CPU memcopy. It is needed because on some platform CPU can not access the device memory and moreover DMA engine reach higher bandwidth more easily than CPU memcopy.

Again this is an helper. The policy on what to migrate, when, ... is outside HMM for now we assume that the device driver is the best place to have this logic. Maybe in few year once we have more device driver using that kind of feature we may grow common code to expose common API to userspace for migration policy.

Quoted 2 lines

some input data created by GPU and migrate back to CPU memory? use for CPU to access GPU's data?

It can be use in any number of way. So yes all the scenario you list do apply. On platform where CPU can not access device memory you need to migrate back to regular memory for CPU access.

Note that the physical memory use behind a virtual address pointer is transparent to the application thus you do not need to modify it in anyway. That is the whole point of HMM.

Quoted 3 lines

3. function one is help the GPU to access CPU's VMA and CPU's physical memory, if CPU want to access GPU's memory, still need to specification device driver API like IOCTL+mmap+DMA?

No you do not need special memory allocator with an HMM capable device (and device driver). HMM mirror functionality is to allow any pointer to point to same memory on both a device and CPU for a given application. This is the Fine-Grained system SVM as specified in the OpenCL 2.0 specification.

Basic example is without HMM:

    mul_mat_on_gpu(float *r, float *a, float *b, unsigned m)
    {
        gpu_buffer_t gpu_r, gpu_a, gpu_b;

        gpu_r = gpu_alloc(m*m*sizeof(float));
        gpu_a = gpu_alloc(m*m*sizeof(float));
        gpu_b = gpu_alloc(m*m*sizeof(float));
        gpu_copy_to(gpu_a, a, m*m*sizeof(float));
        gpu_copy_to(gpu_b, b, m*m*sizeof(float));

        gpu_mul_mat(gpu_r, gpu_a, gpu_b, m);

        gpu_copy_from(gpu_r, r, m*m*sizeof(float));
    }

With HMM:

    mul_mat_on_gpu(float *r, float *a, float *b, unsigned m)
    {
        gpu_mul_mat(r, a, b, m);
    }

So it is going from a world with device specific allocation to a model where any regular process memory (outcome of an mmap to a regular file or for anonymous private memory). can be access by both CPU and GPU using same pointer.

Now on platform like PCIE where CPU can not access the device memory in cache coherent way (also with no garanty regarding CPU atomic operations) you can not have the CPU access the device memory without breaking memory model expected by the programmer.

Hence if some range of the virtual address space of a process has been migrated to device memory it must be migrated back to regular memory.

On platform like CAPI or CCIX you do not need to migrate back to regular memory on CPU access. HMM provide helpers to handle both cases.

Quoted 2 lines

4. is it any example? i remember it has a dummy driver in older patchset version. i canot find in this version.

Dummy driver and userspace:
https://cgit.freedesktop.org/~glisse/linux/log/?h=hmm-next
https://github.com/glisse/hmm-dummy-test-suite

nouveau prototype:
https://cgit.freedesktop.org/~glisse/linux/log/?h=hmm-nouveau

I was waiting on rework of nouveau memory management before working on final implementation of HMM inside nouveau. That rework is now mostly ready:

https://github.com/skeggsb/nouveau/tree/devel-fault

I intend to start working on final HMM inside nouveau after the end of year celebration and i hope to have it in some working state in couple month. At the same time we are working on an open source userspace to make use of that (probably an OpenCL runtime first but we are looking into other thing such as OpenMP, CUDA, ...).

Plans is to upstream all this next year, all the bits are slowly cooking.

Cheers,
Jerome

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#57 lore raw

2017-12-14 0:12 GMT+08:00 Jerome Glisse <[email protected]>:

Quoted 78 lines · Figo.zhang
Quoted 8 lines

it mention that HMM provided two functions:

1. one is mirror the page table between CPU and a device(like GPU, FPGA), so i am confused that: duplicating the CPU page table into a device page table means

that copy the CPU page table entry into device page table? so the device can access the CPU's virtual address? and that device can access the the

CPU

Quoted 6 lines

allocated physical memory which map into this VMA, right?

for example: VMA -> PA (CPU physical address)

mirror: fill the the PTE entry of this VMA into GPU's page table

Quoted 7 lines

so:

For CPU's view: it can access the PA

For GPU's view: it can access the CPU's VMA and PA

right?

Correct. This is for platform/device without ATS/PASID. Note that HMM only provide helpers to snapshot the CPU page table and properly synchronize with concurrent CPU page table update. Most of the code is really inside the device driver as each device driver has its own architecture and its own page table format.

Quoted 2 lines

2. other function is migrate CPU memory to device memory, what is the application scenario ?

Second part of HMM is to allow to register "special" struct page (they are not on the LRU and are associated with a device). Having struct page allow most of the kernel memory management to be oblivous to the underlying memory type (regular DDR memort or device memory).

The migrate helper introduced with HMM is to allow to migrate to and from device memory using DMA engine and not CPU memcopy. It is needed because on some platform CPU can not access the device memory and moreover DMA engine reach higher bandwidth more easily than CPU memcopy.

Again this is an helper. The policy on what to migrate, when, ... is outside HMM for now we assume that the device driver is the best place to have this logic. Maybe in few year once we have more device driver using that kind of feature we may grow common code to expose common API to userspace for migration policy.

Quoted 1 line

some input data created by GPU and migrate back to CPU memory? use for

CPU

Quoted 1 line

to access GPU's data?

It can be use in any number of way. So yes all the scenario you list do apply. On platform where CPU can not access device memory you need to migrate back to regular memory for CPU access.

Note that the physical memory use behind a virtual address pointer is transparent to the application thus you do not need to modify it in anyway. That is the whole point of HMM.

Quoted 3 lines

3. function one is help the GPU to access CPU's VMA and CPU's physical memory, if CPU want to access GPU's memory, still need to specification device driver API like IOCTL+mmap+DMA?

No you do not need special memory allocator with an HMM capable device (and device driver). HMM mirror functionality is to allow any pointer to point to same memory on both a device and CPU for a given application. This is the Fine-Grained system SVM as specified in the OpenCL 2.0 specification.

Basic example is without HMM:

Quoted 15 lines
    mul_mat_on_gpu(float *r, float *a, float *b, unsigned m)
    {
        gpu_buffer_t gpu_r, gpu_a, gpu_b;

        gpu_r = gpu_alloc(m*m*sizeof(float));
        gpu_a = gpu_alloc(m*m*sizeof(float));
        gpu_b = gpu_alloc(m*m*sizeof(float));
        gpu_copy_to(gpu_a, a, m*m*sizeof(float));
        gpu_copy_to(gpu_b, b, m*m*sizeof(float));

        gpu_mul_mat(gpu_r, gpu_a, gpu_b, m);

        gpu_copy_from(gpu_r, r, m*m*sizeof(float));
    }

The traditional workflow is:
1. the pointer a, b and r are total point to the CPU memory
2. create/alloc three GPU buffers: gpu_a, gpu_b, gpu_r
3. copy CPU memory a and b to GPU memory gpu_b and gpu_b
4. let the GPU to do the calculation
5. copy the result from GPU buffer (gpu_r) to CPU buffer (r)

is it right?

Quoted 7 lines

With HMM:

    mul_mat_on_gpu(float *r, float *a, float *b, unsigned m)
    {
        gpu_mul_mat(r, a, b, m);
    }

with HMM workflow:
1. CPU has three buffer: a, b, r, and it is physical addr is : pa, pb, pr and GPU has tree physical buffer: gpu_a, gpu_b, gpu_r
2. GPU want to access buffer a and b, cause a GPU page fault
3. GPU report a page fault to CPU
4. CPU got a GPU page fault:
* unmap the buffer a,b,r (who do it? GPU driver?)
* copy the buffer a ,b's content to GPU physical buffers:
gpu_a, gpu_b
* fill the GPU page table entry with these pages (gpu_a,
gpu_b, gpu_r) of the CPU virtual address: a,b,r;

5. GPU do the calculation
6. CPU want to get result from buffer r and will cause a CPU page fault:
7. in CPU page fault:
* unmap the GPU page table entry for virtual address a,b,r.
(who do the unmap? GPU driver?)
* copy the GPU's buffer content (gpu_a, gpu_b, gpu_r) to
CPU buffer (abr)
* fill the CPU page table entry: virtual_addr -> buffer
(pa,pb,pr)
8. so the CPU can get the result form buffer r.

my guess workflow is right?
it seems need two copy, from CPU to GPU, and then GPU to CPU for result.
* is it CPU and GPU have the page table concurrently, so
no page fault occur?
* how about the performance? it sounds will create lots of page fault.

Quoted 49 lines

So it is going from a world with device specific allocation to a model where any regular process memory (outcome of an mmap to a regular file or for anonymous private memory). can be access by both CPU and GPU using same pointer.

Now on platform like PCIE where CPU can not access the device memory in cache coherent way (also with no garanty regarding CPU atomic operations) you can not have the CPU access the device memory without breaking memory model expected by the programmer.

Hence if some range of the virtual address space of a process has been migrated to device memory it must be migrated back to regular memory.

On platform like CAPI or CCIX you do not need to migrate back to regular memory on CPU access. HMM provide helpers to handle both cases.

Quoted 2 lines

4. is it any example? i remember it has a dummy driver in older patchset version. i canot find in this version.

Dummy driver and userspace:
https://cgit.freedesktop.org/~glisse/linux/log/?h=hmm-next
https://github.com/glisse/hmm-dummy-test-suite

nouveau prototype:
https://cgit.freedesktop.org/~glisse/linux/log/?h=hmm-nouveau

I was waiting on rework of nouveau memory management before working on final implementation of HMM inside nouveau. That rework is now mostly ready:

https://github.com/skeggsb/nouveau/tree/devel-fault

I intend to start working on final HMM inside nouveau after the end of year celebration and i hope to have it in some working state in couple month. At the same time we are working on an open source userspace to make use of that (probably an OpenCL runtime first but we are looking into other thing such as OpenMP, CUDA, ...).

Plans is to upstream all this next year, all the bits are slowly cooking.

Cheers,
Jérôme

#58 lore raw
Quoted 3 lines · Figo.zhang

2017-12-14 0:12 GMT+08:00 Jerome Glisse <[email protected]>:

Quoted 1 line · Figo.zhang

[...]

Quoted 17 lines
Quoted 16 lines

Basic example is without HMM:

    mul_mat_on_gpu(float *r, float *a, float *b, unsigned m)
    {
        gpu_buffer_t gpu_r, gpu_a, gpu_b;

        gpu_r = gpu_alloc(m*m*sizeof(float));
        gpu_a = gpu_alloc(m*m*sizeof(float));
        gpu_b = gpu_alloc(m*m*sizeof(float));
        gpu_copy_to(gpu_a, a, m*m*sizeof(float));
        gpu_copy_to(gpu_b, b, m*m*sizeof(float));

        gpu_mul_mat(gpu_r, gpu_a, gpu_b, m);

        gpu_copy_from(gpu_r, r, m*m*sizeof(float));
    }
Quoted 8 lines

The traditional workflow is:
1. the pointer a, b and r are total point to the CPU memory
2. create/alloc three GPU buffers: gpu_a, gpu_b, gpu_r
3. copy CPU memory a and b to GPU memory gpu_b and gpu_b
4. let the GPU to do the calculation
5. copy the result from GPU buffer (gpu_r) to CPU buffer (r)

is it right?

Right.

Quoted 30 lines
Quoted 6 lines

With HMM:

    mul_mat_on_gpu(float *r, float *a, float *b, unsigned m)
    {
        gpu_mul_mat(r, a, b, m);
    }

with HMM workflow:
1. CPU has three buffer: a, b, r, and it is physical addr is : pa, pb, pr and GPU has tree physical buffer: gpu_a, gpu_b, gpu_r
2. GPU want to access buffer a and b, cause a GPU page fault
3. GPU report a page fault to CPU
4. CPU got a GPU page fault:
* unmap the buffer a,b,r (who do it? GPU driver?)
* copy the buffer a ,b's content to GPU physical buffers:
gpu_a, gpu_b
* fill the GPU page table entry with these pages (gpu_a,
gpu_b, gpu_r) of the CPU virtual address: a,b,r;

5. GPU do the calculation
6. CPU want to get result from buffer r and will cause a CPU page fault:
7. in CPU page fault:
* unmap the GPU page table entry for virtual address a,b,r.
(who do the unmap? GPU driver?)
* copy the GPU's buffer content (gpu_a, gpu_b, gpu_r) to
CPU buffer (abr)
* fill the CPU page table entry: virtual_addr -> buffer
(pa,pb,pr)
8. so the CPU can get the result form buffer r.

Quoted 5 lines

my guess workflow is right?
it seems need two copy, from CPU to GPU, and then GPU to CPU for result.
* is it CPU and GPU have the page table concurrently, so
no page fault occur?
* how about the performance? it sounds will create lots of page fault.

This is not what happen. Here is the workflow with HMM mirror (note that physical address do not matter here so i do not even reference them it is all about virtual address):
1 They are 3 buffers a, b and r at given virtual address both CPU and
GPU can access them (concurently or not this does not matter).
2 GPU can fault so if any virtual address do not have a page table
entry inside the GPU page table this trigger a page fault that will call HMM mirror helper to snapshot CPU page table into the GPU page table. If there is no physical memory backing the virtual address (ie CPU page table is also empty for the given virtual address) then the regular page fault handler of the kernel is invoked.

Without HMM mirror but ATS/PASI (CCIX or CAPI):
1 They are 3 buffers a, b and r at given virtual address both CPU and
GPU can access them (concurently or not this does not matter).
2 GPU use the exact same page table as the CPU and fault exactly like
CPU on empty page table entry

So in the end with HMM mirror or ATS/PASID you get the same behavior. There is no complexity like you seem to assume. This all about virtual address. At any point in time any given valid virtual address of a process point to a given physical memory address and that physical memory address is the same on both the CPU and the GPU at any point in time they are never out of sync (both in HMM mirror and in ATS/PASID case).

The exception is for platform that do not have CAPI or CCIX property ie cache coherency for CPU access to device memory. On such platform when you migrate a virtual address to use device physical memory you update the CPU page table with a special entry. If the CPU try to access the virtual address with special entry it trigger fault and HMM will migrate the virtual address back to regular memory. But this does not apply for CAPI or CCIX platform.

Too minimize page fault the device driver is encourage to pre-fault and prepopulate its page table (the HMM mirror case). Often device driver has enough context information to guess what range of virtual address is about to be access by the device and thus pre-fault thing.

Hope this clarify thing for you.

Cheers,
Jerome

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#59 lore raw

2017-12-14 11:16 GMT+08:00 Jerome Glisse <[email protected]>:

Quoted 72 lines · Figo.zhang
Quoted 3 lines

2017-12-14 0:12 GMT+08:00 Jerome Glisse <[email protected]>:

Quoted 1 line · Figo.zhang

[...]

Quoted 17 lines
Quoted 16 lines

Basic example is without HMM:

    mul_mat_on_gpu(float *r, float *a, float *b, unsigned m)
    {
        gpu_buffer_t gpu_r, gpu_a, gpu_b;

        gpu_r = gpu_alloc(m*m*sizeof(float));
        gpu_a = gpu_alloc(m*m*sizeof(float));
        gpu_b = gpu_alloc(m*m*sizeof(float));
        gpu_copy_to(gpu_a, a, m*m*sizeof(float));
        gpu_copy_to(gpu_b, b, m*m*sizeof(float));

        gpu_mul_mat(gpu_r, gpu_a, gpu_b, m);

        gpu_copy_from(gpu_r, r, m*m*sizeof(float));
    }
Quoted 8 lines

The traditional workflow is:
1. the pointer a, b and r are total point to the CPU memory
2. create/alloc three GPU buffers: gpu_a, gpu_b, gpu_r
3. copy CPU memory a and b to GPU memory gpu_b and gpu_b
4. let the GPU to do the calculation
5. copy the result from GPU buffer (gpu_r) to CPU buffer (r)

is it right?

Right.

Quoted 30 lines
Quoted 6 lines

With HMM:

    mul_mat_on_gpu(float *r, float *a, float *b, unsigned m)
    {
        gpu_mul_mat(r, a, b, m);
    }

with HMM workflow:
1. CPU has three buffer: a, b, r, and it is physical addr is : pa, pb, pr and GPU has tree physical buffer: gpu_a, gpu_b, gpu_r
2. GPU want to access buffer a and b, cause a GPU page fault
3. GPU report a page fault to CPU
4. CPU got a GPU page fault:
* unmap the buffer a,b,r (who do it? GPU driver?)
* copy the buffer a ,b's content to GPU physical buffers:
gpu_a, gpu_b
* fill the GPU page table entry with these pages (gpu_a,
gpu_b, gpu_r) of the CPU virtual address: a,b,r;

5. GPU do the calculation
6. CPU want to get result from buffer r and will cause a CPU page fault:
7. in CPU page fault:
* unmap the GPU page table entry for virtual address a,b,r.
(who do the unmap? GPU driver?)
* copy the GPU's buffer content (gpu_a, gpu_b, gpu_r) to
CPU buffer (abr)
* fill the CPU page table entry: virtual_addr -> buffer
(pa,pb,pr)
8. so the CPU can get the result form buffer r.

Quoted 5 lines

my guess workflow is right?
it seems need two copy, from CPU to GPU, and then GPU to CPU for result.
* is it CPU and GPU have the page table concurrently, so
no page fault occur?
* how about the performance? it sounds will create lots of page fault.

Quoted 12 lines

This is not what happen. Here is the workflow with HMM mirror (note that physical address do not matter here so i do not even reference them it is all about virtual address):
1 They are 3 buffers a, b and r at given virtual address both CPU and
GPU can access them (concurently or not this does not matter).
2 GPU can fault so if any virtual address do not have a page table
entry inside the GPU page table this trigger a page fault that will call HMM mirror helper to snapshot CPU page table into the GPU page table. If there is no physical memory backing the virtual address (ie CPU page table is also empty for the given virtual address) then the regular page fault handler of the kernel is invoked.

so when HMM mirror done, the content of GPU page table entry and CPU page table entry
are same, right? so the GPU and CPU can access the same physical address, this physical
address is allocated by CPU malloc systemcall. is it conflict and race condition? CPU and GPU
write to this physical address concurrently.

i see this slides said:
http://on-demand.gputechconf.com/gtc/2017/presentation/s7764_john-hubbardgpus-using-hmm-blur-the-lines-between-cpu-and-gpu.pdf

in page 22~23:
When CPU page fault occurs:
* UM (unified memory driver) copies page data to CPU, umaps from GPU *HMM maps page to CPU

when GPU page fault occurs:
*HMM has a malloc record buffer, so UM copy page data to GPU *HMM unmaps page from CPU

so in this slides, it said it will has two copies, from CPU to GPU, and from GPU to CPU. so in this case (mul_mat_on_gpu()), is it really need two copies in kernel space?

Quoted 14 lines

Without HMM mirror but ATS/PASI (CCIX or CAPI):
1 They are 3 buffers a, b and r at given virtual address both CPU and
GPU can access them (concurently or not this does not matter).
2 GPU use the exact same page table as the CPU and fault exactly like
CPU on empty page table entry

So in the end with HMM mirror or ATS/PASID you get the same behavior. There is no complexity like you seem to assume. This all about virtual address. At any point in time any given valid virtual address of a process point to a given physical memory address and that physical memory address is the same on both the CPU and the GPU at any point in time they are never out of sync (both in HMM mirror and in ATS/PASID case).

Quoted 8 lines

The exception is for platform that do not have CAPI or CCIX property ie cache coherency for CPU access to device memory. On such platform when you migrate a virtual address to use device physical memory you update the CPU page table with a special entry. If the CPU try to access the virtual address with special entry it trigger fault and HMM will migrate the virtual address back to regular memory. But this does not apply for CAPI or CCIX platform.

the example of the virtual address using device physical memory is : gpu_r = gpu_alloc(m*m*sizeof(float)),
so CPU want to access gpu_r will trigger migrate back to CPU memory, it will allocate CPU page and copy
to gpu_r's content to CPU pages, right?

Quoted 13 lines

Too minimize page fault the device driver is encourage to pre-fault and prepopulate its page table (the HMM mirror case). Often device driver has enough context information to guess what range of virtual address is about to be access by the device and thus pre-fault thing.

Hope this clarify thing for you.

Cheers,
Jérôme

#60 lore raw
Quoted 6 lines · Figo.zhang

2017-12-14 11:16 GMT+08:00 Jerome Glisse <[email protected]>:

Quoted 4 lines · Figo.zhang
Quoted 3 lines

2017-12-14 0:12 GMT+08:00 Jerome Glisse <[email protected]>:

Quoted 1 line · Figo.zhang

[...]

Quoted 13 lines
Quoted 12 lines

This is not what happen. Here is the workflow with HMM mirror (note that physical address do not matter here so i do not even reference them it is all about virtual address):
1 They are 3 buffers a, b and r at given virtual address both CPU and
GPU can access them (concurently or not this does not matter).
2 GPU can fault so if any virtual address do not have a page table
entry inside the GPU page table this trigger a page fault that will call HMM mirror helper to snapshot CPU page table into the GPU page table. If there is no physical memory backing the virtual address (ie CPU page table is also empty for the given virtual address) then the regular page fault handler of the kernel is invoked.

Quoted 7 lines

so when HMM mirror done, the content of GPU page table entry and CPU page table entry
are same, right? so the GPU and CPU can access the same physical address, this physical
address is allocated by CPU malloc systemcall. is it conflict and race condition? CPU and GPU
write to this physical address concurrently.

Correct and yes it is conflict free. PCIE platform already support cache coherent access by device to main memory (snoop transaction in PCIE specification). Access can happen concurently to same byte and it behave exactly the same as if two CPU core try to access the same byte.

Quoted 9 lines

i see this slides said:
http://on-demand.gputechconf.com/gtc/2017/presentation/s7764_john-hubbardgpus-using-hmm-blur-the-lines-between-cpu-and-gpu.pdf

in page 22~23i 1/4 ?
When CPU page fault occurs:
* UM (unified memory driver) copies page data to CPU, umaps from GPU *HMM maps page to CPU

Quoted 7 lines

when GPU page fault occurs:
*HMM has a malloc record buffer, so UM copy page data to GPU *HMM unmaps page from CPU

so in this slides, it said it will has two copies, from CPU to GPU, and from GPU to CPU. so in this case (mul_mat_on_gpu()), is it really need two copies in kernel space?

This slide is for the case where you use device memory on PCIE platform. When that happen only the device can access the virtual address back by device memory. If CPU try to access such address a page fault is trigger and it migrate the data back to regular memory where both GPU and CPU can access it concurently.

And again this behavior only happen if you use HMM non cache coherent device memory model. If you use the device cache coherent model with HMM then CPU can access the device memory directly too and above scenario never happen.

Note that memory copy when data move from device to system or from system to device memory are inevitable. This is exactly as with autoNUMA. Also note that in some case thing can get allocated directly on GPU and never copied back to regular memory (only use by GPU and freed once GPU is done with them) the zero copy case. But i want to stress that the zero copy case is unlikely to happen for input buffer. Usualy you do not get your input data set directly on the GPU but from network or disk and you might do pre-processing on CPU (uncompress input, or do something that is better done on the CPU). Then you feed your data to the GPU and you do computation there.

Quoted 22 lines
Quoted 13 lines

Without HMM mirror but ATS/PASI (CCIX or CAPI):
1 They are 3 buffers a, b and r at given virtual address both CPU and
GPU can access them (concurently or not this does not matter).
2 GPU use the exact same page table as the CPU and fault exactly like
CPU on empty page table entry

So in the end with HMM mirror or ATS/PASID you get the same behavior. There is no complexity like you seem to assume. This all about virtual address. At any point in time any given valid virtual address of a process point to a given physical memory address and that physical memory address is the same on both the CPU and the GPU at any point in time they are never out of sync (both in HMM mirror and in ATS/PASID case).

Quoted 8 lines

The exception is for platform that do not have CAPI or CCIX property ie cache coherency for CPU access to device memory. On such platform when you migrate a virtual address to use device physical memory you update the CPU page table with a special entry. If the CPU try to access the virtual address with special entry it trigger fault and HMM will migrate the virtual address back to regular memory. But this does not apply for CAPI or CCIX platform.

Quoted 5 lines

the example of the virtual address using device physical memory is : gpu_r = gpu_alloc(m*m*sizeof(float)),
so CPU want to access gpu_r will trigger migrate back to CPU memory, it will allocate CPU page and copy
to gpu_r's content to CPU pages, right?

No. Here we are always talking about virtual address that are the outcome of an mmap syscall either as private anonymous memory or as mmap of regular file (ie not a device file but a regular file on a filesystem).

Device driver can migrate any virtual address to use device memory for performance reasons (how, why and when such migration happens is totaly opaque to HMM it is under the control of the device driver).

So if you do:
BUFA = malloc(size);
Then do something with BUFA on the CPU (like reading input or network, ...) the memory is likely to be allocated with regular main memory (like DDR).

Now if you start some job on your GPU that access BUFA the device driver might call migrate_vma() helper to migrate the memory to device memory. At that point the virtual address of BUFA point to physical device memory here CAPI or CCIX. If it is not CAPI/CCIX than the GPU page table point to device memory while the CPU page table point to invalid special entry. The GPU can work on BUFA that now reside inside the device memory. Finaly, in the non CAPI/CCIX case, if CPU try to access that memory then a migration back to regular memory happen.

What you really need is to decouple the virtual address part from what is the physical memory that is backing a virtual address. HMM provide helpers for both aspect. First to mirror page table so that every virtual address point to same physical address. Second side of HMM is to allow to use device memory transparently inside a process by allowing to migrate any virtual address to use device memory. Both aspect are orthogonal to each others.

Cheers,
JA(C)rA'me

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#61 lore raw

2017-12-14 12:16 GMT+08:00 Jerome Glisse <[email protected]>:

Quoted 69 lines · Figo.zhang
Quoted 6 lines

2017-12-14 11:16 GMT+08:00 Jerome Glisse <[email protected]>:

Quoted 4 lines · Figo.zhang
Quoted 3 lines

2017-12-14 0:12 GMT+08:00 Jerome Glisse <[email protected]>:

Quoted 1 line · Figo.zhang

[...]

Quoted 1 line
Quoted 1 line

This is not what happen. Here is the workflow with HMM mirror (note

that

Quoted 1 line
Quoted 1 line

physical address do not matter here so i do not even reference them it

is

Quoted 11 lines
Quoted 10 lines

all about virtual address):
1 They are 3 buffers a, b and r at given virtual address both CPU and
GPU can access them (concurently or not this does not matter).
2 GPU can fault so if any virtual address do not have a page table
entry inside the GPU page table this trigger a page fault that will call HMM mirror helper to snapshot CPU page table into the GPU page table. If there is no physical memory backing the virtual address (ie CPU page table is also empty for the given virtual address) then the regular page fault handler of the kernel is invoked.

Quoted 7 lines

so when HMM mirror done, the content of GPU page table entry and CPU page table entry
are same, right? so the GPU and CPU can access the same physical address, this physical
address is allocated by CPU malloc systemcall. is it conflict and race condition? CPU and GPU
write to this physical address concurrently.

Correct and yes it is conflict free. PCIE platform already support cache coherent access by device to main memory (snoop transaction in PCIE specification). Access can happen concurently to same byte and it behave exactly the same as if two CPU core try to access the same byte.

Quoted 3 lines

i see this slides said:
http://on-demand.gputechconf.com/gtc/2017/presentation/

s7764_john-hubbardgpus-using-hmm-blur-the-lines-between-cpu-and-gpu.pdf

Quoted 6 lines

in page 22~23:
When CPU page fault occurs:
* UM (unified memory driver) copies page data to CPU, umaps from GPU *HMM maps page to CPU

Quoted 6 lines

when GPU page fault occurs:
*HMM has a malloc record buffer, so UM copy page data to GPU *HMM unmaps page from CPU

so in this slides, it said it will has two copies, from CPU to GPU, and from GPU to CPU. so in this case (mul_mat_on_gpu()), is it really need

two

Quoted 1 line

copies in kernel space?

This slide is for the case where you use device memory on PCIE platform. When that happen only the device can access the virtual address back by device memory. If CPU try to access such address a page fault is trigger and it migrate the data back to regular memory where both GPU and CPU can access it concurently.

And again this behavior only happen if you use HMM non cache coherent device memory model. If you use the device cache coherent model with HMM then CPU can access the device memory directly too and above scenario never happen.

Quoted 11 lines

Note that memory copy when data move from device to system or from system to device memory are inevitable. This is exactly as with autoNUMA. Also note that in some case thing can get allocated directly on GPU and never copied back to regular memory (only use by GPU and freed once GPU is done with them) the zero copy case. But i want to stress that the zero copy case is unlikely to happen for input buffer. Usualy you do not get your input data set directly on the GPU but from network or disk and you might do pre-processing on CPU (uncompress input, or do something that is better done on the CPU). Then you feed your data to the GPU and you do computation there.

Great!very detail about the HMM explanation, Thanks a lot. so would you like see my conclusion is correct?
* if support CCIX/CAPI, CPU can access GPU memory directly, and GPU also can access CPU memory directly,
so it no need copy on kernel space in HMM solutions.

* if no support CCIX/CAPI, CPU cannot access GPU memory in cache coherency method, also GPU cannot access CPU memory at cache coherency. it need some copies like John Hobburt's slides. *when GPU page fault, need copy data from CPU page to GPU page, and
HMM unmap the CPU page...
* when CPU page fault, need copy data from GPU page to CPU page
and ummap GPU page and map the CPU page...

Quoted 47 lines
Quoted 11 lines
Quoted 11 lines

Without HMM mirror but ATS/PASI (CCIX or CAPI):
1 They are 3 buffers a, b and r at given virtual address both CPU and
GPU can access them (concurently or not this does not matter).
2 GPU use the exact same page table as the CPU and fault exactly like
CPU on empty page table entry

So in the end with HMM mirror or ATS/PASID you get the same behavior. There is no complexity like you seem to assume. This all about virtual address. At any point in time any given valid virtual address of a

process

Quoted 1 line
Quoted 1 line

point to a given physical memory address and that physical memory

address

Quoted 8 lines
Quoted 8 lines

is the same on both the CPU and the GPU at any point in time they are never out of sync (both in HMM mirror and in ATS/PASID case).

The exception is for platform that do not have CAPI or CCIX property ie cache coherency for CPU access to device memory. On such platform when you migrate a virtual address to use device physical memory you update the CPU page table with a special entry. If the CPU try to access the virtual address with special entry it trigger fault and HMM will

migrate

Quoted 5 lines
Quoted 3 lines

the virtual address back to regular memory. But this does not apply for CAPI or CCIX platform.

the example of the virtual address using device physical memory is :

gpu_r

Quoted 4 lines

= gpu_alloc(m*m*sizeof(float)),
so CPU want to access gpu_r will trigger migrate back to CPU memory, it will allocate CPU page and copy
to gpu_r's content to CPU pages, right?

No. Here we are always talking about virtual address that are the outcome of an mmap syscall either as private anonymous memory or as mmap of regular file (ie not a device file but a regular file on a filesystem).

Device driver can migrate any virtual address to use device memory for performance reasons (how, why and when such migration happens is totaly opaque to HMM it is under the control of the device driver).

So if you do:

   BUFA = malloc(size);

Then do something with BUFA on the CPU (like reading input or network, ...) the memory is likely to be allocated with regular main memory (like DDR).

Quoted 10 lines

Now if you start some job on your GPU that access BUFA the device driver might call migrate_vma() helper to migrate the memory to device memory. At that point the virtual address of BUFA point to physical device memory here CAPI or CCIX. If it is not CAPI/CCIX than the GPU page table point to device
memory while the CPU page table point to invalid special entry. The GPU can work on BUFA that now reside inside the device memory. Finaly, in the non CAPI/CCIX case, if CPU try to access that memory then a migration back to regular memory happen.

in this scenario:
*if CAPI/CCIX support, the CPU's page table and GPU's also point to the device physical page?
in this case, it still need the ZONE_DEVICE infrastructure for CPU page table?

*if no CAPI/CCIX support, the CPU's page table filled a invalid special pte.

Quoted 12 lines

What you really need is to decouple the virtual address part from what is the physical memory that is backing a virtual address. HMM provide helpers for both aspect. First to mirror page table so that every virtual address point to same physical address. Second side of HMM is to allow to use device
memory transparently inside a process by allowing to migrate any virtual address to use device memory. Both aspect are orthogonal to each others.

Cheers,
Jérôme

#62 lore raw
Quoted 6 lines · Figo.zhang

2017-12-14 12:16 GMT+08:00 Jerome Glisse <[email protected]>:

Quoted 5 lines · Figo.zhang
Quoted 4 lines

2017-12-14 11:16 GMT+08:00 Jerome Glisse <[email protected]>:

Quoted 3 lines · Figo.zhang
Quoted 2 lines

2017-12-14 0:12 GMT+08:00 Jerome Glisse <[email protected]>:

Quoted 1 line · Figo.zhang

[...]

Quoted 23 lines
Quoted 11 lines

This slide is for the case where you use device memory on PCIE platform. When that happen only the device can access the virtual address back by device memory. If CPU try to access such address a page fault is trigger and it migrate the data back to regular memory where both GPU and CPU can access it concurently.

And again this behavior only happen if you use HMM non cache coherent device memory model. If you use the device cache coherent model with HMM then CPU can access the device memory directly too and above scenario never happen.

Quoted 11 lines

Note that memory copy when data move from device to system or from system to device memory are inevitable. This is exactly as with autoNUMA. Also note that in some case thing can get allocated directly on GPU and never copied back to regular memory (only use by GPU and freed once GPU is done with them) the zero copy case. But i want to stress that the zero copy case is unlikely to happen for input buffer. Usualy you do not get your input data set directly on the GPU but from network or disk and you might do pre-processing on CPU (uncompress input, or do something that is better done on the CPU). Then you feed your data to the GPU and you do computation there.

Quoted 5 lines

Greati 1/4 ?very detail about the HMM explanation, Thanks a lot. so would you like see my conclusion is correct?
* if support CCIX/CAPI, CPU can access GPU memory directly, and GPU also can access CPU memory directly,
so it no need copy on kernel space in HMM solutions.

Yes but migration do imply copy. The physical address backing a virtual address can change over the lifetime of a virtual address (between mmap and munmap). As a result of various activity (auto NUMA, compaction, swap out then swap back in, ...) and in the case that interest us as the result of a device driver migrating thing to its device memory.

Quoted 7 lines

* if no support CCIX/CAPI, CPU cannot access GPU memory in cache coherency method, also GPU cannot access CPU memory at cache coherency. it need some copies like John Hobburt's slides. *when GPU page fault, need copy data from CPU page to GPU page, and
HMM unmap the CPU page...
* when CPU page fault, need copy data from GPU page to CPU page
and ummap GPU page and map the CPU page...

No, GPU can access main memory just fine (snoop PCIE transaction and in a full cache coherent way with CPU). Only the CPU can not access the device memory. So there is a special case only when migrating some virtual address to use device memory.

What is described inside John's slides is what happen when you migrate some virtual addresses to device memory where the CPU can not access it. This migration is not necessary for the GPU to access memory. It only happens as an optimization when the device driver suspect it will make frequent access to that memory and that CPU will not try to access it.

[...]

Quoted 24 lines
Quoted 13 lines

No. Here we are always talking about virtual address that are the outcome of an mmap syscall either as private anonymous memory or as mmap of regular file (ie not a device file but a regular file on a filesystem).

Device driver can migrate any virtual address to use device memory for performance reasons (how, why and when such migration happens is totaly opaque to HMM it is under the control of the device driver).

So if you do:

   BUFA = malloc(size);

Then do something with BUFA on the CPU (like reading input or network, ...) the memory is likely to be allocated with regular main memory (like DDR).

Quoted 10 lines

Now if you start some job on your GPU that access BUFA the device driver might call migrate_vma() helper to migrate the memory to device memory. At that point the virtual address of BUFA point to physical device memory here CAPI or CCIX. If it is not CAPI/CCIX than the GPU page table point to device
memory while the CPU page table point to invalid special entry. The GPU can work on BUFA that now reside inside the device memory. Finaly, in the non CAPI/CCIX case, if CPU try to access that memory then a migration back to regular memory happen.

Quoted 5 lines

in this scenario:
*if CAPI/CCIX supporti 1/4 ? the CPU's page table and GPU's also point to the device physical page?
in this case, it still need the ZONE_DEVICE infrastructure for CPU page tablei 1/4 ?

Correct, in CAPI/CCIX case there is only one page table and thus after migration they both point to same physical address for the virtual addresses of BUFA.

Quoted 1 line

*if no CAPI/CCIX support, the CPU's page table filled a invalid special pte.

Correct. This is the case described by John's slides.

The physical memory backing a virtual address can change at anytime for many different reasons (autonuma, compaction, swap out follow by swap in, ...) and migration (from one physical memory type to another) for accelerator purposes is just a new reasons in that list.

Cheers,
JA(C)rA'me

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>