From: Chunming Zhou Subject: Re: [PATCH 1/5] drm/ttm: add max_swap_mem in ttm_mem_global Date: Fri, 2 Feb 2018 15:38:02 +0800 Message-ID: References: <1517556145-23505-1-git-send-email-Hongbo.He@amd.com> <037a126e-b084-b5b9-7f7a-80006d84388d@amd.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============0108245983==" Return-path: Received: from NAM03-CO1-obe.outbound.protection.outlook.com (mail-co1nam03on0063.outbound.protection.outlook.com [104.47.40.63]) by gabe.freedesktop.org (Postfix) with ESMTPS id EB66B6EFB1 for ; Fri, 2 Feb 2018 07:38:12 +0000 (UTC) In-Reply-To: <037a126e-b084-b5b9-7f7a-80006d84388d@amd.com> Content-Language: en-US List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" To: Roger He , dri-devel@lists.freedesktop.org Cc: Christian.Koenig@amd.com List-Id: dri-devel@lists.freedesktop.org --===============0108245983== Content-Type: multipart/alternative; boundary="------------A0BDDACB4E19A382DD37AB50" Content-Language: en-US --------------A0BDDACB4E19A382DD37AB50 Content-Type: text/plain; charset="utf-8"; format=flowed Content-Transfer-Encoding: 8bit On 2018年02月02日 15:34, Chunming Zhou wrote: > > > On 2018年02月02日 15:22, Roger He wrote: >> set its initial value as 1/2 * free swap cache size when module initial. >> and adjust this value when allocate TTM memory >> >> Signed-off-by: Roger He >> --- >>   drivers/gpu/drm/ttm/ttm_memory.c | 10 ++++++++-- >>   include/drm/ttm/ttm_memory.h     |  2 ++ >>   2 files changed, 10 insertions(+), 2 deletions(-) >> >> diff --git a/drivers/gpu/drm/ttm/ttm_memory.c >> b/drivers/gpu/drm/ttm/ttm_memory.c >> index aa0c381..b48931d 100644 >> --- a/drivers/gpu/drm/ttm/ttm_memory.c >> +++ b/drivers/gpu/drm/ttm/ttm_memory.c >> @@ -36,6 +36,7 @@ >>   #include >>   #include >>   #include >> +#include >>     #define TTM_MEMORY_ALLOC_RETRIES 4 >>   @@ -372,9 +373,9 @@ int ttm_mem_global_init(struct ttm_mem_global >> *glob) >>           kobject_put(&glob->kobj); >>           return ret; >>       } >> - >> +    /* set it as 1/2 * swap free space we can get at that time */ >> +    glob->max_swap_mem = get_nr_swap_pages() << (PAGE_SHIFT - 1); >>       si_meminfo(&si); Hi Roger, I just find si_meminfo can get total swap size, see struct sysinfo definition:  struct sysinfo {         __kernel_long_t uptime;         /* Seconds since boot */         __kernel_ulong_t loads[3];      /* 1, 5, and 15 minute load averages */         __kernel_ulong_t totalram;      /* Total usable main memory size */         __kernel_ulong_t freeram;       /* Available memory size */         __kernel_ulong_t sharedram;     /* Amount of shared memory */         __kernel_ulong_t bufferram;     /* Memory used by buffers */         __kernel_ulong_t totalswap;     /* Total swap space size */         __kernel_ulong_t freeswap;      /* swap space still available */         __u16 procs;                    /* Number of current processes */ ... can sysinfo.totalswap be used for your change? Regards, David Zhou >> - >>       ret = ttm_mem_init_kernel_zone(glob, &si); >>       if (unlikely(ret != 0)) >>           goto out_no_zone; >> @@ -473,12 +474,17 @@ static int ttm_mem_global_reserve(struct >> ttm_mem_global *glob, >>                     struct ttm_mem_zone *single_zone, >>                     uint64_t amount, bool reserve) >>   { >> +    uint64_t free_swap_mem = get_nr_swap_pages() << (PAGE_SHIFT - 1); >>       uint64_t limit; >>       int ret = -ENOMEM; >>       unsigned int i; >>       struct ttm_mem_zone *zone; >>         spin_lock(&glob->lock); >> +    /* adjust the max_swap_mem to cover the new inserted swap space */ >> +    if (glob->max_swap_mem < free_swap_mem) >> +        glob->max_swap_mem = free_swap_mem; > Seems using max() for exchange is more obvious, otherwise looks ok to me. > > Regards, > David Zhou >> + >>       for (i = 0; i < glob->num_zones; ++i) { >>           zone = glob->zones[i]; >>           if (single_zone && zone != single_zone) >> diff --git a/include/drm/ttm/ttm_memory.h b/include/drm/ttm/ttm_memory.h >> index 8936285..ad5a557 100644 >> --- a/include/drm/ttm/ttm_memory.h >> +++ b/include/drm/ttm/ttm_memory.h >> @@ -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. >> + * @max_swap_mem: upper limit of swap space TTM can use >>    * @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; >> +    uint64_t max_swap_mem; >>       struct ttm_mem_zone *zones[TTM_MEM_MAX_ZONES]; >>       unsigned int num_zones; >>       struct ttm_mem_zone *zone_kernel; > --------------A0BDDACB4E19A382DD37AB50 Content-Type: text/html; charset="utf-8" Content-Transfer-Encoding: 8bit



On 2018年02月02日 15:34, Chunming Zhou wrote:


On 2018年02月02日 15:22, Roger He wrote:
set its initial value as 1/2 * free swap cache size when module initial.
and adjust this value when allocate TTM memory

Signed-off-by: Roger He <Hongbo.He@amd.com>
---
  drivers/gpu/drm/ttm/ttm_memory.c | 10 ++++++++--
  include/drm/ttm/ttm_memory.h     |  2 ++
  2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/ttm/ttm_memory.c b/drivers/gpu/drm/ttm/ttm_memory.c
index aa0c381..b48931d 100644
--- a/drivers/gpu/drm/ttm/ttm_memory.c
+++ b/drivers/gpu/drm/ttm/ttm_memory.c
@@ -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
  @@ -372,9 +373,9 @@ int ttm_mem_global_init(struct ttm_mem_global *glob)
          kobject_put(&glob->kobj);
          return ret;
      }
-
+    /* set it as 1/2 * swap free space we can get at that time */
+    glob->max_swap_mem = get_nr_swap_pages() << (PAGE_SHIFT - 1);
      si_meminfo(&si);
Hi Roger,

I just find si_meminfo can get total swap size, see struct sysinfo definition:
 struct sysinfo {
        __kernel_long_t uptime;         /* Seconds since boot */
        __kernel_ulong_t loads[3];      /* 1, 5, and 15 minute load averages */
        __kernel_ulong_t totalram;      /* Total usable main memory size */
        __kernel_ulong_t freeram;       /* Available memory size */
        __kernel_ulong_t sharedram;     /* Amount of shared memory */
        __kernel_ulong_t bufferram;     /* Memory used by buffers */
        __kernel_ulong_t totalswap;     /* Total swap space size */
        __kernel_ulong_t freeswap;      /* swap space still available */

        __u16 procs;                    /* Number of current processes */
...

can sysinfo.totalswap be used for your change?

Regards,
David Zhou
-
      ret = ttm_mem_init_kernel_zone(glob, &si);
      if (unlikely(ret != 0))
          goto out_no_zone;
@@ -473,12 +474,17 @@ static int ttm_mem_global_reserve(struct ttm_mem_global *glob,
                    struct ttm_mem_zone *single_zone,
                    uint64_t amount, bool reserve)
  {
+    uint64_t free_swap_mem = get_nr_swap_pages() << (PAGE_SHIFT - 1);
      uint64_t limit;
      int ret = -ENOMEM;
      unsigned int i;
      struct ttm_mem_zone *zone;
        spin_lock(&glob->lock);
+    /* adjust the max_swap_mem to cover the new inserted swap space */
+    if (glob->max_swap_mem < free_swap_mem)
+        glob->max_swap_mem = free_swap_mem;
Seems using max() for exchange is more obvious, otherwise looks ok to me.

Regards,
David Zhou
+
      for (i = 0; i < glob->num_zones; ++i) {
          zone = glob->zones[i];
          if (single_zone && zone != single_zone)
diff --git a/include/drm/ttm/ttm_memory.h b/include/drm/ttm/ttm_memory.h
index 8936285..ad5a557 100644
--- a/include/drm/ttm/ttm_memory.h
+++ b/include/drm/ttm/ttm_memory.h
@@ -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.
+ * @max_swap_mem: upper limit of swap space TTM can use
   * @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;
+    uint64_t max_swap_mem;
      struct ttm_mem_zone *zones[TTM_MEM_MAX_ZONES];
      unsigned int num_zones;
      struct ttm_mem_zone *zone_kernel;


--------------A0BDDACB4E19A382DD37AB50-- --===============0108245983== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KZHJpLWRldmVs IG1haWxpbmcgbGlzdApkcmktZGV2ZWxAbGlzdHMuZnJlZWRlc2t0b3Aub3JnCmh0dHBzOi8vbGlz dHMuZnJlZWRlc2t0b3Aub3JnL21haWxtYW4vbGlzdGluZm8vZHJpLWRldmVsCg== --===============0108245983==--