Skip to contentSkip to conversation

mm/device-public-memory: device memory cache coherent with CPU

PATCH New [email protected] 1 participant 1 message Sep 8, 2017

Subsystems (10)
lore
Share & tools
lore

[email protected]

author maintainer
#0 lore raw

PATCH 28/126 mm/device-public-memory: device memory cache coherent with CPU

From: Jérôme Glisse <[email protected]>
Subject: mm/device-public-memory: device memory cache coherent with CPU

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.

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]>
Cc Balbir Singh <[email protected]>
Cc David Nellans <[email protected]>
Cc Evgeny Baskakov <[email protected]>
Cc Johannes Weiner <[email protected]>
Cc John Hubbard <[email protected]>
Cc Kirill A. Shutemov <[email protected]>
Cc Mark Hairgrove <[email protected]>
Cc Michal Hocko <[email protected]>
Cc Sherry Cheung <[email protected]>
Cc Subhash Gutti <[email protected]>
Cc Vladimir Davydov <[email protected]>
Cc Bob Liu <[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             |   55 ++++++++++++++++++++++++-------------
 mm/swap.c                |   11 +++++++
 14 files changed, 158 insertions(+), 46 deletions(-)
fs/proc/task_mmu.c renamed from fs/proc/task_mmu.c~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 +1 -1
diff -puN fs/proc/task_mmu.c~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 fs/proc/task_mmu.c
@@ -1267,7 +1267,7 @@ static pagemap_entry_t pte_to_pagemap_en
 		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 renamed from include/linux/hmm.h~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 +2 -2
diff -puN include/linux/hmm.h~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 include/linux/hmm.h
@@ -327,7 +327,7 @@ int hmm_vma_fault(struct vm_area_struct
 #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 renamed from include/linux/ioport.h~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 +1 -0
diff -puN include/linux/ioport.h~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 include/linux/ioport.h
@@ -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 renamed from include/linux/memremap.h~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 +21 -0
diff -puN include/linux/memremap.h~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 include/linux/memremap.h
@@ -57,10 +57,18 @@ static inline struct vmem_altmap *to_vme
  *
  * 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_pag
 	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_pag
 {
 	return false;
 }
+
+static inline bool is_device_public_page(const struct page *page)
+{
+	return false;
+}
 #endif
 
 /**
include/linux/mm.h renamed from include/linux/mm.h~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 +12 -8
diff -puN include/linux/mm.h~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 include/linux/mm.h
@@ -800,15 +800,16 @@ static inline bool is_zone_device_page(c
 }
 #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);
 
@@ -834,8 +835,9 @@ static inline void put_page(struct 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;
 	}
 
@@ -1224,8 +1226,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 renamed from kernel/memremap.c~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 +4 -4
diff -puN kernel/memremap.c~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 kernel/memremap.c
@@ -501,8 +501,8 @@ struct vmem_altmap *to_vmem_altmap(unsig
 #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);
 
@@ -522,5 +522,5 @@ void put_zone_device_private_page(struct
 	} 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 renamed from mm/Kconfig~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 +11 -0
diff -puN mm/Kconfig~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 mm/Kconfig
@@ -720,12 +720,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 renamed from mm/gup.c~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 +7 -0
diff -puN mm/gup.c~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 mm/gup.c
@@ -456,6 +456,13 @@ static int get_gate_page(struct mm_struc
 		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 renamed from mm/hmm.c~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 +2 -2
diff -puN mm/hmm.c~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 mm/hmm.c
@@ -737,7 +737,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)
 {
@@ -1177,4 +1177,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 renamed from mm/madvise.c~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 +1 -1
diff -puN mm/madvise.c~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 mm/madvise.c
@@ -355,7 +355,7 @@ static int madvise_free_pte_range(pmd_t
 			continue;
 		}
 
-		page = vm_normal_page(vma, addr, ptent);
+		page = _vm_normal_page(vma, addr, ptent, true);
 		if (!page)
 			continue;
 
mm/memcontrol.c renamed from mm/memcontrol.c~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 +7 -5
diff -puN mm/memcontrol.c~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 mm/memcontrol.c
@@ -4623,10 +4623,11 @@ out:
  *   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
  *
@@ -4657,7 +4658,8 @@ static enum mc_target_type get_mctgt_typ
 		 */
 		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 renamed from mm/memory.c~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 +41 -5

70 lines not on this page · raw message

mm/migrate.c renamed from mm/migrate.c~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 +37 -18

107 lines not on this page · raw message

mm/swap.c renamed from mm/swap.c~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 +11 -0
diff -puN mm/swap.c~mm-device-public-memory-device-memory-cache-coherent-with-cpu-v5 mm/swap.c
@@ -765,6 +765,17 @@ void release_pages(struct page **pages,
 		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;

_