From nobody Mon Nov 25 02:53:16 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 ABD8F1E767D; Wed, 30 Oct 2024 13:03:57 +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=1730293440; cv=none; b=Qvv32xu3Hh+QkktUE3hdpLddCzBU/CGT+OwXA1MnQxPC1OM4aMXJCE6UM/RPXS+NVl2hxdvIn+SvvwfO4Mc0zan+Ec4TsRgBT10wl1eGNwr/82QPjCkyXF0QHnEGgZ4zpYdGUEN5EceZfCKIUoBA0sfUCBkSMKPAxgQA7jq3oNY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730293440; 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=T8Bhux9sDzKJNH1XHO3BleKrsBuIUsBGeHcaxPrI5QI71YxwzqbryCU6qie+SP7zXebg0cwY4NV+9xCKLqYuLvn4B5o6nHYkw2FkeDyKIYwvfJevr9hKa6XJbV7nywluwmIPyGnWJhyKpz5YkkJgvqcyWOyMZpFQCyaTX24z3AU= 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=eUcKZTex; 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="eUcKZTex" 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=eUcKZTexxC8M2Wcn30sWoZqyrh LRP8JP0FKQ7H6mI4jJPzJasXn5FjP4SI+86Rk4Y2y+LLjP5yX8tNfAF7HOupm4XdtVK6GYJJltnXv Bii1xx1Lszwsm1Sk9XipuwCR7WesaGLsQWxv1K8M4nDfto6PNgH91hdlxpZw/U8TmeR3WkUCaxwxP 9e05jUf7ku16Xrdqq5krv0SN2z2/ktQAPwydgXKS2DaY/XYz88gcC1yI6Ifwo7iXUkvjmfcUr9YgO HGBP1vZXhg4m0Gh89vceRFzZgwj23LY6lgdxkFEmqno/aBR4spbKMiHFHtUzsjB9SyvtY9BTvrmjz d4nXaHRQ==; 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 1t68MI-00H5h8-GV; Wed, 30 Oct 2024 14:03:35 +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 v3 1/4] mm: fix docs for the kernel parameter ``thp_anon=`` Date: Wed, 30 Oct 2024 09:58:55 -0300 Message-ID: <20241030130308.1066299-2-mcanal@igalia.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241030130308.1066299-1-mcanal@igalia.com> References: <20241030130308.1066299-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 02:53:16 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 3C6CA1E882A; Wed, 30 Oct 2024 13:03:58 +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=1730293441; cv=none; b=NApk5mn9rf2yfeV17YLFeblEHvXYgVj+nEkCH+SWz5tWwwXHW4m/JdtcxW9LkdqJZHl5yPpSNtyJcaCAJtvL/D4Q843eryoFulRfgwSEs6St14fsjk8v9fgNV8DJQnKKQ6q/zKJSc8ecwa4nMAfWDY1d1u9yw+B1gozfUEfyd30= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730293441; c=relaxed/simple; bh=WHDW6fX+oMXYCLEt0nAgO3gmMKuaWGuMvaqGUhibnzc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=aRM8r6adN/8N+bJZbXzLME9VtAUOwWJbUhrmtaTA0ztXi6FxnppzQWYbWy5Qz31zfUa8r1x3sw7N3pmOCcEZ1xcCG/aR9ayDh9c88YMXemgr4dMMANdFJqTdhqAfnN/mGfOkQJK/2MHpwbKQnBV0Kz3P4p15rpOLUo1mlKXBbjY= 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=dZPbD1RG; 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="dZPbD1RG" 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=KhLqGBl5TV6NmgS4fHCrJuFzbGHnT6xMy1eOztxHavg=; b=dZPbD1RG6pReVV1dOCJ0ApCQE/ KzEL6y45ASLkh2mZ6IeR+Eujl9rct/rDPOZ83WttX0a2JnM/jPkILvD+T0CS1O5Olxar+9Q2txQgR Hc7ff5FWE9Pv3eP/oubI1C816wuK+LM1F3z7HDPPRJo/k+ewyEHQW40C1eA5cWEtpV3PsS/v5kRiD bO3sCfaKSyrrA29QeAjdQavHF/2hKaiS/ivzOOj6tjZo2dBqaxqJwpuPLPi5p45fhnWqIMePahczs k9qHbvmOgJ0DYa8a/yr0tvRmLhev8Hz6lhBu/O1TWP2/vvH5kVvfJOf7dp5iTMUL0AntC/DQuow/r qeguRXuQ==; 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 1t68MN-00H5h8-Qy; Wed, 30 Oct 2024 14:03:40 +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 v3 2/4] mm: shmem: control THP support through the kernel command line Date: Wed, 30 Oct 2024 09:58:56 -0300 Message-ID: <20241030130308.1066299-3-mcanal@igalia.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241030130308.1066299-1-mcanal@igalia.com> References: <20241030130308.1066299-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 Acked-by: David Hildenbrand Reviewed-by: Baolin Wang --- .../admin-guide/kernel-parameters.txt | 7 ++ Documentation/admin-guide/mm/transhuge.rst | 6 ++ mm/shmem.c | 72 +++++++++++++------ 3 files changed, 62 insertions(+), 23 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 275251abd596..dfcc88ec6e34 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -582,24 +582,39 @@ static bool shmem_huge_global_enabled(struct inode *i= node, pgoff_t index, } } =20 -#if defined(CONFIG_SYSFS) static int shmem_parse_huge(const char *str) { + int huge; + + if (!str) + return -EINVAL; + if (!strcmp(str, "never")) - return SHMEM_HUGE_NEVER; - if (!strcmp(str, "always")) - return SHMEM_HUGE_ALWAYS; - if (!strcmp(str, "within_size")) - return SHMEM_HUGE_WITHIN_SIZE; - if (!strcmp(str, "advise")) - return SHMEM_HUGE_ADVISE; - if (!strcmp(str, "deny")) - return SHMEM_HUGE_DENY; - if (!strcmp(str, "force")) - return SHMEM_HUGE_FORCE; - return -EINVAL; + huge =3D SHMEM_HUGE_NEVER; + else if (!strcmp(str, "always")) + huge =3D SHMEM_HUGE_ALWAYS; + else if (!strcmp(str, "within_size")) + huge =3D SHMEM_HUGE_WITHIN_SIZE; + else if (!strcmp(str, "advise")) + huge =3D SHMEM_HUGE_ADVISE; + else if (!strcmp(str, "deny")) + huge =3D SHMEM_HUGE_DENY; + else if (!strcmp(str, "force")) + huge =3D SHMEM_HUGE_FORCE; + else + return -EINVAL; + + 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 huge; } -#endif =20 #if defined(CONFIG_SYSFS) || defined(CONFIG_TMPFS) static const char *shmem_format_huge(int huge) @@ -5066,15 +5081,7 @@ static ssize_t shmem_enabled_store(struct kobject *k= obj, =20 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; - - /* 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 huge; =20 shmem_huge =3D huge; if (shmem_huge > SHMEM_HUGE_DENY) @@ -5171,6 +5178,25 @@ 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; + + huge =3D shmem_parse_huge(str); + if (huge =3D=3D -EINVAL) { + pr_warn("transparent_hugepage_shmem=3D cannot parse, ignored\n"); + return huge; + } + + shmem_huge =3D huge; + return 1; +} +__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 02:53:16 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 0ED751E3DD8; Wed, 30 Oct 2024 13:03:57 +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=1730293440; cv=none; b=g03UYsf2LctQK8e3ZvDXQRbTdD7AYlyMxujvKn44Gr+JPSXrMb+h1evK3kfcD70T0EiBhbNBVlETb6jBZa3mVDJ42pJFpo69tRGkJKYHKzAC0pMOFRQviim/ok9/Hbb2EtVwcwoJbcgwedxMbI+Hr9kDVbo8QLWNlo7z2NySryw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730293440; c=relaxed/simple; bh=3BdWMCxoyfe+3Y1LDRhREBxO/8sHpeJgxr5Vut4kmsc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=G4tZvY58G+bxk4KgXyxntjOnv9WW5ZfRzC5N80LjA58vqfUOeZCEw4YqYV1qS1WZ35tchuqz8Ktp+6YtLFd1s30MeptScXGpyHZ7PtYPFHyg4qNu2ZVv0dFujxx3B2oYjoUINs/y3CwRp6k2Ac1xLpr6oZdHxdZgBG/VOOyZheM= 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=BW9uTa1p; 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="BW9uTa1p" 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=Jtek6fKyThwdm08oQdbY7JhOEaPOoD9jVitbx9tAXxg=; b=BW9uTa1pzhUhZZqbRwkIsyDC59 1Vq8aXhWnRUGP5lrQnw5L7p1Vw3ReEVgwhFAZ2xp22wBF5oaGWLGz9poI+qn3mFlP7o+cnWktvUk/ KCWRXISj+4FJGASbNU583nK9FN7wkCJN4R3rcUWMISmkh3J6wOL/o6HpCfVR38FwDJcZxfQxUdAPu rFgp38z4cxZT/UVT/5HwNnArqbcTd4I1KBGdQgrBF5lz1aS7f3FXuO7r3arhcs1Gl25VQoedCL0D0 BgDs9vVSFC6i7FA4L9xMwky7mIlwctV9KyDdf1h6d7DrDg5pQYE9LjUDfMcCCliiDh2unDt11jW1T jIYYyPYA==; 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 1t68MT-00H5h8-4T; Wed, 30 Oct 2024 14:03:45 +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 v3 3/4] mm: shmem: override mTHP shmem default with a kernel parameter Date: Wed, 30 Oct 2024 09:58:57 -0300 Message-ID: <20241030130308.1066299-4-mcanal@igalia.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241030130308.1066299-1-mcanal@igalia.com> References: <20241030130308.1066299-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-8M:= 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 | 109 +++++++++++++++++- 3 files changed, 135 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 dfcc88ec6e34..c2299fa0b345 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 @@ -5180,6 +5182,26 @@ struct kobj_attribute thpsize_shmem_enabled_attr =3D =20 #if defined(CONFIG_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_FILE_DEFAULT) + goto err; + + return order; +err: + pr_err("invalid size %s in thp_shmem boot parameter\n", size_str); + return -EINVAL; +} + static int __init setup_transparent_hugepage_shmem(char *str) { int huge; @@ -5195,6 +5217,91 @@ 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) +{ + char *token, *range, *policy, *subtoken; + unsigned long always, inherit, madvise, within_size; + char *start_size, *end_size; + int start, end, nr; + char *p; + + if (!str || strlen(str) + 1 > PAGE_SIZE) + goto err; + strscpy(str_dup, str); + + always =3D huge_shmem_orders_always; + inherit =3D huge_shmem_orders_inherit; + madvise =3D huge_shmem_orders_madvise; + within_size =3D huge_shmem_orders_within_size; + p =3D str_dup; + while ((token =3D strsep(&p, ";")) !=3D NULL) { + range =3D strsep(&token, ":"); + policy =3D token; + + if (!policy) + goto err; + + while ((subtoken =3D strsep(&range, ",")) !=3D NULL) { + if (strchr(subtoken, '-')) { + start_size =3D strsep(&subtoken, "-"); + end_size =3D subtoken; + + start =3D get_order_from_str(start_size); + end =3D get_order_from_str(end_size); + } else { + start =3D end =3D get_order_from_str(subtoken); + } + + if (start < 0 || end < 0 || start > end) + goto err; + + nr =3D end - start + 1; + if (!strcmp(policy, "always")) { + bitmap_set(&always, start, nr); + bitmap_clear(&inherit, start, nr); + bitmap_clear(&madvise, start, nr); + bitmap_clear(&within_size, start, nr); + } else if (!strcmp(policy, "advise")) { + bitmap_set(&madvise, start, nr); + bitmap_clear(&inherit, start, nr); + bitmap_clear(&always, start, nr); + bitmap_clear(&within_size, start, nr); + } else if (!strcmp(policy, "inherit")) { + bitmap_set(&inherit, start, nr); + bitmap_clear(&madvise, start, nr); + bitmap_clear(&always, start, nr); + bitmap_clear(&within_size, start, nr); + } else if (!strcmp(policy, "within_size")) { + bitmap_set(&within_size, start, nr); + bitmap_clear(&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); + bitmap_clear(&within_size, start, nr); + } else { + pr_err("invalid policy %s in thp_shmem boot parameter\n", policy); + goto err; + } + } + } + + huge_shmem_orders_always =3D always; + huge_shmem_orders_madvise =3D madvise; + huge_shmem_orders_inherit =3D inherit; + huge_shmem_orders_within_size =3D within_size; + 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 From nobody Mon Nov 25 02:53:16 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 E245B1EBA16; Wed, 30 Oct 2024 13:04:02 +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=1730293444; cv=none; b=M5p3lTuqkRA8U0v9BKTUgmB/mhqJrkyvJtkQc1uXEFWGHa3Zq8eb6xM2CAkUfuEhWe3lv7gbGEyPkKKO1sC/RZ7tzz96sLGWhfrNc46faJ2Kzv9iOfjBVZp86mRq1iZZsIiT1AtCCM3A7ynghmTnNFWU35SaC/++GirdvUmPR+8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1730293444; c=relaxed/simple; bh=jtzBEiiVYmdNANE1VaGgB4AR5jbo2kyA1L+ao7NBWbk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=GHqQRV65IiRoQniBm0o95klP/IYXHLFIsYuk/Va+TDwsxTDvEFprme22cGTICqn4f2bR3rwRtdj9l+Hra91dWubYr4EaOhBMsGTQrzJg3vC0ULySz+RTIDmPCdMSRKEVLL0y/fcC94gP+Q0VBoPubfdcZiZwjnADGtQPgMIPd74= 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=AjeU/KsG; 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="AjeU/KsG" 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=/lH6a4L8gtRKOjPZ3v2254wh73VRSysBe4phJD9CDEs=; b=AjeU/KsGJLU50JSiT7VnonXpVh xz162mpXuW5PYT8HVrkynl+V4L5bYQ6ExijZcA+EMXvKfT6YIV+MAjIQJ4gsHzbRp79rnDgZdjgsb upmm9Teb+VtHj3VcJHi4NtlEBRE+XnecY0G26vW2jhQrT4TW46nw65Ed5GVT1hZ9HKRRLCCsDEQ+r sG1XmQcd6BDGZj/lJXYm8hAt9NXjZDSs0ej2GO41/QQC6TAR+iUcAVhs1Tvrrpqfn0X1iC/EtyHWU /RRQGdXOMT6j6rTERDtWG7RAq5YMYsinIfXvvLRGEiuqPZf8FybhAvLw+F78ANejVw4Pj1uJ4n0LO l2zQmP5A==; 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 1t68MY-00H5h8-CM; Wed, 30 Oct 2024 14:03:50 +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 v3 4/4] mm: huge_memory: Use strscpy() instead of strcpy() Date: Wed, 30 Oct 2024 09:58:58 -0300 Message-ID: <20241030130308.1066299-5-mcanal@igalia.com> X-Mailer: git-send-email 2.46.2 In-Reply-To: <20241030130308.1066299-1-mcanal@igalia.com> References: <20241030130308.1066299-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 Replace strcpy() with strscpy() in mm/huge_memory.c strcpy() has been deprecated because it is generally unsafe, so help to eliminate it from the kernel source. Link: https://github.com/KSPP/linux/issues/88 Signed-off-by: Ma=C3=ADra Canal Reviewed-by: Lance Yang --- mm/huge_memory.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index f92068864469..8f41a694433c 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -989,7 +989,7 @@ static int __init setup_thp_anon(char *str) =20 if (!str || strlen(str) + 1 > PAGE_SIZE) goto err; - strcpy(str_dup, str); + strscpy(str_dup, str); =20 always =3D huge_anon_orders_always; madvise =3D huge_anon_orders_madvise; @@ -4175,7 +4175,7 @@ static ssize_t split_huge_pages_write(struct file *fi= le, const char __user *buf, =20 tok =3D strsep(&buf, ","); if (tok) { - strcpy(file_path, tok); + strscpy(file_path, tok); } else { ret =3D -EINVAL; goto out; --=20 2.46.2