mm/ZONE_DEVICE: new type of ZONE_DEVICE for unaddressable memory
Subsystems (6)
[email protected]
maintainerPATCH 17/126 mm/ZONE_DEVICE: new type of ZONE_DEVICE for unaddressable memory
From: Jérôme Glisse <[email protected]>
Subject: mm/ZONE_DEVICE: new type of ZONE_DEVICE for unaddressable memory
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.
---
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 renamed from fs/proc/task_mmu.c~mm-zone_device-new-type-of-zone_device-for-unaddressable-memory-v5 +7 -0
@@ -549,6 +549,8 @@ static void smaps_pte_entry(pte_t *pte, } }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,
@@ -713,6 +715,8 @@ static int smaps_hugetlb_range(pte_t *ptif (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);
@@ -1276,6 +1280,9 @@ static pagemap_entry_t pte_to_pagemap_en 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 renamed from include/linux/ioport.h~mm-zone_device-new-type-of-zone_device-for-unaddressable-memory-v5 +1 -0
@@ -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 renamed from include/linux/memremap.h~mm-zone_device-new-type-of-zone_device-for-unaddressable-memory-v5 +73 -0
97 lines not on this page · raw message
include/linux/mm.h renamed from include/linux/mm.h~mm-zone_device-new-type-of-zone_device-for-unaddressable-memory-v5 +12 -0
@@ -792,11 +792,23 @@ static inline bool is_zone_device_page(c {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 renamed from include/linux/swap.h~mm-zone_device-new-type-of-zone_device-for-unaddressable-memory-v5 +21 -3
@@ -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
@@ -469,8 +487,8 @@ static inline void show_swap_cache_info( { } -#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 renamed from include/linux/swapops.h~mm-zone_device-new-type-of-zone_device-for-unaddressable-memory-v5 +68 -0
74 lines not on this page · raw message
kernel/memremap.c renamed from kernel/memremap.c~mm-zone_device-new-type-of-zone_device-for-unaddressable-memory-v5 +34 -0
@@ -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 */
@@ -219,6 +221,34 @@ static unsigned long order_at(struct resfor (pgoff =0 , order =order_at ((res), pgoff); order <ULONG_MAX ; \ pgoff += 1UL << order, order = order_at((res), pgoff)) +#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) {unsigned long pgoff, order;
@@ -356,6 +386,10 @@ void *devm_memremap_pages(struct device } 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 renamed from mm/Kconfig~mm-zone_device-new-type-of-zone_device-for-unaddressable-memory-v5 +10 -1
@@ -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
@@ -717,6 +717,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 renamed from mm/memory.c~mm-zone_device-new-type-of-zone_device-for-unaddressable-memory-v5 +61 -0
85 lines not on this page · raw message
mm/memory_hotplug.c renamed from mm/memory_hotplug.c~mm-zone_device-new-type-of-zone_device-for-unaddressable-memory-v5 +8 -2
@@ -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_ 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 renamed from mm/mprotect.c~mm-zone_device-new-type-of-zone_device-for-unaddressable-memory-v5 +14 -0
@@ -125,6 +125,20 @@ static unsigned long change_pte_range(st 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 ();
_