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 2FB21C5AE59 for ; Fri, 30 May 2025 01:51:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C4CE96B0088; Thu, 29 May 2025 21:51:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BFDD56B0089; Thu, 29 May 2025 21:51:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B15E56B008A; Thu, 29 May 2025 21:51:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 9216A6B0088 for ; Thu, 29 May 2025 21:51:57 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id EA6B3121C64 for ; Fri, 30 May 2025 01:51:56 +0000 (UTC) X-FDA: 83497898232.05.F9D8084 Received: from out30-112.freemail.mail.aliyun.com (out30-112.freemail.mail.aliyun.com [115.124.30.112]) by imf10.hostedemail.com (Postfix) with ESMTP id F0273C0003 for ; Fri, 30 May 2025 01:51:53 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=pO4Rp+UW; spf=pass (imf10.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.112 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1748569915; 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:dkim-signature; bh=vnZC1rQ0fxZWfqF2Uv901r4Mxjs+2QHPQnmoHX7l9ac=; b=dsy7s1+BofbQKj3ZpwYXSP5ikCg5wA0hZi5tTIfqwEizFwYQWvs0PomjBNOlxuOdNjZk7e Mj9QLP7s4YiSo0cLUCEh3T/rtVHFCHhDbs3pwbjpNlAEQ9ngNh4QjqLYljY0hj1yt8J+NE XywB0YSlhL/wfp8dGiRmMIs0HVWHlxg= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=linux.alibaba.com header.s=default header.b=pO4Rp+UW; spf=pass (imf10.hostedemail.com: domain of baolin.wang@linux.alibaba.com designates 115.124.30.112 as permitted sender) smtp.mailfrom=baolin.wang@linux.alibaba.com; dmarc=pass (policy=none) header.from=linux.alibaba.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1748569915; a=rsa-sha256; cv=none; b=w8XEX/v+TSynnOGEhouIm70PVwjsIaYUCzfA4vKGs7BMBA/sHjqETw9/p4jeHlwki9mdai CYSfwwm7kZKQRhSjOMEV7uMGwkK5cVWZ0LGlXwGRo6T1oLpDSGY9vHEeZSkXMWjNfU+klw JPgjpDkglvIwOkcGqQsTzUzg/I1nGWk= DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1748569911; h=Message-ID:Date:MIME-Version:Subject:To:From:Content-Type; bh=vnZC1rQ0fxZWfqF2Uv901r4Mxjs+2QHPQnmoHX7l9ac=; b=pO4Rp+UWTuJgmyEirE6gzy1tWfOHje4dMP4ozOijWsyg9x0wjFNROZN9hCoM6Ip6FqU8hQxbZefkulzTPCZPKImBPgjtNgwxAwLmQ7srhEf9GPEJFETJzpYnrv1dkrgRRFe5VOiJ3yUvF7As19IyLuuuFXJAKWlWckbGSAQEsLM= Received: from 30.74.144.115(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0WcJPkbk_1748569907 cluster:ay36) by smtp.aliyun-inc.com; Fri, 30 May 2025 09:51:49 +0800 Message-ID: <5acbfc5f-81b6-40e2-b87b-ac50423172f0@linux.alibaba.com> Date: Fri, 30 May 2025 09:51:46 +0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 1/2] mm: huge_memory: disallow hugepages if the system-wide THP sysfs settings are disabled To: Zi Yan Cc: akpm@linux-foundation.org, hughd@google.com, david@redhat.com, lorenzo.stoakes@oracle.com, Liam.Howlett@oracle.com, npache@redhat.com, ryan.roberts@arm.com, dev.jain@arm.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org References: <33577DDE-D88E-44F9-9B91-7AA46EACCCE8@nvidia.com> From: Baolin Wang In-Reply-To: <33577DDE-D88E-44F9-9B91-7AA46EACCCE8@nvidia.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Queue-Id: F0273C0003 X-Rspamd-Server: rspam09 X-Stat-Signature: 5a1ificwmnjkcu7ks74w4me6jf9bwgk6 X-HE-Tag: 1748569913-940531 X-HE-Meta: U2FsdGVkX18MqpNSK7GAPZ+AhnvedmVP3SRnKFPovUwy5E9rYU7fL89lxbAdOdVU0Anfy2bX5Apt/Ktd6DF9pxxYxTOIoAqCwZ5JZ6mMxdh6cWxaajZATmQJBpAk8TG7T/BVRS84bSvrRUnbKvgpN8HxhAnxiIJHVZ5R4H8UAlFs6jbqSJVZO0nEb7KGEWZBCLOQqpKDpYl+sWMnLv6uz/K0emGnU0EkL/elaDfhU+pDsyRsY4SnKGuzLKl/WY/sgob5Adwa/G5wsPbf+Jv8RJx/kbmHvKP8EnAungmpgHu/NwqgDW20LJYK3zK8F58727vv25ovMjXUlATqBCru77QMyuXqqsHGT/oxOkWumUZ/wXhUILTJezF7O7ttj3hDmbxkm/3zabURB7mN+9q9K4t7y1mhKOosBAh3AhvsEMrIoA+RlXKeRwtSaaCGTQgUWXNNv4b1+FKObljRoqOGX1vC4owjnZ5ByayOvTny9irTKx4FHRyYWCFrc7DxUJdAQGQpCEwogjzVpZvPAl8ADaGxf7QxCALJxcfXr2Z7S/c1CoRpReRylBfIaUUiS8w4bI5BK0eRoq3Te0sBuOXiEm/zkFIM6x/KfiJZZkyGXJLXMNg/UIQGyBgg54zWahTJYQ5gMNJT7PU6i3shw4pz5wunpYLG7HqD8XdTEXKO/Jd9TBgAkRsmPgJSUkM8YHLegGs/aSkvdu/CSfVGzFrtSmb2FmKNw52LhfADWfFn5b3UGqhS1S61s6LgJ9CSLISSzjH+kPdxG8sl/Qp/GnP4xC3mY8wIaRSn6su7Tnda+ZTF7DvX7LBFih839BN0Tce7Au7vdYaagmJJGT/6PW7zylBHF3ozgSv22a5mUiM03kcwvgLBO7zFLYCvjMmBU/5Bp7U1BXGGc7L4aA05arH+RePzeaSJsp1U7Y3P8yrQihQDPgDmDtSWhi0OhdamvuHFQ1kS55ey7Me97Fs4ugQ QzYbvc8A 1FsOvQq1ENsnfbJwD9ewMObXx0GnZBvEbp5I5K718ubZ7+HkMDGwc93iaMu6WiYOrgfAiiaNdfS9JqSpkqGQesXPqwnWuwS5rj6gkMy1yTx4juCVVx/klGcfI9nP0PsESu88diiqtYf2KIYHb3ky++1iWn6suvLkHbhG3puA6dZX1HJckU/NGqEtVt0BYuo9yaN+tfyTkZ8SruQrWd1BY1pqh3BEVi45gWpB7XWHgJbJOmoCeELflAxW56AtPeV+HLGKdppifmwwTacxBvzHqNpImvqd8dF9FFvD6tElLRegGTM7bawurggTkV5iWBnEh9q+uOyMde/bBe6THVgc8xbGx5a0X+m8izcFr5Ntij9o9I/tBXaMeatiyaf6Fc6hSXhfs 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 2025/5/29 23:10, Zi Yan wrote: > On 29 May 2025, at 4:23, Baolin Wang wrote: > >> The MADV_COLLAPSE will ignore the system-wide Anon THP sysfs settings, which >> means that even though we have disabled the Anon THP configuration, MADV_COLLAPSE >> will still attempt to collapse into a Anon THP. This violates the rule we have >> agreed upon: never means never. >> >> To address this issue, should check whether the Anon THP configuration is disabled >> in thp_vma_allowable_orders(), even when the TVA_ENFORCE_SYSFS flag is set. >> >> Signed-off-by: Baolin Wang >> --- >> include/linux/huge_mm.h | 23 +++++++++++++++++++---- >> 1 file changed, 19 insertions(+), 4 deletions(-) >> >> diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h >> index 2f190c90192d..199ddc9f04a1 100644 >> --- a/include/linux/huge_mm.h >> +++ b/include/linux/huge_mm.h >> @@ -287,20 +287,35 @@ unsigned long thp_vma_allowable_orders(struct vm_area_struct *vma, >> unsigned long orders) >> { >> /* Optimization to check if required orders are enabled early. */ >> - if ((tva_flags & TVA_ENFORCE_SYSFS) && vma_is_anonymous(vma)) { >> - unsigned long mask = READ_ONCE(huge_anon_orders_always); >> + if (vma_is_anonymous(vma)) { >> + unsigned long always = READ_ONCE(huge_anon_orders_always); >> + unsigned long madvise = READ_ONCE(huge_anon_orders_madvise); >> + unsigned long inherit = READ_ONCE(huge_anon_orders_inherit); >> + unsigned long mask = always | madvise; >> + >> + /* >> + * If the system-wide THP/mTHP sysfs settings are disabled, >> + * then we should never allow hugepages. >> + */ >> + if (!(mask & orders) && !(hugepage_global_enabled() && (inherit & orders))) > > Can you explain the logic here? Is it equivalent to: > 1. if THP is set to always, always_mask & orders == 0, or > 2. if THP if set to madvise, madvise_mask & order == 0, or > 3. if THP is set to inherit, inherit_mask & order == 0? > > I cannot figure out why (always | madvise) & orders does not check > THP enablement case, but inherit & orders checks hugepage_global_enabled(). Sorry for not being clear. Let me try again: Now we can control per-sized mTHP through ‘huge_anon_orders_always’, so always does not need to rely on the check of hugepage_global_enabled(). For madvise, referring to David's suggestion: “allowing for collapsing in a VM without VM_HUGEPAGE in the "madvise" mode would be fine", so we can just check 'huge_anon_orders_madvise' without relying on hugepage_global_enabled(). In the case where hugepage_global_enabled() is enabled, we need to check whether the 'inherit' has enabled the corresponding orders. In summary, the current strategy is: 1. If always & orders == 0, and madvise & orders == 0, and hugepage_global_enabled() == false (global THP settings are not enabled), it means mTHP of the orders are prohibited from being used, then madvise_collapse() is forbidden. 2. If always & orders == 0, and madvise & orders == 0, and hugepage_global_enabled() == true (global THP settings are enabled), and inherit & orders == 0, it means mTHP of the orders are still prohibited from being used, and thus madvise_collapse() is not allowed.