Skip to contentSkip to conversation

drm/ttm: add swap_glob_mem in ttm_mem_global

PATCH In review Roger He 3 participants 5 messages Jan 29, 2018 – Jan 31, 2018

Subsystems (3)
lore
Share & tools
lore

Roger He

author
#0 lore raw

PATCH 1/2 drm/ttm: add swap_glob_mem in ttm_mem_global

separate swapped memory account from zone->used_mem because swapped ttm pages can be flushed into SWAP disk/file under high memory pressure.

add check conditon in ttm_mem_global_reserve to prevent triggering OOM. because if SWAP disk/file is full, all swapped ttm page would stay in system memory which can't be flushed into disk/file. At this time, any memory request will trigger OOM killer.

v2: always get total system swap size rather than getting it once at module init time

---
 drivers/gpu/drm/ttm/ttm_memory.c | 24 ++++++++++++++++--------
 drivers/gpu/drm/ttm/ttm_tt.c     | 13 +++++++++++--
 include/drm/ttm/ttm_memory.h     |  2 ++
 3 files changed, 29 insertions(+), 10 deletions(-)
drivers/gpu/drm/ttm/ttm_memory.c +16 -8
index aa0c381..06024ba 100644
@@ -36,6 +36,7 @@
 #include <linux/mm.h>
 #include <linux/module.h>
 #include <linux/slab.h>
+#include <linux/swap.h>
 
 #define TTM_MEMORY_ALLOC_RETRIES 4
 
@@ -183,20 +184,20 @@ static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob,
 {
 	unsigned int i;
 	struct ttm_mem_zone *zone;
-	uint64_t target;
+	uint64_t target, limit;
 
 	for (i = 0; i < glob->num_zones; ++i) {
 		zone = glob->zones[i];
 
+		limit = (capable(CAP_SYS_ADMIN)) ?
+				zone->emer_mem : zone->max_mem;
+
 		if (from_wq)
 			target = zone->swap_limit;
-		else if (capable(CAP_SYS_ADMIN))
-			target = zone->emer_mem;
 		else
-			target = zone->max_mem;
+			target = limit;
 
 		target = (extra > target) ? 0ULL : target;
-
 		if (zone->used_mem > target)
 			return true;
 	}
@@ -373,6 +374,7 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
 		return ret;
 	}
 
+	atomic64_set(&glob->swap_glob_mem, 0);
 	si_meminfo(&si);
 
 	ret = ttm_mem_init_kernel_zone(glob, &si);
@@ -473,10 +475,14 @@ static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
 				  struct ttm_mem_zone *single_zone,
 				  uint64_t amount, bool reserve)
 {
-	uint64_t limit;
+	uint64_t swap_glob_mem, max_swap_cache;
+	uint64_t limit, total_used_mem;
+	struct ttm_mem_zone *zone;
 	int ret = -ENOMEM;
 	unsigned int i;
-	struct ttm_mem_zone *zone;
+
+	swap_glob_mem = atomic64_read(&glob->swap_glob_mem);
+	max_swap_cache = (get_total_swap_pages() >> 1) << PAGE_SHIFT;
 
 	spin_lock(&glob->lock);
 	for (i = 0; i < glob->num_zones; ++i) {
@@ -487,7 +493,9 @@ static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
 		limit = (capable(CAP_SYS_ADMIN)) ?
 			zone->emer_mem : zone->max_mem;
 
-		if (zone->used_mem > limit)
+		total_used_mem = zone->used_mem + swap_glob_mem;
+		limit += max_swap_cache;
+		if (total_used_mem > limit)
 			goto out_unlock;
 	}
 
drivers/gpu/drm/ttm/ttm_tt.c +11 -2
index 9e4d43d..395cac0 100644
@@ -177,8 +177,11 @@ void ttm_tt_destroy(struct ttm_tt *ttm)
 		ttm_tt_unpopulate(ttm);
 
 	if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
-	    ttm->swap_storage)
+	    ttm->swap_storage) {
 		fput(ttm->swap_storage);
+		atomic64_sub_return(ttm->num_pages << PAGE_SHIFT,
+				    &ttm->glob->mem_glob->swap_glob_mem);
+	}
 
 	ttm->swap_storage = NULL;
 	ttm->func->destroy(ttm);
@@ -318,8 +321,11 @@ int ttm_tt_swapin(struct ttm_tt *ttm)
 		put_page(from_page);
 	}
 
-	if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
+	if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP)) {
 		fput(swap_storage);
+		atomic64_sub_return(ttm->num_pages << PAGE_SHIFT,
+				    &ttm->glob->mem_glob->swap_glob_mem);
+	}
 	ttm->swap_storage = NULL;
 	ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
 
@@ -378,6 +384,9 @@ int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
 	ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
 	if (persistent_swap_storage)
 		ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
+	else
+		atomic64_add_return(ttm->num_pages << PAGE_SHIFT,
+			    &ttm->glob->mem_glob->swap_glob_mem);
 
 	return 0;
 out_err:
include/drm/ttm/ttm_memory.h +2 -0
index 8936285..a273581 100644
@@ -49,6 +49,7 @@
  * @work: The workqueue callback for the shrink queue.
  * @lock: Lock to protect the @shrink - and the memory accounting members,
  * that is, essentially the whole structure with some exceptions.
+ * @swap_glob_mem: total size of ttm page which has been swapped out
  * @zones: Array of pointers to accounting zones.
  * @num_zones: Number of populated entries in the @zones array.
  * @zone_kernel: Pointer to the kernel zone.
@@ -67,6 +68,7 @@ struct ttm_mem_global {
 	struct workqueue_struct *swap_queue;
 	struct work_struct work;
 	spinlock_t lock;
+	atomic64_t swap_glob_mem;
 	struct ttm_mem_zone *zones[TTM_MEM_MAX_ZONES];
 	unsigned int num_zones;
 	struct ttm_mem_zone *zone_kernel;
Signature · 6 lines
2.7.4

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
author
#1 lore raw

PATCH 2/2 drm/ttm: add ttm page_flags TTM_PAGE_FLAG_PAGEFAULT

to indicate whether we are serving for page fault routine when in ttm_mem_global_reserve.

for ttm_mem_global_reserve if in page fault routine, allow success always. because page fault routing already grabbed system memory successfully and allow this exception is harmless. Otherwise, it will trigger OOM killer.

v2: add new page flag TTM_PAGE_FLAG_PAGEFAULT rather than using struct ttm_operation_ctx

---
 drivers/gpu/drm/ttm/ttm_bo_vm.c          |  6 ++++--
 drivers/gpu/drm/ttm/ttm_memory.c         | 27 +++++++++++++++++++--------
 drivers/gpu/drm/ttm/ttm_page_alloc.c     |  5 ++++-
 drivers/gpu/drm/ttm/ttm_page_alloc_dma.c |  9 ++++++---
 drivers/gpu/drm/ttm/ttm_tt.c             |  2 +-
 include/drm/ttm/ttm_bo_driver.h          |  1 +
 include/drm/ttm/ttm_memory.h             |  3 ++-
 7 files changed, 37 insertions(+), 16 deletions(-)
drivers/gpu/drm/ttm/ttm_bo_vm.c +4 -2
index 08a3c32..1aee124 100644
@@ -235,9 +235,11 @@ static int ttm_bo_vm_fault(struct vm_fault *vmf)
 		ttm = bo->ttm;
 		cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
 						cvma.vm_page_prot);
-
+		ttm->page_flags |= TTM_PAGE_FLAG_PAGEFAULT;
 		/* Allocate all page at once, most common usage */
-		if (ttm->bdev->driver->ttm_tt_populate(ttm, &ctx)) {
+		ret = ttm->bdev->driver->ttm_tt_populate(ttm, &ctx);
+		ttm->page_flags &= ~TTM_PAGE_FLAG_PAGEFAULT;
+		if (ret) {
 			retval = VM_FAULT_OOM;
 			goto out_io_unlock;
 		}
drivers/gpu/drm/ttm/ttm_memory.c +19 -8
index 06024ba..d0312a7 100644
@@ -473,7 +473,8 @@ EXPORT_SYMBOL(ttm_mem_global_free);
 
 static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
 				  struct ttm_mem_zone *single_zone,
-				  uint64_t amount, bool reserve)
+				  uint64_t amount, bool reserve,
+				  bool in_pagefault)
 {
 	uint64_t swap_glob_mem, max_swap_cache;
 	uint64_t limit, total_used_mem;
@@ -485,6 +486,13 @@ static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
 	max_swap_cache = (get_total_swap_pages() >> 1) << PAGE_SHIFT;
 
 	spin_lock(&glob->lock);
+	/*
+	 * if in page_fault allow reserve successfully anyway since it
+	 * already allocated system pages. Otherwise it will trigger OOM
+	 */
+	if (in_pagefault)
+		goto reserve_direct;
+
 	for (i = 0; i < glob->num_zones; ++i) {
 		zone = glob->zones[i];
 		if (single_zone && zone != single_zone)
@@ -499,6 +507,7 @@ static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
 			goto out_unlock;
 	}
 
+reserve_direct:
 	if (reserve) {
 		for (i = 0; i < glob->num_zones; ++i) {
 			zone = glob->zones[i];
@@ -520,13 +529,13 @@ static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
 static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob,
 				     struct ttm_mem_zone *single_zone,
 				     uint64_t memory,
-				     struct ttm_operation_ctx *ctx)
+				     struct ttm_operation_ctx *ctx,
+				     bool in_pagefault)
 {
 	int count = TTM_MEMORY_ALLOC_RETRIES;
 
-	while (unlikely(ttm_mem_global_reserve(glob,
-					       single_zone,
-					       memory, true)
+	while (unlikely(ttm_mem_global_reserve(glob, single_zone, memory,
+					       true, in_pagefault)
 			!= 0)) {
 		if (ctx->no_wait_gpu)
 			return -ENOMEM;
@@ -546,13 +555,14 @@ int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
 	 * all zones.
 	 */
 
-	return ttm_mem_global_alloc_zone(glob, NULL, memory, ctx);
+	return ttm_mem_global_alloc_zone(glob, NULL, memory, ctx, false);
 }
 EXPORT_SYMBOL(ttm_mem_global_alloc);
 
 int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
 			      struct page *page, uint64_t size,
-			      struct ttm_operation_ctx *ctx)
+			      struct ttm_operation_ctx *ctx,
+			      bool in_pagefault)
 {
 	struct ttm_mem_zone *zone = NULL;
 
@@ -568,7 +578,8 @@ int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
 	if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
 		zone = glob->zone_kernel;
 #endif
-	return ttm_mem_global_alloc_zone(glob, zone, size, ctx);
+	return ttm_mem_global_alloc_zone(glob, zone, size,
+					 ctx, in_pagefault);
 }
 
 void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct page *page,
drivers/gpu/drm/ttm/ttm_page_alloc.c +4 -1
index 5edcd89..39b184f 100644
@@ -1094,6 +1094,7 @@ ttm_pool_unpopulate_helper(struct ttm_tt *ttm, unsigned mem_count_update)
 int ttm_pool_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
 {
 	struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
+	bool in_pagefault;
 	unsigned i;
 	int ret;
 
@@ -1107,9 +1108,11 @@ int ttm_pool_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
 		return ret;
 	}
 
+	in_pagefault = ttm->page_flags | TTM_PAGE_FLAG_PAGEFAULT;
 	for (i = 0; i < ttm->num_pages; ++i) {
 		ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
-						PAGE_SIZE, ctx);
+						PAGE_SIZE, ctx,
+						in_pagefault);
 		if (unlikely(ret != 0)) {
 			ttm_pool_unpopulate_helper(ttm, i);
 			return -ENOMEM;
drivers/gpu/drm/ttm/ttm_page_alloc_dma.c +6 -3
index 9e90d0e..77dd167 100644
@@ -939,6 +939,7 @@ int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev,
 	struct dma_pool *pool;
 	struct dma_page *d_page;
 	enum pool_type type;
+	bool in_pagefault;
 	unsigned i;
 	int ret;
 
@@ -948,8 +949,8 @@ int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev,
 	INIT_LIST_HEAD(&ttm_dma->pages_list);
 	i = 0;
 
+	in_pagefault = ttm_dma->ttm.page_flags | TTM_PAGE_FLAG_PAGEFAULT;
 	type = ttm_to_type(ttm->page_flags, ttm->caching_state);
-
 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
 	if (ttm->page_flags & TTM_PAGE_FLAG_DMA32)
 		goto skip_huge;
@@ -971,7 +972,8 @@ int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev,
 			break;
 
 		ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
-						pool->size, ctx);
+						pool->size, ctx,
+						in_pagefault);
 		if (unlikely(ret != 0)) {
 			ttm_dma_unpopulate(ttm_dma, dev);
 			return -ENOMEM;
@@ -1008,7 +1010,8 @@ int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev,
 		}
 
 		ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
-						pool->size, ctx);
+						pool->size, ctx,
+						in_pagefault);
 		if (unlikely(ret != 0)) {
 			ttm_dma_unpopulate(ttm_dma, dev);
 			return -ENOMEM;
drivers/gpu/drm/ttm/ttm_tt.c +1 -1
index 395cac0..c36db66 100644
@@ -386,7 +386,7 @@ int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
 		ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
 	else
 		atomic64_add_return(ttm->num_pages << PAGE_SHIFT,
-			    &ttm->glob->mem_glob->swap_glob_mem);
+				    &ttm->glob->mem_glob->swap_glob_mem);
 
 	return 0;
 out_err:
include/drm/ttm/ttm_bo_driver.h +1 -0
index 9b417eb..8f68781 100644
@@ -87,6 +87,7 @@ struct ttm_backend_func {
 #define TTM_PAGE_FLAG_DMA32           (1 << 7)
 #define TTM_PAGE_FLAG_SG              (1 << 8)
 #define TTM_PAGE_FLAG_NO_RETRY	      (1 << 9)
+#define TTM_PAGE_FLAG_PAGEFAULT       (1 << 10)
 
 enum ttm_caching_state {
 	tt_uncached,
include/drm/ttm/ttm_memory.h +2 -1
index a273581..b801010 100644
@@ -87,7 +87,8 @@ extern void ttm_mem_global_free(struct ttm_mem_global *glob,
 				uint64_t amount);
 extern int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
 				     struct page *page, uint64_t size,
-				     struct ttm_operation_ctx *ctx);
+				     struct ttm_operation_ctx *ctx,
+				     bool in_pagefault);
 extern void ttm_mem_global_free_page(struct ttm_mem_global *glob,
 				     struct page *page, uint64_t size);
 extern size_t ttm_round_pot(size_t size);
Signature · 6 lines
2.7.4

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw

Re: [PATCH 2/2] drm/ttm: add ttm page_flags TTM_PAGE_FLAG_PAGEFAULT

drivers/gpu/drm/ttm/ttm_page_alloc.c int ttm_pool_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx) · line 1111

Quoted 6 lines
drivers/gpu/drm/ttm/ttm_page_alloc.c +1 -0
@@ -1107,9 +1108,11 @@ int ttm_pool_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
  		return ret;
  	}
 
+	in_pagefault = ttm->page_flags | TTM_PAGE_FLAG_PAGEFAULT;

That should probably be "ttm->page_flags & TTM_PAGE_FLAG_PAGEFAULT;".

Quoted 21 lines
drivers/gpu/drm/ttm/ttm_page_alloc.c +2 -1
  	for (i = 0; i < ttm->num_pages; ++i) {
  		ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
-						PAGE_SIZE, ctx);
+						PAGE_SIZE, ctx,
+						in_pagefault);
  		if (unlikely(ret != 0)) {
  			ttm_pool_unpopulate_helper(ttm, i);
  			return -ENOMEM;
drivers/gpu/drm/ttm/ttm_page_alloc_dma.c +1 -0
index 9e90d0e..77dd167 100644
@@ -939,6 +939,7 @@ int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev,
  	struct dma_pool *pool;
  	struct dma_page *d_page;
  	enum pool_type type;
+	bool in_pagefault;
  	unsigned i;
  	int ret;
 

drivers/gpu/drm/ttm/ttm_page_alloc_dma.c int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev, · line 952

Quoted 4 lines
drivers/gpu/drm/ttm/ttm_page_alloc_dma.c +1 -0
@@ -948,8 +949,8 @@ int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev,
  	INIT_LIST_HEAD(&ttm_dma->pages_list);
  	i = 0;
 
+	in_pagefault = ttm_dma->ttm.page_flags | TTM_PAGE_FLAG_PAGEFAULT;

Dito.

Christian.

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#3 lore raw

Re: [PATCH 2/2] drm/ttm: add ttm page_flags TTM_PAGE_FLAG_PAGEFAULT

In reply to Roger He
Quoted 134 lines · Roger He

to indicate whether we are serving for page fault routine when in ttm_mem_global_reserve.

for ttm_mem_global_reserve if in page fault routine, allow success always. because page fault routing already grabbed system memory successfully and allow this exception is harmless. Otherwise, it will trigger OOM killer.

v2: add new page flag TTM_PAGE_FLAG_PAGEFAULT rather than using struct ttm_operation_ctx

---
  drivers/gpu/drm/ttm/ttm_bo_vm.c          |  6 ++++--
  drivers/gpu/drm/ttm/ttm_memory.c         | 27 +++++++++++++++++++--------
  drivers/gpu/drm/ttm/ttm_page_alloc.c     |  5 ++++-
  drivers/gpu/drm/ttm/ttm_page_alloc_dma.c |  9 ++++++---
  drivers/gpu/drm/ttm/ttm_tt.c             |  2 +-
  include/drm/ttm/ttm_bo_driver.h          |  1 +
  include/drm/ttm/ttm_memory.h             |  3 ++-
  7 files changed, 37 insertions(+), 16 deletions(-)
drivers/gpu/drm/ttm/ttm_bo_vm.c +4 -2
index 08a3c32..1aee124 100644
@@ -235,9 +235,11 @@ static int ttm_bo_vm_fault(struct vm_fault *vmf)
  		ttm = bo->ttm;
  		cvma.vm_page_prot = ttm_io_prot(bo->mem.placement,
  						cvma.vm_page_prot);
-
+		ttm->page_flags |= TTM_PAGE_FLAG_PAGEFAULT;
  		/* Allocate all page at once, most common usage */
-		if (ttm->bdev->driver->ttm_tt_populate(ttm, &ctx)) {
+		ret = ttm->bdev->driver->ttm_tt_populate(ttm, &ctx);
+		ttm->page_flags &= ~TTM_PAGE_FLAG_PAGEFAULT;
+		if (ret) {
  			retval = VM_FAULT_OOM;
  			goto out_io_unlock;
  		}
drivers/gpu/drm/ttm/ttm_memory.c +19 -8
index 06024ba..d0312a7 100644
@@ -473,7 +473,8 @@ EXPORT_SYMBOL(ttm_mem_global_free);
 
  static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
  				  struct ttm_mem_zone *single_zone,
-				  uint64_t amount, bool reserve)
+				  uint64_t amount, bool reserve,
+				  bool in_pagefault)
  {
  	uint64_t swap_glob_mem, max_swap_cache;
  	uint64_t limit, total_used_mem;
@@ -485,6 +486,13 @@ static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
  	max_swap_cache = (get_total_swap_pages() >> 1) << PAGE_SHIFT;
 
  	spin_lock(&glob->lock);
+	/*
+	 * if in page_fault allow reserve successfully anyway since it
+	 * already allocated system pages. Otherwise it will trigger OOM
+	 */
+	if (in_pagefault)
+		goto reserve_direct;
+
  	for (i = 0; i < glob->num_zones; ++i) {
  		zone = glob->zones[i];
  		if (single_zone && zone != single_zone)
@@ -499,6 +507,7 @@ static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
  			goto out_unlock;
  	}
 
+reserve_direct:
  	if (reserve) {
  		for (i = 0; i < glob->num_zones; ++i) {
  			zone = glob->zones[i];
@@ -520,13 +529,13 @@ static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
  static int ttm_mem_global_alloc_zone(struct ttm_mem_global *glob,
  				     struct ttm_mem_zone *single_zone,
  				     uint64_t memory,
-				     struct ttm_operation_ctx *ctx)
+				     struct ttm_operation_ctx *ctx,
+				     bool in_pagefault)
  {
  	int count = TTM_MEMORY_ALLOC_RETRIES;
 
-	while (unlikely(ttm_mem_global_reserve(glob,
-					       single_zone,
-					       memory, true)
+	while (unlikely(ttm_mem_global_reserve(glob, single_zone, memory,
+					       true, in_pagefault)
  			!= 0)) {
  		if (ctx->no_wait_gpu)
  			return -ENOMEM;
@@ -546,13 +555,14 @@ int ttm_mem_global_alloc(struct ttm_mem_global *glob, uint64_t memory,
  	 * all zones.
  	 */
 
-	return ttm_mem_global_alloc_zone(glob, NULL, memory, ctx);
+	return ttm_mem_global_alloc_zone(glob, NULL, memory, ctx, false);
  }
  EXPORT_SYMBOL(ttm_mem_global_alloc);
 
  int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
  			      struct page *page, uint64_t size,
-			      struct ttm_operation_ctx *ctx)
+			      struct ttm_operation_ctx *ctx,
+			      bool in_pagefault)
  {
  	struct ttm_mem_zone *zone = NULL;
 
@@ -568,7 +578,8 @@ int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
  	if (glob->zone_dma32 && page_to_pfn(page) > 0x00100000UL)
  		zone = glob->zone_kernel;
  #endif
-	return ttm_mem_global_alloc_zone(glob, zone, size, ctx);
+	return ttm_mem_global_alloc_zone(glob, zone, size,
+					 ctx, in_pagefault);
  }
 
  void ttm_mem_global_free_page(struct ttm_mem_global *glob, struct page *page,
drivers/gpu/drm/ttm/ttm_page_alloc.c +1 -0
index 5edcd89..39b184f 100644
@@ -1094,6 +1094,7 @@ ttm_pool_unpopulate_helper(struct ttm_tt *ttm, unsigned mem_count_update)
  int ttm_pool_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
  {
  	struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
+	bool in_pagefault;
  	unsigned i;
  	int ret;
 

drivers/gpu/drm/ttm/ttm_page_alloc.c int ttm_pool_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx) · line 1111

Quoted 4 lines
drivers/gpu/drm/ttm/ttm_page_alloc.c +1 -0
@@ -1107,9 +1108,11 @@ int ttm_pool_populate(struct ttm_tt *ttm, struct ttm_operation_ctx *ctx)
  		return ret;
  	}
 
+	in_pagefault = ttm->page_flags | TTM_PAGE_FLAG_PAGEFAULT;

I guess you want to write as "in_pagefault = ttm->page_flags & TTM_PAGE_FLAG_PAGEFAULT; ".

Quoted 21 lines
drivers/gpu/drm/ttm/ttm_page_alloc.c +2 -1
  	for (i = 0; i < ttm->num_pages; ++i) {
  		ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
-						PAGE_SIZE, ctx);
+						PAGE_SIZE, ctx,
+						in_pagefault);
  		if (unlikely(ret != 0)) {
  			ttm_pool_unpopulate_helper(ttm, i);
  			return -ENOMEM;
drivers/gpu/drm/ttm/ttm_page_alloc_dma.c +1 -0
index 9e90d0e..77dd167 100644
@@ -939,6 +939,7 @@ int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev,
  	struct dma_pool *pool;
  	struct dma_page *d_page;
  	enum pool_type type;
+	bool in_pagefault;
  	unsigned i;
  	int ret;
 

drivers/gpu/drm/ttm/ttm_page_alloc_dma.c int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev, · line 952

Quoted 4 lines
drivers/gpu/drm/ttm/ttm_page_alloc_dma.c +1 -0
@@ -948,8 +949,8 @@ int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev,
  	INIT_LIST_HEAD(&ttm_dma->pages_list);
  	i = 0;
 
+	in_pagefault = ttm_dma->ttm.page_flags | TTM_PAGE_FLAG_PAGEFAULT;

same above, should be "in_pagefault = ttm->page_flags & TTM_PAGE_FLAG_PAGEFAULT; ".

Regards,
David Zhou

Quoted 55 lines
drivers/gpu/drm/ttm/ttm_page_alloc_dma.c +4 -3
  	type = ttm_to_type(ttm->page_flags, ttm->caching_state);
-
  #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  	if (ttm->page_flags & TTM_PAGE_FLAG_DMA32)
  		goto skip_huge;
@@ -971,7 +972,8 @@ int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev,
  			break;
 
  		ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
-						pool->size, ctx);
+						pool->size, ctx,
+						in_pagefault);
  		if (unlikely(ret != 0)) {
  			ttm_dma_unpopulate(ttm_dma, dev);
  			return -ENOMEM;
@@ -1008,7 +1010,8 @@ int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev,
  		}
 
  		ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
-						pool->size, ctx);
+						pool->size, ctx,
+						in_pagefault);
  		if (unlikely(ret != 0)) {
  			ttm_dma_unpopulate(ttm_dma, dev);
  			return -ENOMEM;
drivers/gpu/drm/ttm/ttm_tt.c +1 -1
index 395cac0..c36db66 100644
@@ -386,7 +386,7 @@ int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
  		ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
  	else
  		atomic64_add_return(ttm->num_pages << PAGE_SHIFT,
-			    &ttm->glob->mem_glob->swap_glob_mem);
+				    &ttm->glob->mem_glob->swap_glob_mem);
 
  	return 0;
  out_err:
include/drm/ttm/ttm_bo_driver.h +1 -0
index 9b417eb..8f68781 100644
@@ -87,6 +87,7 @@ struct ttm_backend_func {
  #define TTM_PAGE_FLAG_DMA32           (1 << 7)
  #define TTM_PAGE_FLAG_SG              (1 << 8)
  #define TTM_PAGE_FLAG_NO_RETRY	      (1 << 9)
+#define TTM_PAGE_FLAG_PAGEFAULT       (1 << 10)
 
  enum ttm_caching_state {
  	tt_uncached,
include/drm/ttm/ttm_memory.h +0 -0
index a273581..b801010 100644

include/drm/ttm/ttm_memory.h extern void ttm_mem_global_free(struct ttm_mem_global *glob, · line 94

Quoted 9 lines
include/drm/ttm/ttm_memory.h +2 -1
@@ -87,7 +87,8 @@ extern void ttm_mem_global_free(struct ttm_mem_global *glob,
  				uint64_t amount);
  extern int ttm_mem_global_alloc_page(struct ttm_mem_global *glob,
  				     struct page *page, uint64_t size,
-				     struct ttm_operation_ctx *ctx);
+				     struct ttm_operation_ctx *ctx,
+				     bool in_pagefault);
  extern void ttm_mem_global_free_page(struct ttm_mem_global *glob,
  				     struct page *page, uint64_t size);
  extern size_t ttm_round_pot(size_t size);

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel

#4 lore raw
In reply to Roger He
Quoted 31 lines · Roger He

separate swapped memory account from zone->used_mem because swapped ttm pages can be flushed into SWAP disk/file under high memory pressure.

add check conditon in ttm_mem_global_reserve to prevent triggering OOM. because if SWAP disk/file is full, all swapped ttm page would stay in system memory which can't be flushed into disk/file. At this time, any memory request will trigger OOM killer.

v2: always get total system swap size rather than getting it once at module init time

---
  drivers/gpu/drm/ttm/ttm_memory.c | 24 ++++++++++++++++--------
  drivers/gpu/drm/ttm/ttm_tt.c     | 13 +++++++++++--
  include/drm/ttm/ttm_memory.h     |  2 ++
  3 files changed, 29 insertions(+), 10 deletions(-)
drivers/gpu/drm/ttm/ttm_memory.c +1 -0
index aa0c381..06024ba 100644
@@ -36,6 +36,7 @@
  #include <linux/mm.h>
  #include <linux/module.h>
  #include <linux/slab.h>
+#include <linux/swap.h>
 
  #define TTM_MEMORY_ALLOC_RETRIES 4
 

drivers/gpu/drm/ttm/ttm_memory.c static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob, · line 187

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_memory.c +1 -1
@@ -183,20 +184,20 @@ static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob,
  {
  	unsigned int i;
  	struct ttm_mem_zone *zone;
-	uint64_t target;
+	uint64_t target, limit;

Don't need limit variable.

drivers/gpu/drm/ttm/ttm_memory.c static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob, · line 193

Quoted 6 lines
drivers/gpu/drm/ttm/ttm_memory.c +2 -0
  	for (i = 0; i < glob->num_zones; ++i) {
  		zone = glob->zones[i];
 
+		limit = (capable(CAP_SYS_ADMIN)) ?
+				zone->emer_mem : zone->max_mem;

directly target = ...

drivers/gpu/drm/ttm/ttm_memory.c static bool ttm_zones_above_swap_target(struct ttm_mem_global *glob, · line 198

Quoted 8 lines
drivers/gpu/drm/ttm/ttm_memory.c +2 -3
+
  		if (from_wq)
  			target = zone->swap_limit;
-		else if (capable(CAP_SYS_ADMIN))
-			target = zone->emer_mem;
  		else
-			target = zone->max_mem;
+			target = limit;

Don't need else case.

Quoted 32 lines
drivers/gpu/drm/ttm/ttm_memory.c +7 -3
  		target = (extra > target) ? 0ULL : target;
-
  		if (zone->used_mem > target)
  			return true;
  	}
@@ -373,6 +374,7 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
  		return ret;
  	}
 
+	atomic64_set(&glob->swap_glob_mem, 0);
  	si_meminfo(&si);
 
  	ret = ttm_mem_init_kernel_zone(glob, &si);
@@ -473,10 +475,14 @@ static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
  				  struct ttm_mem_zone *single_zone,
  				  uint64_t amount, bool reserve)
  {
-	uint64_t limit;
+	uint64_t swap_glob_mem, max_swap_cache;
+	uint64_t limit, total_used_mem;
+	struct ttm_mem_zone *zone;
  	int ret = -ENOMEM;
  	unsigned int i;
-	struct ttm_mem_zone *zone;
+
+	swap_glob_mem = atomic64_read(&glob->swap_glob_mem);
+	max_swap_cache = (get_total_swap_pages() >> 1) << PAGE_SHIFT;
 
  	spin_lock(&glob->lock);
  	for (i = 0; i < glob->num_zones; ++i) {

drivers/gpu/drm/ttm/ttm_memory.c static int ttm_mem_global_reserve(struct ttm_mem_global *glob, · line 496

Quoted 5 lines
drivers/gpu/drm/ttm/ttm_memory.c +1 -1
@@ -487,7 +493,9 @@ static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
  		limit = (capable(CAP_SYS_ADMIN)) ?
  			zone->emer_mem : zone->max_mem;
 
-		if (zone->used_mem > limit)
+		total_used_mem = zone->used_mem + swap_glob_mem;

Good catch for total used mem.

Regards,
David Zhou

Quoted 58 lines
drivers/gpu/drm/ttm/ttm_memory.c +2 -0
+		limit += max_swap_cache;
+		if (total_used_mem > limit)
  			goto out_unlock;
  	}
 
drivers/gpu/drm/ttm/ttm_tt.c +11 -2
index 9e4d43d..395cac0 100644
@@ -177,8 +177,11 @@ void ttm_tt_destroy(struct ttm_tt *ttm)
  		ttm_tt_unpopulate(ttm);
 
  	if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
-	    ttm->swap_storage)
+	    ttm->swap_storage) {
  		fput(ttm->swap_storage);
+		atomic64_sub_return(ttm->num_pages << PAGE_SHIFT,
+				    &ttm->glob->mem_glob->swap_glob_mem);
+	}
 
  	ttm->swap_storage = NULL;
  	ttm->func->destroy(ttm);
@@ -318,8 +321,11 @@ int ttm_tt_swapin(struct ttm_tt *ttm)
  		put_page(from_page);
  	}
 
-	if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
+	if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP)) {
  		fput(swap_storage);
+		atomic64_sub_return(ttm->num_pages << PAGE_SHIFT,
+				    &ttm->glob->mem_glob->swap_glob_mem);
+	}
  	ttm->swap_storage = NULL;
  	ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
 
@@ -378,6 +384,9 @@ int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
  	ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
  	if (persistent_swap_storage)
  		ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
+	else
+		atomic64_add_return(ttm->num_pages << PAGE_SHIFT,
+			    &ttm->glob->mem_glob->swap_glob_mem);
 
  	return 0;
  out_err:
include/drm/ttm/ttm_memory.h +1 -0
index 8936285..a273581 100644
@@ -49,6 +49,7 @@
   * @work: The workqueue callback for the shrink queue.
   * @lock: Lock to protect the @shrink - and the memory accounting members,
   * that is, essentially the whole structure with some exceptions.
+ * @swap_glob_mem: total size of ttm page which has been swapped out
   * @zones: Array of pointers to accounting zones.
   * @num_zones: Number of populated entries in the @zones array.
   * @zone_kernel: Pointer to the kernel zone.

include/drm/ttm/ttm_memory.h struct ttm_mem_global { · line 74

Quoted 7 lines
include/drm/ttm/ttm_memory.h +1 -0
@@ -67,6 +68,7 @@ struct ttm_mem_global {
  	struct workqueue_struct *swap_queue;
  	struct work_struct work;
  	spinlock_t lock;
+	atomic64_t swap_glob_mem;
  	struct ttm_mem_zone *zones[TTM_MEM_MAX_ZONES];
  	unsigned int num_zones;
  	struct ttm_mem_zone *zone_kernel;

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel