From patchwork Mon Jul 29 22:27:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nico Pache X-Patchwork-Id: 13745844 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 19FBFC3DA61 for ; Mon, 29 Jul 2024 22:28:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A8F686B008A; Mon, 29 Jul 2024 18:28:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A313B6B008C; Mon, 29 Jul 2024 18:28:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 836BB6B0092; Mon, 29 Jul 2024 18:28:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 647A16B008A for ; Mon, 29 Jul 2024 18:28:00 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D84BC140108 for ; Mon, 29 Jul 2024 22:27:59 +0000 (UTC) X-FDA: 82394229078.09.A088B2D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf29.hostedemail.com (Postfix) with ESMTP id 3311812001E for ; Mon, 29 Jul 2024 22:27:57 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=IjqvUhzC; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf29.hostedemail.com: domain of npache@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=npache@redhat.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722292034; a=rsa-sha256; cv=none; b=P659qq5h5eOM35aPGm4gieAn8aD4V9+AtPUbmjGRJXlb0jQJ32iCj7r/oTK0EjUgZWFhV8 2OfdxRcTVPRen1Zv+B98XmuvU3EM7VIKUV0Pn7gF3m0o+gdQB8c9M842kqa+GfZa2LVUaI xCsTHv8dVc6BDfJmUeVr8LlFZ+0JKfA= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=IjqvUhzC; dmarc=pass (policy=none) header.from=redhat.com; spf=pass (imf29.hostedemail.com: domain of npache@redhat.com designates 170.10.129.124 as permitted sender) smtp.mailfrom=npache@redhat.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722292034; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Bx7PoZA+3BaXuBlEAlsF0yYgH6+llN/9+YIdocNVsPk=; b=K0FlMiekhONn16L0O0H4kP42z/z8DycV3eZJQOWIWwOQ5N7aSVvKmqlKy53+TJ19xSUGFt F+pfwJM5xXbJmFkH1KpVhUPymHBeqemGrspEhWs6Yd1Yz9cUWQz8b6lUuDX29OaZ8US17/ GdzYvFXffP06rCAIWlBdWtc/EiyAmd8= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1722292077; 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=Bx7PoZA+3BaXuBlEAlsF0yYgH6+llN/9+YIdocNVsPk=; b=IjqvUhzCwGOOhjb3XWi7QRUJQGW3s+i2jUwLtJQqABw+mV4MYmWZnviEUOoCkd6ws3wOis AkoPJRALArGNbGqrAL4csDGA4CLwmDtyWCceqIjU0GMThS99UW5uILr6l69XxlX8pjkOgz FB+vyzGb1Y+sf3E+ehNeYlFXNyt6H3E= Received: from mx-prod-mc-05.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-347-zMAMeneGNAWQ5cntbgBMYQ-1; Mon, 29 Jul 2024 18:27:53 -0400 X-MC-Unique: zMAMeneGNAWQ5cntbgBMYQ-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (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-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id CFA261955F49; Mon, 29 Jul 2024 22:27:51 +0000 (UTC) Received: from h1.redhat.com (unknown [10.22.10.55]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 290111955F3B; Mon, 29 Jul 2024 22:27:47 +0000 (UTC) From: Nico Pache To: linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org Cc: Andrew Morton , David Hildenbrand , Matthew Wilcox , Barry Song , Ryan Roberts , Baolin Wang , Lance Yang , Peter Xu , Zi Yan , Rafael Aquini , Andrea Arcangeli , Jonathan Corbet Subject: [RFC 1/2] mm: defer THP insertion to khugepaged Date: Mon, 29 Jul 2024 16:27:26 -0600 Message-ID: <20240729222727.64319-2-npache@redhat.com> In-Reply-To: <20240729222727.64319-1-npache@redhat.com> References: <20240729222727.64319-1-npache@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 3311812001E X-Stat-Signature: h99hdf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fhwgJ0U4iX/uXRsubDEknqZVcF540iYmUESY2tDz1auz3iP32wFqnNO8T0ArtBlGvIM0NAuic/yMs3Tdek7iGvHhY4f4Vox/TV4tnmlEHkQT0noSuHqkgdEXh409BEf1bcfFe8D33Pwv2R+zylFWCTeDvqHbDWaNwH1TwiwF6LmxNerwucB+SKc1n5DQlgJl3X6iHxi7uLKJo+1rGUnwS5xDVT2etndz+pAMpJtv0oWKjlYhGkBMCqr7XrEE2ILpPLwM0q0swqoUcxPYnjdBoWLJWiBK+0/ceBYRNNVVABzJ2i3AT9z6Od1abdQ0MiyKMDhL+/l5rLO+7nnB36k1CEWts3yXBuumkYgf63xMmFxEXQsj7ER9j9blQwv6JhUU4n2H3/wV81gNLCwtrn7IpybR7XwzIvGtxNBv/N7L1sTKyjQY4sv+fQK2JceKXAjt7KS6nmBqEdOP8pNWrQLs9ZAShjxhYmcZru49V0stJG/OqQrC6uBmeVUS7SJ64mrgzBxdYTiFg6l0hXVOPTyb7BPJHIviwKTcRQBlHELTQvgxio9RpU1qTSGrHPBkTQyVmVz0jN/2ZSVtaMq2n7kH/603aECNqidYQMfNZ+FLXSgGNELI1spdFyfl 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: setting /transparent_hugepages/enabled=always allows applications to benefit from THPs without having to madvise. However, the pf 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=always or MADV_HUGEPAGE. Introduce the ability to set enabled=defer, 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. Cc: Andrew Morton Cc: David Hildenbrand Cc: Matthew Wilcox Cc: Barry Song Cc: Ryan Roberts Cc: Baolin Wang Cc: Lance Yang Cc: Peter Xu Cc: Zi Yan Cc: Rafael Aquini Cc: Andrea Arcangeli Cc: Jonathan Corbet 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 e25d9ebfdf89..7ef7d3e2f5c6 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -45,6 +45,7 @@ enum transparent_hugepage_flag { TRANSPARENT_HUGEPAGE_UNSUPPORTED, TRANSPARENT_HUGEPAGE_FLAG, TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, + TRANSPARENT_HUGEPAGE_DEFER_PF_INST_FLAG, TRANSPARENT_HUGEPAGE_DEFRAG_DIRECT_FLAG, TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_FLAG, TRANSPARENT_HUGEPAGE_DEFRAG_KSWAPD_OR_MADV_FLAG, @@ -125,6 +126,7 @@ static inline bool hugepage_global_enabled(void) { return transparent_hugepage_flags & ((1<