Re: [PATCH -mm -v7 2/9] mm, memcg: Support to charge/uncharge multiple swap entries

2017-03-31 Thread Johannes Weiner
On Thu, Mar 30, 2017 at 08:53:50AM +0800, Huang, Ying wrote:
> Johannes Weiner  writes:
> > but there doesn't seem to be a reason to
> > pass @nr_entries when we have the struct page. Why can't this function
> > just check PageTransHuge() by itself?
> 
> Because sometimes we need to charge one swap entry for a THP.  Please
> take a look at the original add_to_swap() implementation.  For a THP,
> one swap entry will be allocated and charged to the mem cgroup before
> the THP is split.  And I think it is not easy to change this, because we
> don't want to split THP if the mem cgroup for swap exceeds its limit.

I think we do. Let's continue this discussion in the 9/9 subthread.


Re: [PATCH -mm -v7 2/9] mm, memcg: Support to charge/uncharge multiple swap entries

2017-03-31 Thread Johannes Weiner
On Thu, Mar 30, 2017 at 08:53:50AM +0800, Huang, Ying wrote:
> Johannes Weiner  writes:
> > but there doesn't seem to be a reason to
> > pass @nr_entries when we have the struct page. Why can't this function
> > just check PageTransHuge() by itself?
> 
> Because sometimes we need to charge one swap entry for a THP.  Please
> take a look at the original add_to_swap() implementation.  For a THP,
> one swap entry will be allocated and charged to the mem cgroup before
> the THP is split.  And I think it is not easy to change this, because we
> don't want to split THP if the mem cgroup for swap exceeds its limit.

I think we do. Let's continue this discussion in the 9/9 subthread.


Re: [PATCH -mm -v7 2/9] mm, memcg: Support to charge/uncharge multiple swap entries

2017-03-29 Thread Huang, Ying
Johannes Weiner  writes:

> On Tue, Mar 28, 2017 at 01:32:02PM +0800, Huang, Ying wrote:
>> @@ -5908,16 +5907,19 @@ void mem_cgroup_swapout(struct page *page, 
>> swp_entry_t entry)
>>  css_put(>css);
>>  }
>>  
>> -/*
>> - * mem_cgroup_try_charge_swap - try charging a swap entry
>> +/**
>> + * mem_cgroup_try_charge_swap - try charging a set of swap entries
>>   * @page: page being added to swap
>> - * @entry: swap entry to charge
>> + * @entry: the first swap entry to charge
>> + * @nr_entries: the number of swap entries to charge
>>   *
>> - * Try to charge @entry to the memcg that @page belongs to.
>> + * Try to charge @nr_entries swap entries starting from @entry to the
>> + * memcg that @page belongs to.
>>   *
>>   * Returns 0 on success, -ENOMEM on failure.
>>   */
>> -int mem_cgroup_try_charge_swap(struct page *page, swp_entry_t entry)
>> +int mem_cgroup_try_charge_swap(struct page *page, swp_entry_t entry,
>> +   unsigned int nr_entries)
>
> I've pointed this out before,

Yes.  And I have replied to your original comments too :-)

> but there doesn't seem to be a reason to
> pass @nr_entries when we have the struct page. Why can't this function
> just check PageTransHuge() by itself?

Because sometimes we need to charge one swap entry for a THP.  Please
take a look at the original add_to_swap() implementation.  For a THP,
one swap entry will be allocated and charged to the mem cgroup before
the THP is split.  And I think it is not easy to change this, because we
don't want to split THP if the mem cgroup for swap exceeds its limit.

Best Regards,
Huang, Ying


Re: [PATCH -mm -v7 2/9] mm, memcg: Support to charge/uncharge multiple swap entries

2017-03-29 Thread Huang, Ying
Johannes Weiner  writes:

> On Tue, Mar 28, 2017 at 01:32:02PM +0800, Huang, Ying wrote:
>> @@ -5908,16 +5907,19 @@ void mem_cgroup_swapout(struct page *page, 
>> swp_entry_t entry)
>>  css_put(>css);
>>  }
>>  
>> -/*
>> - * mem_cgroup_try_charge_swap - try charging a swap entry
>> +/**
>> + * mem_cgroup_try_charge_swap - try charging a set of swap entries
>>   * @page: page being added to swap
>> - * @entry: swap entry to charge
>> + * @entry: the first swap entry to charge
>> + * @nr_entries: the number of swap entries to charge
>>   *
>> - * Try to charge @entry to the memcg that @page belongs to.
>> + * Try to charge @nr_entries swap entries starting from @entry to the
>> + * memcg that @page belongs to.
>>   *
>>   * Returns 0 on success, -ENOMEM on failure.
>>   */
>> -int mem_cgroup_try_charge_swap(struct page *page, swp_entry_t entry)
>> +int mem_cgroup_try_charge_swap(struct page *page, swp_entry_t entry,
>> +   unsigned int nr_entries)
>
> I've pointed this out before,

Yes.  And I have replied to your original comments too :-)

> but there doesn't seem to be a reason to
> pass @nr_entries when we have the struct page. Why can't this function
> just check PageTransHuge() by itself?

Because sometimes we need to charge one swap entry for a THP.  Please
take a look at the original add_to_swap() implementation.  For a THP,
one swap entry will be allocated and charged to the mem cgroup before
the THP is split.  And I think it is not easy to change this, because we
don't want to split THP if the mem cgroup for swap exceeds its limit.

Best Regards,
Huang, Ying


Re: [PATCH -mm -v7 2/9] mm, memcg: Support to charge/uncharge multiple swap entries

2017-03-29 Thread Johannes Weiner
On Tue, Mar 28, 2017 at 01:32:02PM +0800, Huang, Ying wrote:
> @@ -5908,16 +5907,19 @@ void mem_cgroup_swapout(struct page *page, 
> swp_entry_t entry)
>   css_put(>css);
>  }
>  
> -/*
> - * mem_cgroup_try_charge_swap - try charging a swap entry
> +/**
> + * mem_cgroup_try_charge_swap - try charging a set of swap entries
>   * @page: page being added to swap
> - * @entry: swap entry to charge
> + * @entry: the first swap entry to charge
> + * @nr_entries: the number of swap entries to charge
>   *
> - * Try to charge @entry to the memcg that @page belongs to.
> + * Try to charge @nr_entries swap entries starting from @entry to the
> + * memcg that @page belongs to.
>   *
>   * Returns 0 on success, -ENOMEM on failure.
>   */
> -int mem_cgroup_try_charge_swap(struct page *page, swp_entry_t entry)
> +int mem_cgroup_try_charge_swap(struct page *page, swp_entry_t entry,
> +unsigned int nr_entries)

I've pointed this out before, but there doesn't seem to be a reason to
pass @nr_entries when we have the struct page. Why can't this function
just check PageTransHuge() by itself?


Re: [PATCH -mm -v7 2/9] mm, memcg: Support to charge/uncharge multiple swap entries

2017-03-29 Thread Johannes Weiner
On Tue, Mar 28, 2017 at 01:32:02PM +0800, Huang, Ying wrote:
> @@ -5908,16 +5907,19 @@ void mem_cgroup_swapout(struct page *page, 
> swp_entry_t entry)
>   css_put(>css);
>  }
>  
> -/*
> - * mem_cgroup_try_charge_swap - try charging a swap entry
> +/**
> + * mem_cgroup_try_charge_swap - try charging a set of swap entries
>   * @page: page being added to swap
> - * @entry: swap entry to charge
> + * @entry: the first swap entry to charge
> + * @nr_entries: the number of swap entries to charge
>   *
> - * Try to charge @entry to the memcg that @page belongs to.
> + * Try to charge @nr_entries swap entries starting from @entry to the
> + * memcg that @page belongs to.
>   *
>   * Returns 0 on success, -ENOMEM on failure.
>   */
> -int mem_cgroup_try_charge_swap(struct page *page, swp_entry_t entry)
> +int mem_cgroup_try_charge_swap(struct page *page, swp_entry_t entry,
> +unsigned int nr_entries)

I've pointed this out before, but there doesn't seem to be a reason to
pass @nr_entries when we have the struct page. Why can't this function
just check PageTransHuge() by itself?


[PATCH -mm -v7 2/9] mm, memcg: Support to charge/uncharge multiple swap entries

2017-03-27 Thread Huang, Ying
From: Huang Ying 

This patch make it possible to charge or uncharge a set of continuous
swap entries in the swap cgroup.  The number of swap entries is
specified via an added parameter.

This will be used for the THP (Transparent Huge Page) swap support.
Where a swap cluster backing a THP may be allocated and freed as a
whole.  So a set of (HPAGE_PMD_NR) continuous swap entries backing one
THP need to be charged or uncharged together.  This will batch the
cgroup operations for the THP swap too.

Cc: Andrea Arcangeli 
Cc: Kirill A. Shutemov 
Cc: Vladimir Davydov 
Cc: Johannes Weiner 
Cc: Michal Hocko 
Cc: Tejun Heo 
Cc: cgro...@vger.kernel.org
Signed-off-by: "Huang, Ying" 
---
 include/linux/swap.h| 12 ++
 include/linux/swap_cgroup.h |  6 +++--
 mm/memcontrol.c | 57 +
 mm/shmem.c  |  2 +-
 mm/swap_cgroup.c| 40 +++
 mm/swap_state.c |  2 +-
 mm/swapfile.c   |  2 +-
 7 files changed, 77 insertions(+), 44 deletions(-)

diff --git a/include/linux/swap.h b/include/linux/swap.h
index 486494e6b2fc..278e1349a424 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -550,8 +550,10 @@ static inline int mem_cgroup_swappiness(struct mem_cgroup 
*mem)
 
 #ifdef CONFIG_MEMCG_SWAP
 extern void mem_cgroup_swapout(struct page *page, swp_entry_t entry);
-extern int mem_cgroup_try_charge_swap(struct page *page, swp_entry_t entry);
-extern void mem_cgroup_uncharge_swap(swp_entry_t entry);
+extern int mem_cgroup_try_charge_swap(struct page *page, swp_entry_t entry,
+ unsigned int nr_entries);
+extern void mem_cgroup_uncharge_swap(swp_entry_t entry,
+unsigned int nr_entries);
 extern long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg);
 extern bool mem_cgroup_swap_full(struct page *page);
 #else
@@ -560,12 +562,14 @@ static inline void mem_cgroup_swapout(struct page *page, 
swp_entry_t entry)
 }
 
 static inline int mem_cgroup_try_charge_swap(struct page *page,
-swp_entry_t entry)
+swp_entry_t entry,
+unsigned int nr_entries)
 {
return 0;
 }
 
-static inline void mem_cgroup_uncharge_swap(swp_entry_t entry)
+static inline void mem_cgroup_uncharge_swap(swp_entry_t entry,
+   unsigned int nr_entries)
 {
 }
 
diff --git a/include/linux/swap_cgroup.h b/include/linux/swap_cgroup.h
index 145306bdc92f..b2b8ec7bda3f 100644
--- a/include/linux/swap_cgroup.h
+++ b/include/linux/swap_cgroup.h
@@ -7,7 +7,8 @@
 
 extern unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
unsigned short old, unsigned short new);
-extern unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id);
+extern unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id,
+unsigned int nr_ents);
 extern unsigned short lookup_swap_cgroup_id(swp_entry_t ent);
 extern int swap_cgroup_swapon(int type, unsigned long max_pages);
 extern void swap_cgroup_swapoff(int type);
@@ -15,7 +16,8 @@ extern void swap_cgroup_swapoff(int type);
 #else
 
 static inline
-unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
+unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id,
+ unsigned int nr_ents)
 {
return 0;
 }
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 490d5b4676c1..13ee82fe81c8 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2393,10 +2393,9 @@ void mem_cgroup_split_huge_fixup(struct page *head)
 
 #ifdef CONFIG_MEMCG_SWAP
 static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
-bool charge)
+  int nr_entries)
 {
-   int val = (charge) ? 1 : -1;
-   this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
+   this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], nr_entries);
 }
 
 /**
@@ -2422,8 +2421,8 @@ static int mem_cgroup_move_swap_account(swp_entry_t entry,
new_id = mem_cgroup_id(to);
 
if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
-   mem_cgroup_swap_statistics(from, false);
-   mem_cgroup_swap_statistics(to, true);
+   mem_cgroup_swap_statistics(from, -1);
+   mem_cgroup_swap_statistics(to, 1);
return 0;
}
return -EINVAL;
@@ -5451,7 +5450,7 @@ void mem_cgroup_commit_charge(struct page *page, struct 
mem_cgroup *memcg,
 * let's not wait for it.  The page already received a
   

[PATCH -mm -v7 2/9] mm, memcg: Support to charge/uncharge multiple swap entries

2017-03-27 Thread Huang, Ying
From: Huang Ying 

This patch make it possible to charge or uncharge a set of continuous
swap entries in the swap cgroup.  The number of swap entries is
specified via an added parameter.

This will be used for the THP (Transparent Huge Page) swap support.
Where a swap cluster backing a THP may be allocated and freed as a
whole.  So a set of (HPAGE_PMD_NR) continuous swap entries backing one
THP need to be charged or uncharged together.  This will batch the
cgroup operations for the THP swap too.

Cc: Andrea Arcangeli 
Cc: Kirill A. Shutemov 
Cc: Vladimir Davydov 
Cc: Johannes Weiner 
Cc: Michal Hocko 
Cc: Tejun Heo 
Cc: cgro...@vger.kernel.org
Signed-off-by: "Huang, Ying" 
---
 include/linux/swap.h| 12 ++
 include/linux/swap_cgroup.h |  6 +++--
 mm/memcontrol.c | 57 +
 mm/shmem.c  |  2 +-
 mm/swap_cgroup.c| 40 +++
 mm/swap_state.c |  2 +-
 mm/swapfile.c   |  2 +-
 7 files changed, 77 insertions(+), 44 deletions(-)

diff --git a/include/linux/swap.h b/include/linux/swap.h
index 486494e6b2fc..278e1349a424 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -550,8 +550,10 @@ static inline int mem_cgroup_swappiness(struct mem_cgroup 
*mem)
 
 #ifdef CONFIG_MEMCG_SWAP
 extern void mem_cgroup_swapout(struct page *page, swp_entry_t entry);
-extern int mem_cgroup_try_charge_swap(struct page *page, swp_entry_t entry);
-extern void mem_cgroup_uncharge_swap(swp_entry_t entry);
+extern int mem_cgroup_try_charge_swap(struct page *page, swp_entry_t entry,
+ unsigned int nr_entries);
+extern void mem_cgroup_uncharge_swap(swp_entry_t entry,
+unsigned int nr_entries);
 extern long mem_cgroup_get_nr_swap_pages(struct mem_cgroup *memcg);
 extern bool mem_cgroup_swap_full(struct page *page);
 #else
@@ -560,12 +562,14 @@ static inline void mem_cgroup_swapout(struct page *page, 
swp_entry_t entry)
 }
 
 static inline int mem_cgroup_try_charge_swap(struct page *page,
-swp_entry_t entry)
+swp_entry_t entry,
+unsigned int nr_entries)
 {
return 0;
 }
 
-static inline void mem_cgroup_uncharge_swap(swp_entry_t entry)
+static inline void mem_cgroup_uncharge_swap(swp_entry_t entry,
+   unsigned int nr_entries)
 {
 }
 
diff --git a/include/linux/swap_cgroup.h b/include/linux/swap_cgroup.h
index 145306bdc92f..b2b8ec7bda3f 100644
--- a/include/linux/swap_cgroup.h
+++ b/include/linux/swap_cgroup.h
@@ -7,7 +7,8 @@
 
 extern unsigned short swap_cgroup_cmpxchg(swp_entry_t ent,
unsigned short old, unsigned short new);
-extern unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id);
+extern unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id,
+unsigned int nr_ents);
 extern unsigned short lookup_swap_cgroup_id(swp_entry_t ent);
 extern int swap_cgroup_swapon(int type, unsigned long max_pages);
 extern void swap_cgroup_swapoff(int type);
@@ -15,7 +16,8 @@ extern void swap_cgroup_swapoff(int type);
 #else
 
 static inline
-unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id)
+unsigned short swap_cgroup_record(swp_entry_t ent, unsigned short id,
+ unsigned int nr_ents)
 {
return 0;
 }
diff --git a/mm/memcontrol.c b/mm/memcontrol.c
index 490d5b4676c1..13ee82fe81c8 100644
--- a/mm/memcontrol.c
+++ b/mm/memcontrol.c
@@ -2393,10 +2393,9 @@ void mem_cgroup_split_huge_fixup(struct page *head)
 
 #ifdef CONFIG_MEMCG_SWAP
 static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
-bool charge)
+  int nr_entries)
 {
-   int val = (charge) ? 1 : -1;
-   this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
+   this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], nr_entries);
 }
 
 /**
@@ -2422,8 +2421,8 @@ static int mem_cgroup_move_swap_account(swp_entry_t entry,
new_id = mem_cgroup_id(to);
 
if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
-   mem_cgroup_swap_statistics(from, false);
-   mem_cgroup_swap_statistics(to, true);
+   mem_cgroup_swap_statistics(from, -1);
+   mem_cgroup_swap_statistics(to, 1);
return 0;
}
return -EINVAL;
@@ -5451,7 +5450,7 @@ void mem_cgroup_commit_charge(struct page *page, struct 
mem_cgroup *memcg,
 * let's not wait for it.  The page already received a
 * memory+swap charge, drop the swap entry duplicate.
 */
-   mem_cgroup_uncharge_swap(entry);
+