From nobody Fri Dec 19 19:07:37 2025 Received: from out30-99.freemail.mail.aliyun.com (out30-99.freemail.mail.aliyun.com [115.124.30.99]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 670EA1C32 for ; Thu, 5 Jun 2025 08:01:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.99 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749110482; cv=none; b=ZeNp6NXvbAaQTJ+jZpSJxEo1Aj4Y0Jk/EUJfGwZ2fjKmxquMuA1i1u2x9NtXP08f9SsqepGjd8b+y59rfKyQEM2evgqaphUbotnEIpyTCnOmbo2HTVVUVdcA5fjEZAeXMbqxfTijBMRrKAreM2/63vS95UQjhxTfSs3pzWlEpqQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749110482; c=relaxed/simple; bh=r6E3rglfRLSqSLXHR+V3IXnb3kPvV51qptkCoiFWZdo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=OP/fQ1wM7AQwsXkh8n7VUkoVgolkFb80gwJCRggLHUEspNIxqyP9oUdLGfpQBTi3fhVWntvGTPzG8z0GkLsrv8qTn9vpoOQPbrmyutpjYaJq1CuhsEul7/8AWWJTrY9mVxk9ZB93rdH7NAXvYNj4vRLr2BupayFygPkzgz+m3xs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=f5A91PJC; arc=none smtp.client-ip=115.124.30.99 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="f5A91PJC" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1749110474; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type; bh=jCGzSGCdqXE9W9t2gnUeBiPltG2v3zMxn7QoZ0xpXD4=; b=f5A91PJCmZzJLnNaTWPX6XjknLVdaE4FSLwdzMYaQ+xIU+PlTzNDo0cNrRZoN22/+Rr3w8yHhr2vA8hOtpLslrufCE9Xik5tUtbS4fmhHJx1sUc55PhKAPwOltuLPZcQ9XO/4DNcLIGstbbBEYJhfwUK0+RK/Tf0Z0+dsdhF9vo= Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0Wd7WSL6_1749110473 cluster:ay36) by smtp.aliyun-inc.com; Thu, 05 Jun 2025 16:01:14 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com, david@redhat.com Cc: lorenzo.stoakes@oracle.com, Liam.Howlett@oracle.com, npache@redhat.com, ryan.roberts@arm.com, dev.jain@arm.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 1/2] mm: huge_memory: disallow hugepages if the system-wide THP sysfs settings are disabled Date: Thu, 5 Jun 2025 16:00:58 +0800 Message-ID: <8eefb0809c598fadaa4a022634fba5689a4f3257.1749109709.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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_CO= LLAPSE will still attempt to collapse into a Anon THP. This violates the rule we h= ave agreed upon: never means never. Another rule for madvise, referring to David's suggestion: =E2=80=9Callowin= g for collapsing in a VM without VM_HUGEPAGE in the "madvise" mode would be fine". To address this issue, should check whether the Anon THP configuration is d= isabled in thp_vma_allowable_orders(), even when the TVA_ENFORCE_SYSFS flag is set. In summary, the current strategy is: 1. If always & orders =3D=3D 0, and madvise & orders =3D=3D 0, and hugepage= _global_enabled() =3D=3D false (global THP settings are not enabled), it means mTHP of that orders are pro= hibited from being used, then madvise_collapse() is forbidden for that orders. 2. If always & orders =3D=3D 0, and madvise & orders =3D=3D 0, and hugepage= _global_enabled() =3D=3D true (global THP settings are enabled), and inherit & orders =3D=3D 0, it means = mTHP of that orders are still prohibited from being used, thus madvise_collapse() is not= allowed for that orders. Reviewed-by: Zi Yan Signed-off-by: Baolin Wang Reviewed-by: Dev Jain Tested-by: Nico Pache --- 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 =3D READ_ONCE(huge_anon_orders_always); + if (vma_is_anonymous(vma)) { + unsigned long always =3D READ_ONCE(huge_anon_orders_always); + unsigned long madvise =3D READ_ONCE(huge_anon_orders_madvise); + unsigned long inherit =3D READ_ONCE(huge_anon_orders_inherit); + unsigned long mask =3D 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= ))) + return 0; + + if (!(tva_flags & TVA_ENFORCE_SYSFS)) + goto skip; =20 + mask =3D always; if (vm_flags & VM_HUGEPAGE) - mask |=3D READ_ONCE(huge_anon_orders_madvise); + mask |=3D madvise; if (hugepage_global_always() || ((vm_flags & VM_HUGEPAGE) && hugepage_global_enabled())) - mask |=3D READ_ONCE(huge_anon_orders_inherit); + mask |=3D inherit; =20 orders &=3D mask; if (!orders) return 0; } =20 +skip: return __thp_vma_allowable_orders(vma, vm_flags, tva_flags, orders); } =20 --=20 2.43.5 From nobody Fri Dec 19 19:07:37 2025 Received: from out30-113.freemail.mail.aliyun.com (out30-113.freemail.mail.aliyun.com [115.124.30.113]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 26C8A2036FE for ; Thu, 5 Jun 2025 08:01:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=115.124.30.113 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749110487; cv=none; b=DhICwKPjSB5oWEOpr6tGYGymobeJOvdFBBEpKxePnyuNpM/Q6Q0sPaQiUG11AykKSo0iyCjK/l1lJii/08H7Ph2vzkBLCqE7kB2FRv9XA/7Cd0dC/GysJkVY7c4sljKkm3s313s0az5afjtB+eNJhZ7+a0avgjuH+lghzLrWwgA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749110487; c=relaxed/simple; bh=Tcz70U8JZfh0iopbLwoN+FiTKUvlh0lEEAacCCr2wUo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=SjF7FiWN/rsygOMOgROvdCtC98t4EqmxgzmpH8fx3OMLZ2Mwz5AOrSJVxhRhpLPv2H9FNs3nIG6GbVKb7v9DiJ+kx7vON0E3tisHXljqXsQMQAMkeDYGTH7o0UNQGOaDtjBz5K02toDqAR9AeFfCmXsD6FBsjq/l46keA1SWaUw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com; spf=pass smtp.mailfrom=linux.alibaba.com; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b=goA/OwPn; arc=none smtp.client-ip=115.124.30.113 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.alibaba.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.alibaba.com header.i=@linux.alibaba.com header.b="goA/OwPn" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.alibaba.com; s=default; t=1749110477; h=From:To:Subject:Date:Message-ID:MIME-Version:Content-Type; bh=ClldW9K+An+N0FGacslbT4R62a2U/aH83hunRBaO6nc=; b=goA/OwPnrFNsVe2LXt3baGqmyy5LigqVaWw+m1/aHYPSAR4sug8REHOU114Lp6nqSHB7LTFUvBMV5oNeJPHOQBQOoRO3M4sJ9gxP3NGUujUAWLeuJDE/UZMNPTCdcDs2RvedruQf2l4u1HrrzD4xMmQCJMmVw9GA7cGKv9SkJRY= Received: from localhost(mailfrom:baolin.wang@linux.alibaba.com fp:SMTPD_---0Wd7PMBy_1749110474 cluster:ay36) by smtp.aliyun-inc.com; Thu, 05 Jun 2025 16:01:15 +0800 From: Baolin Wang To: akpm@linux-foundation.org, hughd@google.com, david@redhat.com Cc: lorenzo.stoakes@oracle.com, Liam.Howlett@oracle.com, npache@redhat.com, ryan.roberts@arm.com, dev.jain@arm.com, ziy@nvidia.com, baolin.wang@linux.alibaba.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v2 2/2] mm: shmem: disallow hugepages if the system-wide shmem THP sysfs settings are disabled Date: Thu, 5 Jun 2025 16:00:59 +0800 Message-ID: <39d7617a6142c6091f233357171c5793e0992d36.1749109709.git.baolin.wang@linux.alibaba.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable The MADV_COLLAPSE will ignore the system-wide shmem THP sysfs settings, whi= ch means that even though we have disabled the shmem THP configuration, MADV_C= OLLAPSE will still attempt to collapse into a shmem THP. This violates the rule we = have agreed upon: never means never. Another rule for madvise, referring to David's suggestion: =E2=80=9Callowin= g for collapsing in a VM without VM_HUGEPAGE in the "madvise" mode would be fine". Then the current strategy is: For shmem, if none of always, madvise, within_size, and inherit have enabled PMD-sized THP, then MADV_COLLAPSE will be prohibited from collapsing PMD-si= zed THP. For tmpfs, if the mount option is set with the 'huge=3Dnever' parameter, th= en MADV_COLLAPSE will be prohibited from collapsing PMD-sized THP. Acked-by: Zi Yan Signed-off-by: Baolin Wang --- mm/huge_memory.c | 2 +- mm/shmem.c | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index d3e66136e41a..a8cfa37cae72 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -166,7 +166,7 @@ unsigned long __thp_vma_allowable_orders(struct vm_area= _struct *vma, * own flags. */ if (!in_pf && shmem_file(vma->vm_file)) - return shmem_allowable_huge_orders(file_inode(vma->vm_file), + return orders & shmem_allowable_huge_orders(file_inode(vma->vm_file), vma, vma->vm_pgoff, 0, !enforce_sysfs); =20 diff --git a/mm/shmem.c b/mm/shmem.c index 4b42419ce6b2..9af45d4e27e6 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -625,7 +625,7 @@ static unsigned int shmem_huge_global_enabled(struct in= ode *inode, pgoff_t index return 0; if (shmem_huge =3D=3D SHMEM_HUGE_DENY) return 0; - if (shmem_huge_force || shmem_huge =3D=3D SHMEM_HUGE_FORCE) + if (shmem_huge =3D=3D SHMEM_HUGE_FORCE) return maybe_pmd_order; =20 /* @@ -660,7 +660,7 @@ static unsigned int shmem_huge_global_enabled(struct in= ode *inode, pgoff_t index =20 fallthrough; case SHMEM_HUGE_ADVISE: - if (vm_flags & VM_HUGEPAGE) + if (shmem_huge_force || (vm_flags & VM_HUGEPAGE)) return maybe_pmd_order; fallthrough; default: @@ -1790,7 +1790,7 @@ unsigned long shmem_allowable_huge_orders(struct inod= e *inode, /* Allow mTHP that will be fully within i_size. */ mask |=3D shmem_get_orders_within_size(inode, within_size_orders, index, = 0); =20 - if (vm_flags & VM_HUGEPAGE) + if (shmem_huge_force || (vm_flags & VM_HUGEPAGE)) mask |=3D READ_ONCE(huge_shmem_orders_madvise); =20 if (global_orders > 0) --=20 2.43.5