From nobody Sun Feb 8 11:15:42 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 BF839292901 for ; Thu, 15 May 2025 03:39:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747280390; cv=none; b=QWx8StNnGpvJHdtc57CmqR9eVCdVEIqi3u7od67/KXZxO3Hq8jVy1CPQxljj/H0WS24b+W+On5CKA/RLZx6lyka40mSZ/zI5mY3RrK3fY/d6HxBTjijqFQBBrj/coIgK71nFTSOqqOrWEM3H3bGeZ1FlCfP3g12pfZBWpYiX75s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747280390; c=relaxed/simple; bh=IQfoJgb8b3FMmomPOPPPp27biP5OlIdFMeSBihDo49U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=lQJUjw1vkfeYGPF2wkLAkELN9CfXTePWxdI5DtN1spzO/1tQh0L3+FZ8rhJrJsjlFIduS0G1RCCFJM1wOnKUJXBR6py8pA/gsQPnYv+543akzAD/h7JUm3cIze9AvgKFSXkr9a5V3QXfu5Csq2AUxMONlMnQcmGtUmX5ULBsZP4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=Vd+WaEXQ; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="Vd+WaEXQ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1747280387; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=BQb3Gd4y9CLA3GzXKNCccBYdyfEwUNAPaSPoLscp5Es=; b=Vd+WaEXQoY7YSZ4siduPOBWPFQ3bHFYFt8awlQ04uCacmqL/pXHA8FqJNrHJzUD7aNxKb5 Fv7TTVQSq/N4KjXS5yKpRSaI4fgRKlqE1+EmiuKf24Np3rrqDnhwadqUI+Tf0QQZFSpsU9 8mry3JfCTI5dZy8dheCAGZlfmdiAvYc= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-12-n3UWDWEqPxSdmjHv7ZPF_A-1; Wed, 14 May 2025 23:39:43 -0400 X-MC-Unique: n3UWDWEqPxSdmjHv7ZPF_A-1 X-Mimecast-MFC-AGG-ID: n3UWDWEqPxSdmjHv7ZPF_A_1747280379 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6AD921800256; Thu, 15 May 2025 03:39:38 +0000 (UTC) Received: from h1.redhat.com (unknown [10.22.88.116]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 99DCF1953B85; Thu, 15 May 2025 03:39:22 +0000 (UTC) From: Nico Pache To: linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: rientjes@google.com, hannes@cmpxchg.org, lorenzo.stoakes@oracle.com, rdunlap@infradead.org, mhocko@suse.com, Liam.Howlett@oracle.com, zokeefe@google.com, surenb@google.com, jglisse@google.com, cl@gentwo.org, jack@suse.cz, dave.hansen@linux.intel.com, will@kernel.org, tiwai@suse.de, catalin.marinas@arm.com, anshuman.khandual@arm.com, dev.jain@arm.com, raquini@redhat.com, aarcange@redhat.com, kirill.shutemov@linux.intel.com, yang@os.amperecomputing.com, thomas.hellstrom@linux.intel.com, vishal.moola@gmail.com, sunnanyong@huawei.com, usamaarif642@gmail.com, wangkefeng.wang@huawei.com, ziy@nvidia.com, shuah@kernel.org, peterx@redhat.com, willy@infradead.org, ryan.roberts@arm.com, baolin.wang@linux.alibaba.com, baohua@kernel.org, david@redhat.com, mathieu.desnoyers@efficios.com, mhiramat@kernel.org, rostedt@goodmis.org, corbet@lwn.net, akpm@linux-foundation.org, npache@redhat.com Subject: [PATCH v6 1/4] mm: defer THP insertion to khugepaged Date: Wed, 14 May 2025 21:38:54 -0600 Message-ID: <20250515033857.132535-2-npache@redhat.com> In-Reply-To: <20250515033857.132535-1-npache@redhat.com> References: <20250515033857.132535-1-npache@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 Content-Type: text/plain; charset="utf-8" setting /transparent_hugepages/enabled=3Dalways allows applications to benefit from THPs without having to madvise. However, the page fault handler takes very few considerations to decide weather or not to actually use a THP. This can lead to a lot of wasted memory. khugepaged only operates on memory that was either allocated with enabled=3Dalways or MADV_HUGEPAGE. Introduce the ability to set enabled=3Ddefer, which will prevent THPs from being allocated by the page fault handler unless madvise is set, leaving it up to khugepaged to decide which allocations will collapse to a THP. This should allow applications to benefits from THPs, while curbing some of the memory waste. Acked-by: Zi Yan Co-developed-by: Rafael Aquini Signed-off-by: Rafael Aquini Signed-off-by: Nico Pache --- include/linux/huge_mm.h | 15 +++++++++++++-- mm/huge_memory.c | 31 +++++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index e3d15c737008..02038e3db829 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -48,6 +48,7 @@ enum transparent_hugepage_flag { TRANSPARENT_HUGEPAGE_UNSUPPORTED, TRANSPARENT_HUGEPAGE_FLAG, TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, + TRANSPARENT_HUGEPAGE_DEFER_PF_FLAG, TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, @@ -186,6 +187,7 @@ static inline bool hugepage_global_enabled(void) { return transparent_hugepage_flags & ((1<; Thu, 15 May 2025 03:40:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747280407; cv=none; b=WWIGvmf5cpRTlt0VkQMvYl9qSS6kgqrkZhBhqPgF3frPFwrVC9D0xFZzmOwYhIx8nAS3K5uDd/0LHgLwM063PY0DvSqGeOxH8QhaR/yzxAjgaG7jbDU+B1h15O/j/1H/fgKf19xgTSOTH6r+hZW7ODnjr0CR8voBUtEjgVL0NWA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747280407; c=relaxed/simple; bh=wm+P6eF/Tb2F6jKCjfMYwZ5NuAGurGxLdAK3FwkVMfQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=T4HhKkjNHYw79FxOBSwWKI5scXsWzxc7esz8tdTbP5cYJWhoGLWtEJQLk/nToKV5c1l2mDwSuWUtxfEjyu5yQ6aXi87yNsx01gH4TvLugBrUtfnQ/xQxkHAosL2Ww/RfELT+RoB9iyMQ9UFGaDCXXLvJckQ4Ag5qZ29xxC39T+A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=PrCZjWDU; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="PrCZjWDU" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1747280405; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=illy9a0IaRLRjpyollQHr/drUNF4chj8Y2TZlHp92rI=; b=PrCZjWDUuh9KkgNQK5XVQsX9DvoMecgL+lHOzEoxW7IOVdfxlWNeAJ1Qa/+fRiCa5RXHGO 9kdQ0nR2KCfiuxOaWAR+ej6SOkaPU982u3MLb98/Rh6PyS1Enmi3Oh0p//W4LAZJMAjmcA XY6T7h6QLljarv+slMjvKqkE/gPesAs= Received: from mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-131-vUokHE6TMzmJBnlO53tfIg-1; Wed, 14 May 2025 23:40:00 -0400 X-MC-Unique: vUokHE6TMzmJBnlO53tfIg-1 X-Mimecast-MFC-AGG-ID: vUokHE6TMzmJBnlO53tfIg_1747280396 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 39AE21956095; Thu, 15 May 2025 03:39:55 +0000 (UTC) Received: from h1.redhat.com (unknown [10.22.88.116]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0E95E1955F24; Thu, 15 May 2025 03:39:38 +0000 (UTC) From: Nico Pache To: linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: rientjes@google.com, hannes@cmpxchg.org, lorenzo.stoakes@oracle.com, rdunlap@infradead.org, mhocko@suse.com, Liam.Howlett@oracle.com, zokeefe@google.com, surenb@google.com, jglisse@google.com, cl@gentwo.org, jack@suse.cz, dave.hansen@linux.intel.com, will@kernel.org, tiwai@suse.de, catalin.marinas@arm.com, anshuman.khandual@arm.com, dev.jain@arm.com, raquini@redhat.com, aarcange@redhat.com, kirill.shutemov@linux.intel.com, yang@os.amperecomputing.com, thomas.hellstrom@linux.intel.com, vishal.moola@gmail.com, sunnanyong@huawei.com, usamaarif642@gmail.com, wangkefeng.wang@huawei.com, ziy@nvidia.com, shuah@kernel.org, peterx@redhat.com, willy@infradead.org, ryan.roberts@arm.com, baolin.wang@linux.alibaba.com, baohua@kernel.org, david@redhat.com, mathieu.desnoyers@efficios.com, mhiramat@kernel.org, rostedt@goodmis.org, corbet@lwn.net, akpm@linux-foundation.org, npache@redhat.com, Bagas Sanjaya Subject: [PATCH v6 2/4] mm: document (m)THP defer usage Date: Wed, 14 May 2025 21:38:55 -0600 Message-ID: <20250515033857.132535-3-npache@redhat.com> In-Reply-To: <20250515033857.132535-1-npache@redhat.com> References: <20250515033857.132535-1-npache@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 Content-Type: text/plain; charset="utf-8" The new defer option for (m)THPs allows for a more conservative approach to (m)THPs. Document its usage in the transhuge admin-guide. Reviewed-by: Bagas Sanjaya Signed-off-by: Nico Pache --- Documentation/admin-guide/mm/transhuge.rst | 31 ++++++++++++++++------ 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/Documentation/admin-guide/mm/transhuge.rst b/Documentation/adm= in-guide/mm/transhuge.rst index 5c63fe51b3ad..7e87ef317add 100644 --- a/Documentation/admin-guide/mm/transhuge.rst +++ b/Documentation/admin-guide/mm/transhuge.rst @@ -88,8 +88,9 @@ In certain cases when hugepages are enabled system wide, = application may end up allocating more memory resources. An application may mmap a large region but only touch 1 byte of it, in that case a 2M page might be allocated instead of a 4k page for no good. This is why it's -possible to disable hugepages system-wide and to only have them inside -MADV_HUGEPAGE madvise regions. +possible to disable hugepages system-wide, only have them inside +MADV_HUGEPAGE madvise regions, or defer them away from the page fault +handler to khugepaged. =20 Embedded systems should enable hugepages only inside madvise regions to eliminate any risk of wasting any precious byte of memory and to @@ -99,6 +100,15 @@ Applications that gets a lot of benefit from hugepages = and that don't risk to lose memory by using hugepages, should use madvise(MADV_HUGEPAGE) on their critical mmapped regions. =20 +Applications that would like to benefit from THPs but would still like a +more memory conservative approach can choose 'defer'. This avoids +inserting THPs at the page fault handler unless they are MADV_HUGEPAGE. +Khugepaged will then scan all mappings, even those not explicitly marked +with MADV_HUGEPAGE, for potential collapses into (m)THPs. Admins using +this the 'defer' setting should consider tweaking max_ptes_none. The +current default of 511 may aggressively collapse your PTEs into PMDs. +Lower this value to conserve more memory (i.e., max_ptes_none=3D64). + .. _thp_sysfs: =20 sysfs @@ -109,11 +119,14 @@ Global THP controls =20 Transparent Hugepage Support for anonymous memory can be entirely disabled (mostly for debugging purposes) or only enabled inside MADV_HUGEPAGE -regions (to avoid the risk of consuming more memory resources) or enabled -system wide. This can be achieved per-supported-THP-size with one of:: +regions (to avoid the risk of consuming more memory resources), deferred to +khugepaged, or enabled system wide. + +This can be achieved per-supported-THP-size with one of:: =20 echo always >/sys/kernel/mm/transparent_hugepage/hugepages-kB/enabl= ed echo madvise >/sys/kernel/mm/transparent_hugepage/hugepages-kB/enab= led + echo defer >/sys/kernel/mm/transparent_hugepage/hugepages-kB/enabled echo never >/sys/kernel/mm/transparent_hugepage/hugepages-kB/enabled =20 where is the hugepage size being addressed, the available sizes @@ -136,6 +149,7 @@ The top-level setting (for use with "inherit") can be s= et by issuing one of the following commands:: =20 echo always >/sys/kernel/mm/transparent_hugepage/enabled + echo defer >/sys/kernel/mm/transparent_hugepage/enabled echo madvise >/sys/kernel/mm/transparent_hugepage/enabled echo never >/sys/kernel/mm/transparent_hugepage/enabled =20 @@ -286,7 +300,8 @@ of small pages into one large page:: A higher value leads to use additional memory for programs. A lower value leads to gain less thp performance. Value of max_ptes_none can waste cpu time very little, you can -ignore it. +ignore it. Consider lowering this value when using +``transparent_hugepage=3Ddefer`` =20 ``max_ptes_swap`` specifies how many pages can be brought in from swap when collapsing a group of pages into a transparent huge page:: @@ -311,14 +326,14 @@ Boot parameters =20 You can change the sysfs boot time default for the top-level "enabled" control by passing the parameter ``transparent_hugepage=3Dalways`` or -``transparent_hugepage=3Dmadvise`` or ``transparent_hugepage=3Dnever`` to = the -kernel command line. +``transparent_hugepage=3Dmadvise`` or ``transparent_hugepage=3Ddefer`` or +``transparent_hugepage=3Dnever`` to the kernel command line. =20 Alternatively, each supported anonymous THP size can be controlled by passing ``thp_anon=3D[KMG],[KMG]:;[KMG]-[KM= G]:``, where ```` is the THP size (must be a power of 2 of PAGE_SIZE and supported anonymous THP) and ```` is one of ``always``, ``madvise`= `, -``never`` or ``inherit``. +``defer``, ``never`` or ``inherit``. =20 For example, the following will set 16K, 32K, 64K THP to ``always``, set 128K, 512K to ``inherit``, set 256K to ``madvise`` and 1M, 2M --=20 2.49.0 From nobody Sun Feb 8 11:15:42 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 D82C026ACC for ; Thu, 15 May 2025 03:40:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747280422; cv=none; b=kVU29RefiRC6WuvBXf3HWmTqE/2bZOYs0+mTziw3fVrVGAfuo5FJl2o/6LLdg5+hkK2c3rM+tJe8SslVdO0fvP/kdXx6ZrJMEAFf2BY+0OrX1F3x7lCgKXrQynZ0iMT3bk1e8s+vzzXzg8VvFTKAij1cxuvd0KrO/s37LyGxdoI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747280422; c=relaxed/simple; bh=gK9SXcfkwTFJ8H80waokKBfM9j2Y//eZhsa0mahqpss=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Ke1xGs7l5SuAUPJe5WX0OmSiJUW6epJZcoRFcCVLAzwmpsC67MM4Z1QM7rJ8ABvf2BDMIC7KKFAg/6GQLTZhpBbPLp3N/kkmXzjb2YbB1p4cLr6T/MITOxcLCiGkKJjQzFU1DPyj7TpMhvjY9LoRSvCgr6krHlhbsk7ed4IN/+o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=HpDeoGJB; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="HpDeoGJB" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1747280419; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2FcfP1VjBWLoQo1T5hleMjgd59JUGzOjdgnK8A5BAbo=; b=HpDeoGJB3u+PlczVCIApXFri8eJPWU70SmTnD5ZtEEY86qtzD9A9T2ZdNOb4g/3Gaku+fx gRf0XoMPh60S/6C3loxg2ffaw/0pnHVwzeBlBVfAV0AZUtptBRKH1jXjjCEeT6Dne9J3Pj HZoQGXEathM1jdtNtEIp0HF6HteRfTs= Received: from mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-691-ePQ_r8ucNfyXTZagynrRTw-1; Wed, 14 May 2025 23:40:16 -0400 X-MC-Unique: ePQ_r8ucNfyXTZagynrRTw-1 X-Mimecast-MFC-AGG-ID: ePQ_r8ucNfyXTZagynrRTw_1747280411 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-06.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id CE9A31800361; Thu, 15 May 2025 03:40:10 +0000 (UTC) Received: from h1.redhat.com (unknown [10.22.88.116]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id D79D81953B80; Thu, 15 May 2025 03:39:55 +0000 (UTC) From: Nico Pache To: linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: rientjes@google.com, hannes@cmpxchg.org, lorenzo.stoakes@oracle.com, rdunlap@infradead.org, mhocko@suse.com, Liam.Howlett@oracle.com, zokeefe@google.com, surenb@google.com, jglisse@google.com, cl@gentwo.org, jack@suse.cz, dave.hansen@linux.intel.com, will@kernel.org, tiwai@suse.de, catalin.marinas@arm.com, anshuman.khandual@arm.com, dev.jain@arm.com, raquini@redhat.com, aarcange@redhat.com, kirill.shutemov@linux.intel.com, yang@os.amperecomputing.com, thomas.hellstrom@linux.intel.com, vishal.moola@gmail.com, sunnanyong@huawei.com, usamaarif642@gmail.com, wangkefeng.wang@huawei.com, ziy@nvidia.com, shuah@kernel.org, peterx@redhat.com, willy@infradead.org, ryan.roberts@arm.com, baolin.wang@linux.alibaba.com, baohua@kernel.org, david@redhat.com, mathieu.desnoyers@efficios.com, mhiramat@kernel.org, rostedt@goodmis.org, corbet@lwn.net, akpm@linux-foundation.org, npache@redhat.com Subject: [PATCH v6 3/4] khugepaged: add defer option to mTHP options Date: Wed, 14 May 2025 21:38:56 -0600 Message-ID: <20250515033857.132535-4-npache@redhat.com> In-Reply-To: <20250515033857.132535-1-npache@redhat.com> References: <20250515033857.132535-1-npache@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 Content-Type: text/plain; charset="utf-8" Now that we have defer to globally disable THPs at fault time, lets add a defer setting to the mTHP options. This will allow khugepaged to operate at that order, while avoiding it at PF time. Signed-off-by: Nico Pache --- include/linux/huge_mm.h | 5 +++++ mm/huge_memory.c | 38 +++++++++++++++++++++++++++++++++----- mm/khugepaged.c | 8 ++++---- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 02038e3db829..71a1edb5062e 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -96,6 +96,7 @@ extern struct kobj_attribute thpsize_shmem_enabled_attr; #define TVA_SMAPS (1 << 0) /* Will be used for procfs */ #define TVA_IN_PF (1 << 1) /* Page fault handler */ #define TVA_ENFORCE_SYSFS (1 << 2) /* Obey sysfs configuration */ +#define TVA_IN_KHUGEPAGE ((1 << 2) | (1 << 3)) /* Khugepaged defer support= */ =20 #define thp_vma_allowable_order(vma, vm_flags, tva_flags, order) \ (!!thp_vma_allowable_orders(vma, vm_flags, tva_flags, BIT(order))) @@ -182,6 +183,7 @@ extern unsigned long transparent_hugepage_flags; extern unsigned long huge_anon_orders_always; extern unsigned long huge_anon_orders_madvise; extern unsigned long huge_anon_orders_inherit; +extern unsigned long huge_anon_orders_defer; =20 static inline bool hugepage_global_enabled(void) { @@ -306,6 +308,9 @@ unsigned long thp_vma_allowable_orders(struct vm_area_s= truct *vma, /* 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 ((tva_flags & TVA_IN_KHUGEPAGE) =3D=3D TVA_IN_KHUGEPAGE) + mask |=3D READ_ONCE(huge_anon_orders_defer); if (vm_flags & VM_HUGEPAGE) mask |=3D READ_ONCE(huge_anon_orders_madvise); if (hugepage_global_always() || hugepage_global_defer() || diff --git a/mm/huge_memory.c b/mm/huge_memory.c index ce0ee74753af..addf4c16c91d 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -81,6 +81,7 @@ unsigned long huge_zero_pfn __read_mostly =3D ~0UL; unsigned long huge_anon_orders_always __read_mostly; unsigned long huge_anon_orders_madvise __read_mostly; unsigned long huge_anon_orders_inherit __read_mostly; +unsigned long huge_anon_orders_defer __read_mostly; static bool anon_orders_configured __initdata; =20 static inline bool file_thp_enabled(struct vm_area_struct *vma) @@ -505,13 +506,15 @@ static ssize_t anon_enabled_show(struct kobject *kobj, const char *output; =20 if (test_bit(order, &huge_anon_orders_always)) - output =3D "[always] inherit madvise never"; + output =3D "[always] inherit madvise defer never"; else if (test_bit(order, &huge_anon_orders_inherit)) - output =3D "always [inherit] madvise never"; + output =3D "always [inherit] madvise defer never"; else if (test_bit(order, &huge_anon_orders_madvise)) - output =3D "always inherit [madvise] never"; + output =3D "always inherit [madvise] defer never"; + else if (test_bit(order, &huge_anon_orders_defer)) + output =3D "always inherit madvise [defer] never"; else - output =3D "always inherit madvise [never]"; + output =3D "always inherit madvise defer [never]"; =20 return sysfs_emit(buf, "%s\n", output); } @@ -527,25 +530,36 @@ static ssize_t anon_enabled_store(struct kobject *kob= j, spin_lock(&huge_anon_orders_lock); clear_bit(order, &huge_anon_orders_inherit); clear_bit(order, &huge_anon_orders_madvise); + clear_bit(order, &huge_anon_orders_defer); set_bit(order, &huge_anon_orders_always); spin_unlock(&huge_anon_orders_lock); } else if (sysfs_streq(buf, "inherit")) { spin_lock(&huge_anon_orders_lock); clear_bit(order, &huge_anon_orders_always); clear_bit(order, &huge_anon_orders_madvise); + clear_bit(order, &huge_anon_orders_defer); set_bit(order, &huge_anon_orders_inherit); spin_unlock(&huge_anon_orders_lock); } else if (sysfs_streq(buf, "madvise")) { spin_lock(&huge_anon_orders_lock); clear_bit(order, &huge_anon_orders_always); clear_bit(order, &huge_anon_orders_inherit); + clear_bit(order, &huge_anon_orders_defer); set_bit(order, &huge_anon_orders_madvise); spin_unlock(&huge_anon_orders_lock); + } else if (sysfs_streq(buf, "defer")) { + spin_lock(&huge_anon_orders_lock); + clear_bit(order, &huge_anon_orders_always); + clear_bit(order, &huge_anon_orders_inherit); + clear_bit(order, &huge_anon_orders_madvise); + set_bit(order, &huge_anon_orders_defer); + spin_unlock(&huge_anon_orders_lock); } else if (sysfs_streq(buf, "never")) { spin_lock(&huge_anon_orders_lock); clear_bit(order, &huge_anon_orders_always); clear_bit(order, &huge_anon_orders_inherit); clear_bit(order, &huge_anon_orders_madvise); + clear_bit(order, &huge_anon_orders_defer); spin_unlock(&huge_anon_orders_lock); } else ret =3D -EINVAL; @@ -1002,7 +1016,7 @@ static char str_dup[PAGE_SIZE] __initdata; static int __init setup_thp_anon(char *str) { char *token, *range, *policy, *subtoken; - unsigned long always, inherit, madvise; + unsigned long always, inherit, madvise, defer; char *start_size, *end_size; int start, end, nr; char *p; @@ -1014,6 +1028,8 @@ static int __init setup_thp_anon(char *str) always =3D huge_anon_orders_always; madvise =3D huge_anon_orders_madvise; inherit =3D huge_anon_orders_inherit; + defer =3D huge_anon_orders_defer; + p =3D str_dup; while ((token =3D strsep(&p, ";")) !=3D NULL) { range =3D strsep(&token, ":"); @@ -1053,18 +1069,28 @@ static int __init setup_thp_anon(char *str) bitmap_set(&always, start, nr); bitmap_clear(&inherit, start, nr); bitmap_clear(&madvise, start, nr); + bitmap_clear(&defer, start, nr); } else if (!strcmp(policy, "madvise")) { bitmap_set(&madvise, start, nr); bitmap_clear(&inherit, start, nr); bitmap_clear(&always, start, nr); + bitmap_clear(&defer, start, nr); } else if (!strcmp(policy, "inherit")) { bitmap_set(&inherit, start, nr); bitmap_clear(&madvise, start, nr); bitmap_clear(&always, start, nr); + bitmap_clear(&defer, start, nr); + } else if (!strcmp(policy, "defer")) { + bitmap_set(&defer, start, nr); + bitmap_clear(&madvise, start, nr); + bitmap_clear(&always, start, nr); + bitmap_clear(&inherit, start, nr); } else if (!strcmp(policy, "never")) { bitmap_clear(&inherit, start, nr); bitmap_clear(&madvise, start, nr); bitmap_clear(&always, start, nr); + bitmap_clear(&defer, start, nr); + } else { pr_err("invalid policy %s in thp_anon boot parameter\n", policy); goto err; @@ -1075,6 +1101,8 @@ static int __init setup_thp_anon(char *str) huge_anon_orders_always =3D always; huge_anon_orders_madvise =3D madvise; huge_anon_orders_inherit =3D inherit; + huge_anon_orders_defer =3D defer; + anon_orders_configured =3D true; return 1; =20 diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 0723b184c7a4..428060495c49 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -491,7 +491,7 @@ void khugepaged_enter_vma(struct vm_area_struct *vma, { if (!test_bit(MMF_VM_HUGEPAGE, &vma->vm_mm->flags) && hugepage_pmd_enabled()) { - if (thp_vma_allowable_order(vma, vm_flags, TVA_ENFORCE_SYSFS, + if (thp_vma_allowable_order(vma, vm_flags, TVA_IN_KHUGEPAGE, PMD_ORDER)) __khugepaged_enter(vma->vm_mm); } @@ -955,7 +955,7 @@ static int hugepage_vma_revalidate(struct mm_struct *mm= , unsigned long address, struct collapse_control *cc, int order) { struct vm_area_struct *vma; - unsigned long tva_flags =3D cc->is_khugepaged ? TVA_ENFORCE_SYSFS : 0; + unsigned long tva_flags =3D cc->is_khugepaged ? TVA_IN_KHUGEPAGE : 0; =20 if (unlikely(khugepaged_test_exit_or_disable(mm))) return SCAN_ANY_PROCESS; @@ -1434,7 +1434,7 @@ static int khugepaged_scan_pmd(struct mm_struct *mm, bool writable =3D false; int chunk_none_count =3D 0; int scaled_none =3D khugepaged_max_ptes_none >> (HPAGE_PMD_ORDER - KHUGEP= AGED_MIN_MTHP_ORDER); - unsigned long tva_flags =3D cc->is_khugepaged ? TVA_ENFORCE_SYSFS : 0; + unsigned long tva_flags =3D cc->is_khugepaged ? TVA_IN_KHUGEPAGE : 0; VM_BUG_ON(address & ~HPAGE_PMD_MASK); =20 result =3D find_pmd_or_thp_or_none(mm, address, &pmd); @@ -2626,7 +2626,7 @@ static unsigned int khugepaged_scan_mm_slot(unsigned = int pages, int *result, break; } if (!thp_vma_allowable_order(vma, vma->vm_flags, - TVA_ENFORCE_SYSFS, PMD_ORDER)) { + TVA_IN_KHUGEPAGE, PMD_ORDER)) { skip: progress++; continue; --=20 2.49.0 From nobody Sun Feb 8 11:15:42 2026 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 8B84B226183 for ; Thu, 15 May 2025 03:40:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747280437; cv=none; b=iWyDCN696o1AupCNppaTB1hpqciVTpgQ/pL8Tpn1OjWeABHtmz+I5yv6/oH2oVDfHudgxlFIOulfcJLmFO65WWJYOR+z/gie0MBGMIaxEmt0VwwpEE9hk8pmdg1SGjcaX8mbhQu4mIIzXWpA6MMj0HFqm4YuMcd32OJ6detIaX0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1747280437; c=relaxed/simple; bh=ojvYh32c7Hy4vJTBL+DkaUPpFW/+kSsAeaxM9iD4T7M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CP3Qfqoqzxai0Cx+1z5ZJrofXuqbe+BHzDmqxfSzSoP7bhWDhpURJ5jVRvUpP2dfT/uihPuYHuFEyEnGBCEfz7PmlZ7d1y2xaxwsaapv+Or5DreLn4UDwnEerPBlTogLW9BcoS0GzXCBJebwT1m9okEflvP6D2rMjmZGKjbB9mM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=TyoRoDAm; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="TyoRoDAm" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1747280434; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=5OpOeDWkKDKT63sqe7Ct3ueIe83ZxEVQ2GdgfA8/ZAk=; b=TyoRoDAmo+F7gnQIzhrwloYEm/sSsv8Mk2FvF/fyJmmWFX2C8/kb9/jZz+EI8twADScS15 WPhvbWhNoSydDxJsLY3rZcG70KwkwhOhy97uXyirhHEm7Ykc7xVi3ZFz3ukgBBwyMfUoAE Z5OecDrOUJBn2czZb3KS1r8wwT9Mxys= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-462-PfgWC-tvPuGK-zi7d_D3Vw-1; Wed, 14 May 2025 23:40:32 -0400 X-MC-Unique: PfgWC-tvPuGK-zi7d_D3Vw-1 X-Mimecast-MFC-AGG-ID: PfgWC-tvPuGK-zi7d_D3Vw_1747280427 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 13C401956046; Thu, 15 May 2025 03:40:27 +0000 (UTC) Received: from h1.redhat.com (unknown [10.22.88.116]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 6EC2A1955F24; Thu, 15 May 2025 03:40:11 +0000 (UTC) From: Nico Pache To: linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: rientjes@google.com, hannes@cmpxchg.org, lorenzo.stoakes@oracle.com, rdunlap@infradead.org, mhocko@suse.com, Liam.Howlett@oracle.com, zokeefe@google.com, surenb@google.com, jglisse@google.com, cl@gentwo.org, jack@suse.cz, dave.hansen@linux.intel.com, will@kernel.org, tiwai@suse.de, catalin.marinas@arm.com, anshuman.khandual@arm.com, dev.jain@arm.com, raquini@redhat.com, aarcange@redhat.com, kirill.shutemov@linux.intel.com, yang@os.amperecomputing.com, thomas.hellstrom@linux.intel.com, vishal.moola@gmail.com, sunnanyong@huawei.com, usamaarif642@gmail.com, wangkefeng.wang@huawei.com, ziy@nvidia.com, shuah@kernel.org, peterx@redhat.com, willy@infradead.org, ryan.roberts@arm.com, baolin.wang@linux.alibaba.com, baohua@kernel.org, david@redhat.com, mathieu.desnoyers@efficios.com, mhiramat@kernel.org, rostedt@goodmis.org, corbet@lwn.net, akpm@linux-foundation.org, npache@redhat.com Subject: [PATCH v6 4/4] selftests: mm: add defer to thp setting parser Date: Wed, 14 May 2025 21:38:57 -0600 Message-ID: <20250515033857.132535-5-npache@redhat.com> In-Reply-To: <20250515033857.132535-1-npache@redhat.com> References: <20250515033857.132535-1-npache@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 Content-Type: text/plain; charset="utf-8" add the defer setting to the selftests library for reading thp settings. Signed-off-by: Nico Pache --- tools/testing/selftests/mm/thp_settings.c | 1 + tools/testing/selftests/mm/thp_settings.h | 1 + 2 files changed, 2 insertions(+) diff --git a/tools/testing/selftests/mm/thp_settings.c b/tools/testing/self= tests/mm/thp_settings.c index ad872af1c81a..b2f9f62b302a 100644 --- a/tools/testing/selftests/mm/thp_settings.c +++ b/tools/testing/selftests/mm/thp_settings.c @@ -20,6 +20,7 @@ static const char * const thp_enabled_strings[] =3D { "always", "inherit", "madvise", + "defer", NULL }; =20 diff --git a/tools/testing/selftests/mm/thp_settings.h b/tools/testing/self= tests/mm/thp_settings.h index fc131d23d593..0d52e6d4f754 100644 --- a/tools/testing/selftests/mm/thp_settings.h +++ b/tools/testing/selftests/mm/thp_settings.h @@ -11,6 +11,7 @@ enum thp_enabled { THP_ALWAYS, THP_INHERIT, THP_MADVISE, + THP_DEFER, }; =20 enum thp_defrag { --=20 2.49.0