From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 57C4CC47422 for ; Fri, 19 Jan 2024 02:05:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DDAE46B0081; Thu, 18 Jan 2024 21:05:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D8C5D6B0082; Thu, 18 Jan 2024 21:05:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C79C86B0085; Thu, 18 Jan 2024 21:05:24 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id B8B566B0081 for ; Thu, 18 Jan 2024 21:05:24 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 7E115C0660 for ; Fri, 19 Jan 2024 02:05:24 +0000 (UTC) X-FDA: 81694418568.05.B66B37A Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by imf19.hostedemail.com (Postfix) with ESMTP id 242E71A000B for ; Fri, 19 Jan 2024 02:05:20 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf19.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1705629922; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=6HZowCDUKbTNui0+4VUQFlKevLPD2Euqu7Sh3yGRugk=; b=uCX8mJWnYS0H7BBhqXfqVzMMRSwejoFygMVZX6yrDxZ38er9amsLgiJ9U385ewj4L1KRC4 RYMXQC6YfhN1j30ZeZhoV0Dv6uJF2rLk53oTXqV+14kxips89jAtqtUT06nLvdDtK1wdgp r6PtT62IUyK9JJQEDefdfpFzNO0daUU= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=none; dmarc=pass (policy=quarantine) header.from=huawei.com; spf=pass (imf19.hostedemail.com: domain of wangkefeng.wang@huawei.com designates 45.249.212.189 as permitted sender) smtp.mailfrom=wangkefeng.wang@huawei.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705629922; a=rsa-sha256; cv=none; b=HpC49PB4LO9r5eS1g5qyYoK+O3lfXLUcR3D1Mt7FT0Awiyl4rfUyPMxZwzLkHGcgnwRtwo Uz5T/NNdGi0zb0WNzsAwV3l86jA7aSYvuDSZinPvt/5RPdCyEb55UMG44S36jGxeTPFeKY /g5zkKY87hp8ihNLpv5Qq8egs73nO2s= Received: from mail.maildlp.com (unknown [172.19.162.254]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4TGNGP43ZBzNlLp; Fri, 19 Jan 2024 10:04:29 +0800 (CST) Received: from dggpemm100001.china.huawei.com (unknown [7.185.36.93]) by mail.maildlp.com (Postfix) with ESMTPS id A1C6A180076; Fri, 19 Jan 2024 10:05:16 +0800 (CST) Received: from [10.174.177.243] (10.174.177.243) by dggpemm100001.china.huawei.com (7.185.36.93) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Fri, 19 Jan 2024 10:05:16 +0800 Message-ID: Date: Fri, 19 Jan 2024 10:05:15 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] mm: memory: move mem_cgroup_charge() into alloc_anon_folio() Content-Language: en-US To: Michal Hocko CC: Andrew Morton , , , , Matthew Wilcox , David Hildenbrand References: <20240117103954.2756050-1-wangkefeng.wang@huawei.com> From: Kefeng Wang In-Reply-To: Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.174.177.243] X-ClientProxiedBy: dggems702-chm.china.huawei.com (10.3.19.179) To dggpemm100001.china.huawei.com (7.185.36.93) X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 242E71A000B X-Stat-Signature: h4fteirksw94x7tn3jc65fd9kcszgb1x X-HE-Tag: 1705629920-736412 X-HE-Meta: U2FsdGVkX1+IKf59EY+i/0vUimlNIPO91Uv6ikTthbBLu8GxV3VHSm/H0DMdRRkrLkXONOeYkLD2O3z8Ksf2zx6o+ripHQz6GtSQk76fY4EYL9Rb/9wpCAneUHz36rEHbWkwzF21BY1w/4KEWg3zxJ8DWP6fURFLhyP6SoO0qZB8wLlD2od1Gr6+sOTbWeyG9bOsVV/chyYORBV2ZDV3Tu3DGK/PJtY/XhZFoJGKdNXTqMFu5UQ1aq+mSHUrHPlVo1XAUQ+bUDPM2FOu5lR95l3XI4bIk+u/+42QKRYp5NRFMobTjG8jUAU8xLMCBpOsosJfxf6UsE8FWRslfl9uOJaPniVLhTHvkmn0pCM+m1AybbKy91J6SkQ7Tzb2PcwOh+kak9uNHjRSGWQ3yWYYGB3z55c94EowghVu4hKsWOcFdX319cj4nBu/3cNjM8lXmRljWkQKsejj9ffBelGYb6UyAI9vBMeDIsyXN70NMfyQ88nFIl1gv2aneKD6SIVvwrOSGkrou6FQ1wW0jLFFxLyI3oEDWy5dF1ExDAHhIW1syH3jgtoWAa86XmigjSAfPgOKGK57CptPyZzQJXm0Yk5z6X+nzTPKU5FhRvteVkmpwLoIL3W7exhJAz+WtMI3hm/AfXyxtgt36jbLIoOPUps/Ub8HU92KmCuugmdu6sM66ZNFjeIJVS0hRGXLb297L/Dx0Z5RZeZlxaUSWH4eWX4njX/qS9Be8HjhoZqAkqP02J/vs5DXGOk7eWXZXr9sxpqUWWkuVnjBskHKTZAPJJlYHMTbk6GgN4FTb4q8uDFXWflAoPbFo0Bl/U0k39k7mefXHPVS4lLcPG2UbAmEFS9VdGGv/J8El/q1bjbqJRcuzmjTc5o95WEaUsJfUyJc2AY6wWSaaOetp3bDOo2Jpq7vGm9KJHqJjQDYzt693IDqvhMkkePHMCnbv5c84U+IyJDN0Z2yiXokdKOgarO NcACjjOn A8KEw/uVu92FsCqy7wc633T4ikYe25O4YnWUlda7mmEBpfKXBBZ2XhxEyQVpabbliBaVhpygIHuU91Pjo5/LDN2lZXSFQkenKU3kp954YTQugKSOU4CmmYy/bX8DwxrY2EDO5Hv9KsQrdq+bfNTcqQg76ZEu7Ss5ni0VodZzcC5OOsEI72TzBxOekAUc5S6Zcj/UbMzqPopjCEQjUJ/XER74UGf1IfPDbmJEynhFW9iQZkTJawdTH6D+E5g== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On 2024/1/18 23:59, Michal Hocko wrote: > On Wed 17-01-24 18:39:54, Kefeng Wang wrote: >> mem_cgroup_charge() uses the GFP flags in a fairly sophisticated way. >> In addition to checking gfpflags_allow_blocking(), it pays attention >> to __GFP_NORETRY and __GFP_RETRY_MAYFAIL to ensure that processes within >> this memcg do not exceed their quotas. Using the same GFP flags ensures >> that we handle large anonymous folios correctly, including falling back >> to smaller orders when there is plenty of memory available in the system >> but this memcg is close to its limits. > > The changelog is not really clear in the actual problem you are trying > to fix. Is this pure consistency fix or have you actually seen any > misbehavior. From the patch I suspect you are interested in THPs much > more than regular order-0 pages because those are GFP_KERNEL like when > it comes to charging. THPs have a variety of options on how aggressive > the allocation should try. From that perspective NORETRY and > RETRY_MAYFAIL are not all that interesting because costly allocations > (which THPs are) already do imply MAYFAIL and NORETRY. I don't meet actual issue, it founds from code inspection. mTHP is introduced by Ryan(19eaf44954df "mm: thp: support allocation of anonymous multi-size THP"),so we have similar check for mTHP like PMD THP in alloc_anon_folio(), it will try to allocate large order folio below PMD_ORDER, and fallback to order-0 folio if fails, meanwhile, it get GFP flags from vma_thp_gfp_mask() according to user configuration like PMD THP allocation, so 1) the memory charge failure check should be moved into fallback logical, because it will make us to allocated as much as possible large order folio, although the memcg's memory usage is close to its limits. 2) using seem GFP flags for allocate/mem charge, be consistent with PMD THP firstly, in addition, according to GFP flag returned for vma_thp_gfp_mask(), GFP_TRANSHUGE_LIGHT could make us skip direct reclaim, _GFP_NORETRY will make us skip mem_cgroup_oom and won't kill any progress from large order folio charging. > > GFP_TRANSHUGE_LIGHT is more interesting though because those do not dive > into the direct reclaim at all. With the current code they will reclaim > charges to free up the space for the allocated THP page and that defeats > the light mode. I have a vague recollection of preparing a patch to We are interesting to GFP_TRANSHUGE_LIGHT and _GFP_NORETRY as mentioned above. > address that in the past. Let me have a look at the current code... Yes, commit 3b3636924dfe ("mm, memcg: sync allocation and memcg charge gfp flags for THP") for PMD THP from you :) > > ... So yes, we still do THP charging the way I remember > (do_huge_pmd_anonymous_page). Your patch touches handle_pte_fault -> > do_anonymous_page path which is not THP AFAICS. Or am I missing > something? mTHP is one kind of THP. Thanks. > >> Signed-off-by: Kefeng Wang >> --- >> v2: >> - fix built when !CONFIG_TRANSPARENT_HUGEPAGE >> - update changelog suggested by Matthew Wilcox >> >> mm/memory.c | 16 ++++++++-------- >> 1 file changed, 8 insertions(+), 8 deletions(-) >> >> diff --git a/mm/memory.c b/mm/memory.c >> index 5e88d5379127..551f0b21bc42 100644 >> --- a/mm/memory.c >> +++ b/mm/memory.c >> @@ -4153,8 +4153,8 @@ static bool pte_range_none(pte_t *pte, int nr_pages) >> >> static struct folio *alloc_anon_folio(struct vm_fault *vmf) >> { >> -#ifdef CONFIG_TRANSPARENT_HUGEPAGE >> struct vm_area_struct *vma = vmf->vma; >> +#ifdef CONFIG_TRANSPARENT_HUGEPAGE >> unsigned long orders; >> struct folio *folio; >> unsigned long addr; >> @@ -4206,15 +4206,21 @@ static struct folio *alloc_anon_folio(struct vm_fault *vmf) >> addr = ALIGN_DOWN(vmf->address, PAGE_SIZE << order); >> folio = vma_alloc_folio(gfp, order, vma, addr, true); >> if (folio) { >> + if (mem_cgroup_charge(folio, vma->vm_mm, gfp)) { >> + folio_put(folio); >> + goto next; >> + } >> + folio_throttle_swaprate(folio, gfp); >> clear_huge_page(&folio->page, vmf->address, 1 << order); >> return folio; >> } >> +next: >> order = next_order(&orders, order); >> } >> >> fallback: >> #endif >> - return vma_alloc_zeroed_movable_folio(vmf->vma, vmf->address); >> + return folio_prealloc(vma->vm_mm, vma, vmf->address, true); >> } >> >> /* >> @@ -4281,10 +4287,6 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf) >> nr_pages = folio_nr_pages(folio); >> addr = ALIGN_DOWN(vmf->address, nr_pages * PAGE_SIZE); >> >> - if (mem_cgroup_charge(folio, vma->vm_mm, GFP_KERNEL)) >> - goto oom_free_page; >> - folio_throttle_swaprate(folio, GFP_KERNEL); >> - >> /* >> * The memory barrier inside __folio_mark_uptodate makes sure that >> * preceding stores to the page contents become visible before >> @@ -4338,8 +4340,6 @@ static vm_fault_t do_anonymous_page(struct vm_fault *vmf) >> release: >> folio_put(folio); >> goto unlock; >> -oom_free_page: >> - folio_put(folio); >> oom: >> return VM_FAULT_OOM; >> } >> -- >> 2.27.0 >> >