From nobody Mon Nov 25 07:48:38 2024 Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) (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 874B3567D; Tue, 29 Oct 2024 00:24:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.60.130.6 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730161454; cv=none; b=sDERpNyMQRiVUFPZmY50iyAgO11NjMCLYDEnk1TSr3x69wrx2vWhAKrZ75rmey1d2VlhltKItxmko2/14VEuC4qCrQR8IqyUwMJnCUyI7wGYt0gV8s4+d/Bp8JQtDr8FE578dAylJg+Ffw2zGDHOYUq1N/AIykbiX08LujEmk6Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730161454; c=relaxed/simple; bh=PXExopGqHf/XUbPawY0gjrjO+odnFm47+qge4z72Bx0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=OgvYq6gmQLIDAJiVtkzmwBuESDsWXVgh6bQmdI21kUka8/175xw9ytGupYm3pmYsTVb4m8q6sOav71HJP46t9k14oM+7sUswDXAiWRx0L0VEh1dZhdwqSLreBOxzVpJZC+wvm6d+iWH7WWJyCOSVpVFa5A2D1hS1SiDzGC4ok7U= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=igalia.com; spf=pass smtp.mailfrom=igalia.com; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.b=XhqzgsmJ; arc=none smtp.client-ip=178.60.130.6 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=igalia.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=igalia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.b="XhqzgsmJ" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=x/+J4VNwyIjYndlAztlVz2EFWQN/WXmhUknqKDEMai0=; b=XhqzgsmJFPEicL7uXc8og75hT6 GoLHVGkfeLhnFeZnqsIp/LmFd5p+DMTqpcTnTZfsMNWPvpMO8cOMAdP9dEL+4qLDcz1l22Axah76m 5i8pvPIen5duuIFzXBoxOYd7BriPN3mw1yLxwqosyH5YEDOg2v6xdKMhLPbcYuoXQTjLTcQWzujZB XazKzT53tLeY4W0J9nnM8LxKURXn5gPxzexKe8HoK0Hdjjkmggv3IrEB7I5A1EbZhEYVL48Tqn11g qvO5YgR8dEtRmTQQpliXY0rpILzWz9KAMN1MJpWzmlWTTPVsq3Aoy6WRl1ZLPYDQa5A48redJ6zTc HrqroFaw==; Received: from [187.36.213.55] (helo=morissey..) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1t5a1U-00GIrd-TT; Tue, 29 Oct 2024 01:23:49 +0100 From: =?UTF-8?q?Ma=C3=ADra=20Canal?= To: Jonathan Corbet , Andrew Morton , Hugh Dickins , Barry Song , David Hildenbrand , Ryan Roberts , Baolin Wang , Lance Yang Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-dev@igalia.com, =?UTF-8?q?Ma=C3=ADra=20Canal?= Subject: [PATCH v2 1/4] mm: fix docs for the kernel parameter ``thp_anon=`` Date: Mon, 28 Oct 2024 21:13:05 -0300 Message-ID: <20241029002324.1062723-2-mcanal@igalia.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241029002324.1062723-1-mcanal@igalia.com> References: <20241029002324.1062723-1-mcanal@igalia.com> 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 If we add ``thp_anon=3D32,64K:always`` to the kernel command line, we will see the following error: [ 0.000000] huge_memory: thp_anon=3D32,64K:always: error parsing string,= ignoring setting This happens because the correct format isn't ``thp_anon=3D,[KM= G]:```, as [KMG] must follow each number to especify its unit. So, the correct format is ``thp_anon=3D[KMG],[KMG]:```. Therefore, adjust the documentation to reflect the correct format of the parameter ``thp_anon=3D``. Fixes: dd4d30d1cdbe ("mm: override mTHP "enabled" defaults at kernel cmdlin= e") Signed-off-by: Ma=C3=ADra Canal Acked-by: Barry Song Acked-by: David Hildenbrand --- Documentation/admin-guide/kernel-parameters.txt | 2 +- Documentation/admin-guide/mm/transhuge.rst | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentatio= n/admin-guide/kernel-parameters.txt index 1518343bbe22..1666576acc0e 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -6688,7 +6688,7 @@ 0: no polling (default) =20 thp_anon=3D [KNL] - Format: ,[KMG]:;-[KMG]: + Format: [KMG],[KMG]:;[KMG]-[KMG]: state is one of "always", "madvise", "never" or "inherit". Control the default behavior of the system with respect to anonymous transparent hugepages. diff --git a/Documentation/admin-guide/mm/transhuge.rst b/Documentation/adm= in-guide/mm/transhuge.rst index 203ba7aaf5fc..745055c3dc09 100644 --- a/Documentation/admin-guide/mm/transhuge.rst +++ b/Documentation/admin-guide/mm/transhuge.rst @@ -303,7 +303,7 @@ control by passing the parameter ``transparent_hugepage= =3Dalways`` or kernel command line. =20 Alternatively, each supported anonymous THP size can be controlled by -passing ``thp_anon=3D,[KMG]:;-[KMG]:= ``, +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``. --=20 2.46.2 From nobody Mon Nov 25 07:48:38 2024 Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) (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 A8FCD5C96; Tue, 29 Oct 2024 00:24:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.60.130.6 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730161456; cv=none; b=BWGyJMr/kdbC4Z4s4xN5/Bhh8NsZuRC6l00BNW+0SHRx8cRu/sex1tA0B/Xb5rIl0Ln39fd+hM5NzphQ+KGWxwb8U8v2StyHFOeM2o425ZNFVFbVZZguRD7PFAqcBeXA5KA+EpEvJHhPzrauaRUimoWTtkL9PGbqIi9/bQD20YU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730161456; c=relaxed/simple; bh=tgup4Naod+yHf6BvrZZdrcCIgk5Ku9Q8LXpTZKnnyKU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=IcOywnHUZHHnjS16P4002QBpy+fijafFgvj1KyR+gjc6rhas7e31BykdsuTT6E6DIo1jr6+ctGZiMK/mbFPC7d2thVRgQJXAYYpkldxrpJ7gy6rhKXO5NmQgLGzS55VQotrSRceazcTazl3WzvsUAKVLK4iSLudGdRXE/dMblB0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=igalia.com; spf=pass smtp.mailfrom=igalia.com; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.b=bTdXTTpk; arc=none smtp.client-ip=178.60.130.6 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=igalia.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=igalia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.b="bTdXTTpk" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=f8gjxek9e09f6K3wtJdNM5s9pyafMNTnPC2fusopxIU=; b=bTdXTTpkXCmQP+P7vIFB9/gVW5 q6pgvxDrtE0mWPVomEbGVstuXNZsRkNV9ZEFtTqYfYuGDSRsXA9Pgx0FLw7D770Qi0mSaxnkqeXM+ RLbnLEuYpUUxQe5JQ6Bf99l/dCpjqnCgEl+AS1FOpqj2/+ihN6K1jjf+q8hVMB25f1ni71wDtz4/1 cmiqN7ToZQCDuSjpsQ3xSnSeBUdV+faRG6Jy09C1DdxOLAgUOlC5HLpNxDZB+VUncT0p+MFUgtEmB uN6wWULCJjf+7D03vrK3hGIXYdTC9lg/9yt0Pr278kRwmWREl6D+JAKJBjVa23jE3Ec79yFI/sheN XJAAHk7Q==; Received: from [187.36.213.55] (helo=morissey..) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1t5a1a-00GIrd-1C; Tue, 29 Oct 2024 01:23:54 +0100 From: =?UTF-8?q?Ma=C3=ADra=20Canal?= To: Jonathan Corbet , Andrew Morton , Hugh Dickins , Barry Song , David Hildenbrand , Ryan Roberts , Baolin Wang , Lance Yang Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-dev@igalia.com, =?UTF-8?q?Ma=C3=ADra=20Canal?= Subject: [PATCH v2 2/4] mm: shmem: control THP support through the kernel command line Date: Mon, 28 Oct 2024 21:13:06 -0300 Message-ID: <20241029002324.1062723-3-mcanal@igalia.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241029002324.1062723-1-mcanal@igalia.com> References: <20241029002324.1062723-1-mcanal@igalia.com> 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 Add a new kernel command line to control the hugepage allocation policy for the internal shmem mount, ``transparent_hugepage_shmem``. The parameter is similar to ``transparent_hugepage`` and has the following format: transparent_hugepage_shmem=3D where ```` is one of the seven valid policies available for shmem. By configuring the default hugepage allocation policy for the internal shmem mount, applications that use shmem, such as the DRM GEM objects, can take advantage of mTHP before it's been configured through sysfs. Signed-off-by: Ma=C3=ADra Canal --- .../admin-guide/kernel-parameters.txt | 7 +++ Documentation/admin-guide/mm/transhuge.rst | 6 +++ mm/shmem.c | 53 +++++++++++++++---- 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentatio= n/admin-guide/kernel-parameters.txt index 1666576acc0e..acabb04d0dd4 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -6926,6 +6926,13 @@ See Documentation/admin-guide/mm/transhuge.rst for more details. =20 + transparent_hugepage_shmem=3D [KNL] + Format: [always|within_size|advise|never|deny|force] + Can be used to control the hugepage allocation policy for + the internal shmem mount. + See Documentation/admin-guide/mm/transhuge.rst + for more details. + trusted.source=3D [KEYS] Format: This parameter identifies the trust source as a backend diff --git a/Documentation/admin-guide/mm/transhuge.rst b/Documentation/adm= in-guide/mm/transhuge.rst index 745055c3dc09..9b5b02c4d1ab 100644 --- a/Documentation/admin-guide/mm/transhuge.rst +++ b/Documentation/admin-guide/mm/transhuge.rst @@ -326,6 +326,12 @@ PMD_ORDER THP policy will be overridden. If the policy= for PMD_ORDER is not defined within a valid ``thp_anon``, its policy will default to ``never``. =20 +Similarly to ``transparent_hugepage``, you can control the hugepage +allocation policy for the internal shmem mount by using the kernel paramet= er +``transparent_hugepage_shmem=3D``, where ```` is one of the +seven valid policies for shmem (``always``, ``within_size``, ``advise``, +``never``, ``deny``, and ``force``). + Hugepages in tmpfs/shmem =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 diff --git a/mm/shmem.c b/mm/shmem.c index 44282a296c33..26c1eb1b4b1d 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -582,7 +582,6 @@ static bool shmem_huge_global_enabled(struct inode *ino= de, pgoff_t index, } } =20 -#if defined(CONFIG_SYSFS) static int shmem_parse_huge(const char *str) { if (!strcmp(str, "never")) @@ -599,7 +598,6 @@ static int shmem_parse_huge(const char *str) return SHMEM_HUGE_FORCE; return -EINVAL; } -#endif =20 #if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS) static const char *shmem_format_huge(int huge) @@ -624,6 +622,20 @@ static const char *shmem_format_huge(int huge) } #endif =20 +static int shmem_valid_huge(int huge) +{ + if (!has_transparent_hugepage() && + huge !=3D SHMEM_HUGE_NEVER && huge !=3D SHMEM_HUGE_DENY) + return -EINVAL; + + /* Do not override huge allocation policy with non-PMD sized mTHP */ + if (huge =3D=3D SHMEM_HUGE_FORCE && + huge_shmem_orders_inherit !=3D BIT(HPAGE_PMD_ORDER)) + return -EINVAL; + + return 0; +} + static unsigned long shmem_unused_huge_shrink(struct shmem_sb_info *sbinfo, struct shrink_control *sc, unsigned long nr_to_free) { @@ -5070,14 +5082,10 @@ static ssize_t shmem_enabled_store(struct kobject *= kobj, huge =3D shmem_parse_huge(tmp); if (huge =3D=3D -EINVAL) return -EINVAL; - if (!has_transparent_hugepage() && - huge !=3D SHMEM_HUGE_NEVER && huge !=3D SHMEM_HUGE_DENY) - return -EINVAL; =20 - /* Do not override huge allocation policy with non-PMD sized mTHP */ - if (huge =3D=3D SHMEM_HUGE_FORCE && - huge_shmem_orders_inherit !=3D BIT(HPAGE_PMD_ORDER)) - return -EINVAL; + err =3D shmem_valid_huge(huge); + if (err) + return err; =20 shmem_huge =3D huge; if (shmem_huge > SHMEM_HUGE_DENY) @@ -5174,6 +5182,33 @@ struct kobj_attribute thpsize_shmem_enabled_attr =3D __ATTR(shmem_enabled, 0644, thpsize_shmem_enabled_show, thpsize_shmem_ena= bled_store); #endif /* CONFIG_TRANSPARENT_HUGEPAGE && CONFIG_SYSFS */ =20 +#if defined(CONFIG_TRANSPARENT_HUGEPAGE) + +static int __init setup_transparent_hugepage_shmem(char *str) +{ + int huge, ret =3D 0; + + if (!str) + goto out; + + huge =3D shmem_parse_huge(str); + if (huge =3D=3D -EINVAL) + goto out; + + ret =3D shmem_valid_huge(huge); + if (ret) + goto out; + + shmem_huge =3D huge; + return 1; +out: + pr_warn("transparent_hugepage_shmem=3D cannot parse, ignored\n"); + return ret; +} +__setup("transparent_hugepage_shmem=3D", setup_transparent_hugepage_shmem); + +#endif /* CONFIG_TRANSPARENT_HUGEPAGE */ + #else /* !CONFIG_SHMEM */ =20 /* --=20 2.46.2 From nobody Mon Nov 25 07:48:38 2024 Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) (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 03060F9D6; Tue, 29 Oct 2024 00:24:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.60.130.6 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730161458; cv=none; b=ZnaMgezZFPdpbPJzpio2TsmWfk6U0ucY78pcH4VA2CnIkg82aRop0LjNaPBK97iorkWWysFt/vOj6RpILMpfieTuUOXzFaZiPcsJkmedJlyV6I5Ru4YbQ1ip4/Attym/rx9yGdzN3hZu1x23qXWSuqzrRB+a32Z29yp5bmblItg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730161458; c=relaxed/simple; bh=iYa9GLbU8mTvg0QZEgBm/tnKiX/52pJDSuqu2BXQW10=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=jZsguXWX4z0p5AJGkZTnYF7TaDaZdkKb7eCvYrGIKICJ9B4gv3od81MicvXPGZaYIr1mNobqKzwqjC8z5//vXJjVwEH3+PjCFl43/Tnjwtd0tnL7vjb37MThyXZAribHoQotfCSUJhtE4EH83Av6vd9Oluk39xok+zuJg7lCr7E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=igalia.com; spf=pass smtp.mailfrom=igalia.com; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.b=Sdshq8td; arc=none smtp.client-ip=178.60.130.6 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=igalia.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=igalia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.b="Sdshq8td" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=w+ns6OSoZMKNPdhNYQfwqi4ljcYi8WU+6sPV1i33TG4=; b=Sdshq8tdeTIIZrwGdLocfz8Z1U /2xLHI03EpXyzG964+QjuqUdm7LDnIh4Fdiefj+fzZ0ATq0IkgA2c9zEpJ3pJlnMgD7k9smvtGXkj hNzBnBZuh21B65Mq9xtip4Ughf7wt4IwyKJ+siF42LigZgZe1s35ifUaMxw+H8QVKh+5hFekWf6pV 9LdZlYmo9CCSvlkAMg8AqIn6xg6TPWB1FxT54SgAP8jMTLU+4xmdR9e6Gs9IAuDAyfdgJtWaa1QbK uP+1i0vmjaqC8pP3eE0NtDIBhZvDEBPSmyiTJav4TEdkcRhY6TL1w/oFvVRNU0g9q/mNQLqWviQ3N PxRkKfNw==; Received: from [187.36.213.55] (helo=morissey..) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1t5a1f-00GIrd-8G; Tue, 29 Oct 2024 01:23:59 +0100 From: =?UTF-8?q?Ma=C3=ADra=20Canal?= To: Jonathan Corbet , Andrew Morton , Hugh Dickins , Barry Song , David Hildenbrand , Ryan Roberts , Baolin Wang , Lance Yang Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-dev@igalia.com, =?UTF-8?q?Ma=C3=ADra=20Canal?= Subject: [PATCH v2 3/4] mm: generalize the implementation of ``thp_anon=`` Date: Mon, 28 Oct 2024 21:13:07 -0300 Message-ID: <20241029002324.1062723-4-mcanal@igalia.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241029002324.1062723-1-mcanal@igalia.com> References: <20241029002324.1062723-1-mcanal@igalia.com> 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 Currently, the function ``setup_thp_anon()`` is extremely tied to the policies and bitmaps that currently exist for anon THP. This means that we can't reuse the function if we implement a ``thp_shmem=3D``. This commit extracts the behavior of the function to a new generic function. The new function is exposed in common headers for future use by mm/shmem.c. Signed-off-by: Ma=C3=ADra Canal --- include/linux/huge_mm.h | 29 +++++++++++ mm/huge_memory.c | 109 ++++++++++++++++++---------------------- 2 files changed, 78 insertions(+), 60 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index b94c2e8ee918..b82e9379f2bf 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -471,6 +471,35 @@ void split_huge_pmd_locked(struct vm_area_struct *vma,= unsigned long address, bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, pmd_t *pmdp, struct folio *folio); =20 +static inline int get_order_from_str(const char *size_str, + const unsigned long thp_orders) +{ + unsigned long size; + char *endptr; + int order; + + size =3D memparse(size_str, &endptr); + + if (!is_power_of_2(size)) + goto err; + + order =3D get_order(size); + if (BIT(order) & ~thp_orders) + goto err; + + return order; +err: + pr_err("invalid size %s in boot parameter\n", size_str); + return -EINVAL; +} + +struct thp_policy_bitmap { + const char *policy; + unsigned long bitmap; +}; + +int parse_huge_orders(char *p, struct thp_policy_bitmap *policies, + const int num_policies, const unsigned long thp_orders); #else /* CONFIG_TRANSPARENT_HUGEPAGE */ =20 static inline bool folio_test_pmd_mappable(struct folio *folio) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 832ca761b4c3..c61f4481cb6a 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -958,91 +958,80 @@ static int __init setup_transparent_hugepage(char *st= r) } __setup("transparent_hugepage=3D", setup_transparent_hugepage); =20 -static inline int get_order_from_str(const char *size_str) -{ - unsigned long size; - char *endptr; - int order; - - size =3D memparse(size_str, &endptr); - - if (!is_power_of_2(size)) - goto err; - order =3D get_order(size); - if (BIT(order) & ~THP_ORDERS_ALL_ANON) - goto err; - - return order; -err: - pr_err("invalid size %s in thp_anon boot parameter\n", size_str); - return -EINVAL; -} - -static char str_dup[PAGE_SIZE] __initdata; -static int __init setup_thp_anon(char *str) +int parse_huge_orders(char *p, struct thp_policy_bitmap *policies, + const int num_policies, const unsigned long thp_orders) { char *token, *range, *policy, *subtoken; - unsigned long always, inherit, madvise; char *start_size, *end_size; - int start, end, nr; - char *p; + int start, end, nr, i; + bool policy_set; =20 - if (!str || strlen(str) + 1 > PAGE_SIZE) - goto err; - strcpy(str_dup, str); - - always =3D huge_anon_orders_always; - madvise =3D huge_anon_orders_madvise; - inherit =3D huge_anon_orders_inherit; - p =3D str_dup; while ((token =3D strsep(&p, ";")) !=3D NULL) { range =3D strsep(&token, ":"); policy =3D token; =20 if (!policy) - goto err; + return 0; =20 while ((subtoken =3D strsep(&range, ",")) !=3D NULL) { + policy_set =3D false; + if (strchr(subtoken, '-')) { start_size =3D strsep(&subtoken, "-"); end_size =3D subtoken; =20 - start =3D get_order_from_str(start_size); - end =3D get_order_from_str(end_size); + start =3D get_order_from_str(start_size, thp_orders); + end =3D get_order_from_str(end_size, thp_orders); } else { - start =3D end =3D get_order_from_str(subtoken); + start =3D end =3D get_order_from_str(subtoken, thp_orders); } =20 if (start < 0 || end < 0 || start > end) - goto err; + return 0; =20 nr =3D end - start + 1; - if (!strcmp(policy, "always")) { - bitmap_set(&always, start, nr); - bitmap_clear(&inherit, start, nr); - bitmap_clear(&madvise, start, nr); - } else if (!strcmp(policy, "madvise")) { - bitmap_set(&madvise, start, nr); - bitmap_clear(&inherit, start, nr); - bitmap_clear(&always, start, nr); - } else if (!strcmp(policy, "inherit")) { - bitmap_set(&inherit, start, nr); - bitmap_clear(&madvise, start, nr); - bitmap_clear(&always, start, nr); - } else if (!strcmp(policy, "never")) { - bitmap_clear(&inherit, start, nr); - bitmap_clear(&madvise, start, nr); - bitmap_clear(&always, start, nr); - } else { - pr_err("invalid policy %s in thp_anon boot parameter\n", policy); - goto err; + + for (i =3D 0; i < num_policies; i++) { + if (!strcmp(policy, policies[i].policy)) { + bitmap_set(&policies[i].bitmap, start, nr); + policy_set =3D true; + } else + bitmap_clear(&policies[i].bitmap, start, nr); + } + + if (!policy_set && strcmp(policy, "never")) { + pr_err("invalid policy %s in boot parameter\n", policy); + return 0; } } } =20 - huge_anon_orders_always =3D always; - huge_anon_orders_madvise =3D madvise; - huge_anon_orders_inherit =3D inherit; + return 1; +} + +static char str_dup[PAGE_SIZE] __initdata; +static int __init setup_thp_anon(char *str) +{ + struct thp_policy_bitmap policies[] =3D { + { "always", huge_anon_orders_always }, + { "madvise", huge_anon_orders_madvise }, + { "inherit", huge_anon_orders_inherit }, + }; + char *p; + + if (!str || strlen(str) + 1 > PAGE_SIZE) + goto err; + + strscpy(str_dup, str); + p =3D str_dup; + + if (!parse_huge_orders(p, policies, ARRAY_SIZE(policies), + THP_ORDERS_ALL_ANON)) + goto err; + + huge_anon_orders_always =3D policies[0].bitmap; + huge_anon_orders_madvise =3D policies[1].bitmap; + huge_anon_orders_inherit =3D policies[2].bitmap; anon_orders_configured =3D true; return 1; =20 --=20 2.46.2 From nobody Mon Nov 25 07:48:38 2024 Received: from fanzine2.igalia.com (fanzine.igalia.com [178.60.130.6]) (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 E56C917BD6; Tue, 29 Oct 2024 00:24:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=178.60.130.6 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730161459; cv=none; b=MSVbkE5LlljWEpwhcK/4gnFjP8328ODdxf5gAAjZEyUD2N2pFtuG0lNKWjg0ZfUkX/vWXK4irBcaTjlxI2Er6nV+9kNSlviiK77vUB8QDtkhPboemK3jMmCnpCkIYmccy8yEU6YKCxyDtVFXXT+I1br81qLaekwmt5vhTOWgwbA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730161459; c=relaxed/simple; bh=DDEEbY5NKTODUyQzjfOpOKJVymT68lWkQwWLp7Dd1PI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=fpTM6Aaz3ez8z6UKeeTUTsj1mwyEQGEsWCSypvNJnteMoyKAM83ISgUqNqW25XmRRx9ZYWBQY8miY/9PJaL2MXRwa6gg3yYS9ZfKOTLgcQ0mPW+bJT/jPJY01OUMYs80abqDI2mU9GFw3DC0By0dUXfzz9Zr925Pd6RvdI1VqJc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=igalia.com; spf=pass smtp.mailfrom=igalia.com; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.b=icCQQT2l; arc=none smtp.client-ip=178.60.130.6 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=igalia.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=igalia.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=igalia.com header.i=@igalia.com header.b="icCQQT2l" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=BwshJwo48RIhJnqvpdLmCKcZc0vOflKYESOeF9s3h5g=; b=icCQQT2lxYD/Bkswn+kH5dOcQl 4OyL053b9xdV1IU4hvc3z6E6kEqCdEQKIEOC3InqAVEdZmg0nkJYzr+9gIoHMpwBzRVEPPwROwdcz 1raCGT8RCC7Uh3t7hjq/6slsuJgezXZxSUq/LCx//sXBjGDMmcZ7r1nBGM9G1BoVBC2Hq6ZqUHRYk 31RYRo3CfcM+ElquSuLvP2lxg7i46wfBU+3vacpv/2Rd5XwuCzqZksb1AGiXJZJe93JRyWhLd5r1U /P0X6aHHZdihEbdOoe/XLiNuG6lQRhNlmCNnt3Pwcpm5Max35hEfROmuCh3Doo7ExtT6383OxEDOZ /B+mHGrg==; Received: from [187.36.213.55] (helo=morissey..) by fanzine2.igalia.com with esmtpsa (Cipher TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim) id 1t5a1k-00GIrd-8p; Tue, 29 Oct 2024 01:24:04 +0100 From: =?UTF-8?q?Ma=C3=ADra=20Canal?= To: Jonathan Corbet , Andrew Morton , Hugh Dickins , Barry Song , David Hildenbrand , Ryan Roberts , Baolin Wang , Lance Yang Cc: linux-mm@kvack.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, kernel-dev@igalia.com, =?UTF-8?q?Ma=C3=ADra=20Canal?= Subject: [PATCH v2 4/4] mm: shmem: override mTHP shmem default with a kernel parameter Date: Mon, 28 Oct 2024 21:13:08 -0300 Message-ID: <20241029002324.1062723-5-mcanal@igalia.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241029002324.1062723-1-mcanal@igalia.com> References: <20241029002324.1062723-1-mcanal@igalia.com> 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 Add the ``thp_shmem=3D`` kernel command line to allow specifying the default policy of each supported shmem hugepage size. The kernel parameter accepts the following format: thp_shmem=3D[KMG],[KMG]:;[KMG]-[KMG]: For example, thp_shmem=3D16K-64K:always;128K,512K:inherit;256K:advise;1M-2M:never;4M-8= M:within_size By configuring the default policy of several shmem hugepages, the user can take advantage of mTHP before it's been configured through sysfs. Signed-off-by: Ma=C3=ADra Canal --- .../admin-guide/kernel-parameters.txt | 10 +++++ Documentation/admin-guide/mm/transhuge.rst | 17 +++++++++ mm/shmem.c | 38 ++++++++++++++++++- 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentatio= n/admin-guide/kernel-parameters.txt index acabb04d0dd4..b48d744d99b0 100644 --- a/Documentation/admin-guide/kernel-parameters.txt +++ b/Documentation/admin-guide/kernel-parameters.txt @@ -6700,6 +6700,16 @@ Force threading of all interrupt handlers except those marked explicitly IRQF_NO_THREAD. =20 + thp_shmem=3D [KNL] + Format: [KMG],[KMG]:;[KMG]-[KMG]: + Control the default policy of each hugepage size for the + internal shmem mount. is one of policies available + for the shmem mount ("always", "inherit", "never", "within_size", + and "advise"). + It can be used multiple times for multiple shmem THP sizes. + See Documentation/admin-guide/mm/transhuge.rst for more + details. + topology=3D [S390,EARLY] Format: {off | on} Specify if the kernel should make use of the cpu diff --git a/Documentation/admin-guide/mm/transhuge.rst b/Documentation/adm= in-guide/mm/transhuge.rst index 9b5b02c4d1ab..47e7fc30e22d 100644 --- a/Documentation/admin-guide/mm/transhuge.rst +++ b/Documentation/admin-guide/mm/transhuge.rst @@ -332,6 +332,23 @@ allocation policy for the internal shmem mount by usin= g the kernel parameter seven valid policies for shmem (``always``, ``within_size``, ``advise``, ``never``, ``deny``, and ``force``). =20 +In the same manner as ``thp_anon`` controls each supported anonymous THP +size, ``thp_shmem`` controls each supported shmem THP size. ``thp_shmem`` +has the same format as ``thp_anon``, but also supports the policy +``within_size``. + +``thp_shmem=3D`` may be specified multiple times to configure all THP sizes +as required. If ``thp_shmem=3D`` is specified at least once, any shmem THP +sizes not explicitly configured on the command line are implicitly set to +``never``. + +``transparent_hugepage_shmem`` setting only affects the global toggle. If +``thp_shmem`` is not specified, PMD_ORDER hugepage will default to +``inherit``. However, if a valid ``thp_shmem`` setting is provided by the +user, the PMD_ORDER hugepage policy will be overridden. If the policy for +PMD_ORDER is not defined within a valid ``thp_shmem``, its policy will +default to ``never``. + Hugepages in tmpfs/shmem =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 diff --git a/mm/shmem.c b/mm/shmem.c index 26c1eb1b4b1d..37951bb863dd 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -136,6 +136,7 @@ static unsigned long huge_shmem_orders_always __read_mo= stly; static unsigned long huge_shmem_orders_madvise __read_mostly; static unsigned long huge_shmem_orders_inherit __read_mostly; static unsigned long huge_shmem_orders_within_size __read_mostly; +static bool shmem_orders_configured __initdata; #endif =20 #ifdef CONFIG_TMPFS @@ -5027,7 +5028,8 @@ void __init shmem_init(void) * Default to setting PMD-sized THP to inherit the global setting and * disable all other multi-size THPs. */ - huge_shmem_orders_inherit =3D BIT(HPAGE_PMD_ORDER); + if (!shmem_orders_configured) + huge_shmem_orders_inherit =3D BIT(HPAGE_PMD_ORDER); #endif return; =20 @@ -5184,6 +5186,7 @@ struct kobj_attribute thpsize_shmem_enabled_attr =3D =20 #if defined(CONFIG_TRANSPARENT_HUGEPAGE) =20 + static int __init setup_transparent_hugepage_shmem(char *str) { int huge, ret =3D 0; @@ -5207,6 +5210,39 @@ static int __init setup_transparent_hugepage_shmem(c= har *str) } __setup("transparent_hugepage_shmem=3D", setup_transparent_hugepage_shmem); =20 +static char str_dup[PAGE_SIZE] __initdata; +static int __init setup_thp_shmem(char *str) +{ + struct thp_policy_bitmap policies[] =3D { + { "always", huge_shmem_orders_always }, + { "inherit", huge_shmem_orders_inherit }, + { "advise", huge_shmem_orders_madvise }, + { "within_size", huge_shmem_orders_within_size } + }; + char *p; + + if (!str || strlen(str) + 1 > PAGE_SIZE) + goto err; + + strscpy(str_dup, str); + p =3D str_dup; + + if (!parse_huge_orders(p, policies, ARRAY_SIZE(policies), + THP_ORDERS_ALL_FILE_DEFAULT)) + goto err; + + huge_shmem_orders_always =3D policies[0].bitmap; + huge_shmem_orders_inherit =3D policies[1].bitmap; + huge_shmem_orders_madvise =3D policies[2].bitmap; + huge_shmem_orders_within_size =3D policies[3].bitmap; + shmem_orders_configured =3D true; + return 1; +err: + pr_warn("thp_shmem=3D%s: error parsing string, ignoring setting\n", str); + return 0; +} +__setup("thp_shmem=3D", setup_thp_shmem); + #endif /* CONFIG_TRANSPARENT_HUGEPAGE */ =20 #else /* !CONFIG_SHMEM */ --=20 2.46.2