diff mbox

[1/5] drm/ttm: add max_swap_mem in ttm_mem_global

Message ID 1517556145-23505-1-git-send-email-Hongbo.He@amd.com (mailing list archive)
State New, archived
Headers show

Commit Message

He, Hongbo Feb. 2, 2018, 7:22 a.m. UTC
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(-)

Comments

Chunming Zhou Feb. 2, 2018, 7:34 a.m. UTC | #1
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);
> -
>   	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;
Chunming Zhou Feb. 2, 2018, 7:38 a.m. UTC | #2
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;
>
diff mbox

Patch

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);
-
 	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;
+
 	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;