Re: [RFC][PATCH] memcg: avoid THP split in task migration

From: Hillf Danton
Date: Wed Feb 29 2012 - 09:40:13 EST


On Wed, Feb 29, 2012 at 8:28 AM, KAMEZAWA Hiroyuki
<kamezawa.hiroyu@xxxxxxxxxxxxxx> wrote:
> On Tue, 28 Feb 2012 16:12:32 -0500
> Naoya Horiguchi <n-horiguchi@xxxxxxxxxxxxx> wrote:
>
>> Currently we can't do task migration among memory cgroups without THP split,
>> which means processes heavily using THP experience large overhead in task
>> migration. This patch introduce the code for moving charge of THP and makes
>> THP more valuable.
>>
>> Signed-off-by: Naoya Horiguchi <n-horiguchi@xxxxxxxxxxxxx>
>> Cc: Hillf Danton <dhillf@xxxxxxxxx>
>
>
> Thank you!

++hd;

>
> A comment below.
>
>> ---
>> Âmm/memcontrol.c | Â 76 ++++++++++++++++++++++++++++++++++++++++++++++++++----
>> Â1 files changed, 70 insertions(+), 6 deletions(-)
>>
>> diff --git linux-next-20120228.orig/mm/memcontrol.c linux-next-20120228/mm/memcontrol.c
>> index c83aeb5..e97c041 100644
>> --- linux-next-20120228.orig/mm/memcontrol.c
>> +++ linux-next-20120228/mm/memcontrol.c
>> @@ -5211,6 +5211,42 @@ static int is_target_pte_for_mc(struct vm_area_struct *vma,
>> Â Â Â return ret;
>> Â}
>>
>> +#ifdef CONFIG_TRANSPARENT_HUGEPAGE
>> +/*
>> + * We don't consider swapping or file mapped pages because THP does not
>> + * support them for now.
>> + */
>> +static int is_target_huge_pmd_for_mc(struct vm_area_struct *vma,

static int is_target_thp_for_mc(struct vm_area_struct *vma,
or
static int is_target_pmd_for_mc(struct vm_area_struct *vma,
sounds better?

>> + Â Â Â Â Â Â unsigned long addr, pmd_t pmd, union mc_target *target)
>> +{
>> + Â Â struct page *page = NULL;
>> + Â Â struct page_cgroup *pc;
>> + Â Â int ret = 0;
>> +
>> + Â Â if (pmd_present(pmd))
>> + Â Â Â Â Â Â page = pmd_page(pmd);
>> + Â Â if (!page)
>> + Â Â Â Â Â Â return 0;
>> + Â Â VM_BUG_ON(!PageHead(page));

With a huge and stable pmd, the above operations on page could be
compacted into one line?

page = pmd_page(pmd);

>> + Â Â get_page(page);
>> + Â Â pc = lookup_page_cgroup(page);
>> + Â Â if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
>> + Â Â Â Â Â Â ret = MC_TARGET_PAGE;
>> + Â Â Â Â Â Â if (target)

After checking target, looks only get_page() needed?

>> + Â Â Â Â Â Â Â Â Â Â target->page = page;
>> + Â Â }
>> + Â Â if (!ret || !target)
>> + Â Â Â Â Â Â put_page(page);
>> + Â Â return ret;
>> +}
>> +#else
>> +static inline int is_target_huge_pmd_for_mc(struct vm_area_struct *vma,
>> + Â Â Â Â Â Â unsigned long addr, pmd_t pmd, union mc_target *target)
>> +{
>> + Â Â return 0;
>> +}
>> +#endif
>> +
>> Âstatic int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â unsigned long addr, unsigned long end,
>> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â struct mm_walk *walk)
>> @@ -5219,7 +5255,13 @@ static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
>> Â Â Â pte_t *pte;
>> Â Â Â spinlock_t *ptl;
>>
>> - Â Â split_huge_page_pmd(walk->mm, pmd);
>> + Â Â if (pmd_trans_huge_lock(pmd, vma) == 1) {
>> + Â Â Â Â Â Â if (is_target_huge_pmd_for_mc(vma, addr, *pmd, NULL))

if (is_target_huge_pmd_for_mc(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
looks clearer

>> + Â Â Â Â Â Â Â Â Â Â mc.precharge += HPAGE_PMD_NR;

As HPAGE_PMD_NR is directly used, compiler beeps if THP disabled, I guess.

If yes, please cleanup huge_mm.h with s/BUG()/BUILD_BUG()/ and with
both HPAGE_PMD_ORDER and HPAGE_PMD_NR also defined,
to easy others a bit.

>> + Â Â Â Â Â Â spin_unlock(&walk->mm->page_table_lock);

spin_unlock(&vma->mm->page_table_lock);
looks clearer
>> + Â Â Â Â Â Â cond_resched();
>> + Â Â Â Â Â Â return 0;
>> + Â Â }
>>
>> Â Â Â pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
>> Â Â Â for (; addr != end; pte++, addr += PAGE_SIZE)
>> @@ -5378,16 +5420,38 @@ static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
>> Â Â Â struct vm_area_struct *vma = walk->private;
>> Â Â Â pte_t *pte;
>> Â Â Â spinlock_t *ptl;
>> + Â Â int type;
>> + Â Â union mc_target target;
>> + Â Â struct page *page;
>> + Â Â struct page_cgroup *pc;
>> +
>> + Â Â if (pmd_trans_huge_lock(pmd, vma) == 1) {
>> + Â Â Â Â Â Â if (!mc.precharge)
>> + Â Â Â Â Â Â Â Â Â Â return 0;

Bang, return without page table lock released.

>> + Â Â Â Â Â Â type = is_target_huge_pmd_for_mc(vma, addr, *pmd, &target);
>> + Â Â Â Â Â Â if (type == MC_TARGET_PAGE) {
>> + Â Â Â Â Â Â Â Â Â Â page = target.page;
>> + Â Â Â Â Â Â Â Â Â Â if (!isolate_lru_page(page)) {
>> + Â Â Â Â Â Â Â Â Â Â Â Â Â Â pc = lookup_page_cgroup(page);
>
> Here is a diffuclut point. Please see mem_cgroup_split_huge_fixup(). It splits

Hard and hard point IMO.

> updates memcg's status of splitted pages under lru_lock and compound_lock
> but not under mm->page_table_lock.
>
> Looking into split_huge_page()
>
> Â Â Â Âsplit_huge_page() Â# take anon_vma lock
> Â Â Â Â Â Â Â Â__split_huge_page()
> Â Â Â Â Â Â Â Â Â Â Â Â__split_huge_page_refcount() # take lru_lock, compound_lock.
> Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Âmem_cgroup_split_huge_fixup()
> Â Â Â Â Â Â Â Â Â Â Â Â__split_huge_page_map() # take page table lock.
>
[copied from Naoya-san's reply]

> I'm afraid this callchain is not correct.

s/correct/complete/

> Page table lock seems to be taken before we enter the main split work.
>
> Â Âsplit_huge_page
> Â Â Â Âtake anon_vma lock
> Â Â Â Â__split_huge_page
> Â Â Â Â Â Â__split_huge_page_splitting
>        Âlock page_table_lock   <--- *1
> Â Â Â Â Â Â Â Âpage_check_address_pmd
> Â Â Â Â Â Â Â Âunlock page_table_lock

Yeah, splitters are blocked.
Plus from the *ugly* documented lock function(another
cleanup needed), the embedded mmap_sem also blocks splitters.

That said, could we simply wait and see results of test cases?

-hd

/* mmap_sem must be held on entry */
static inline int pmd_trans_huge_lock(pmd_t *pmd,
struct vm_area_struct *vma)
{
VM_BUG_ON(!rwsem_is_locked(&vma->vm_mm->mmap_sem));
if (pmd_trans_huge(*pmd))
return __pmd_trans_huge_lock(pmd, vma);
else
return 0;
}

> Â Â Â Â Â Â__split_huge_page_refcount
> Â Â Â Â Â Â Â Âlock lru_lock
> Â Â Â Â Â Â Â Âcompound_lock
> Â Â Â Â Â Â Â Âmem_cgroup_split_huge_fixup
> Â Â Â Â Â Â Â Âcompound_unlock
> Â Â Â Â Â Â Â Âunlock lru_lock
> Â Â Â Â Â Â__split_huge_page_map
> Â Â Â Â Â Â Â Âlock page_table_lock
> Â Â Â Â Â Â Â Â... some work
> Â Â Â Â Â Â Â Âunlock page_table_lock
> Â Â Â Âunlock anon_vma lock
>
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/