From nobody Mon Jun 8 23:57:49 2026 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.5]) (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 11FC9355F4E for ; Mon, 25 May 2026 10:28:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.5 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779704908; cv=none; b=YPPKkjpq5HtfY0pzVN5OBuVn37DwDOEV6IkhBbfArgKQL4n1N4Iffnxr7zcBM473vP8huIPD4byy5fAg8BcQMJjuVrhNmLhdEKEVPmdfVa7bymokzs7RkuzbYDantQAP5j/cg2sesKXxOvESyS+Jv78d+MxLo7nV9T3gTxK0bd0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779704908; c=relaxed/simple; bh=4tEneQC0YvhtgU11kwqstvKWqdW8Zj0yaQqYqfieE9Y=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=uXgpq9NGjEGUeAa3b9aWkm2VYkzfdJFxd2xAAuRBpymKlehs68IfD4OspdIJE5m0OjIm/WceVfgNTT2cuxMRRWqc8tyqY20ZUMkreJxpDB/bFHwrbPTSjhfxodLZD+rBxqEd/t0pUBlwqlYSBFDKZDmtErLl006ovwFPTxQ1dj0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=RGPb0mhr; arc=none smtp.client-ip=117.135.210.5 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="RGPb0mhr" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=zb TwoaOkXwx1Ghzc6peFnK8IaPJSEI9x2zHIcuTcCaY=; b=RGPb0mhrhpLnP0xwrn a79/V6f2tjKOIULWVDO2WIawbLM150vTiMtoSkMeuFMRrpQtftucPUFyCz9kyaS3 ICs0PdJxsdeCKhS8fRV2ib2zJQhijsSXsonEPjEAKbq3qqXViEeEfEr7tg74Dqw3 7OQN2XjntorNmQ2aIXdUzuKxQ= Received: from ubuntu24-z.. (unknown []) by gzga-smtp-mtada-g1-4 (Coremail) with SMTP id _____wD3V9L2IxRqXqahDg--.22926S3; Mon, 25 May 2026 18:27:04 +0800 (CST) From: ranxiaokai627@163.com To: ljs@kernel.org, hughd@google.com, baolin.wang@linux.alibaba.com, akpm@linux-foundation.org Cc: leitao@debian.org, ziy@nvidia.com, liam@infradead.org, npache@redhat.com, ryan.roberts@arm.com, dev.jain@arm.com, baohua@kernel.org, lance.yang@linux.dev, david@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, ran.xiaokai@zte.com.cn, ranxiaokai627@163.com Subject: [PATCH v4 1/2] mm: shmem: refactor thpsize_shmem_enabled_store() with sysfs_match_string() Date: Mon, 25 May 2026 10:26:59 +0000 Message-ID: <20260525102700.68707-2-ranxiaokai627@163.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260525102700.68707-1-ranxiaokai627@163.com> References: <20260525102700.68707-1-ranxiaokai627@163.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-CM-TRANSID: _____wD3V9L2IxRqXqahDg--.22926S3 X-Coremail-Antispam: 1Uf129KBjvJXoW3WrW8WFWUAry3Jw48Ar45Awb_yoW7Kr4DpF 4fC34fAry8Jr9rK3yxJF48tryrtrs3Ja1DtrWkJas3Aan8JryYyF1vy348Z34DJryfXr47 KF18WasFgrWDtw7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0ziqXdrUUUUU= X-CM-SenderInfo: xudq5x5drntxqwsxqiywtou0bp/xtbC7Rmm8moUI-mPVQAA3e Content-Type: text/plain; charset="utf-8" From: Ran Xiaokai Inspired by commit 82d9ff648c6c ("mm: huge_memory: refactor anon_enabled_store() with set_anon_enabled_mode()"), refactor thpsize_shmem_enabled_store() using sysfs_match_string(). This eliminates the duplicated spin_lock/unlock(), set/clear_bit(), calls across all branches, reducing code duplication. Behavioral change: Call start_stop_khugepaged() only when the mode actually changes. If unchanged, call set_recommended_min_free_kbytes() to preserve legacy watermark behavior. This avoids unnecessary khugepaged restarts. Tested with selftests ./run_kselftest.sh -t mm:ksft_thp.sh, all test cases passed. Signed-off-by: Ran Xiaokai Reviewed-by: Baolin Wang Reviewed-by: Barry Song Tested-by: Baolin Wang Tested-by: Lance Yang Acked-by: David Hildenbrand (arm) Reviewed-by: Lorenzo Stoakes --- mm/shmem.c | 107 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 60 insertions(+), 47 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 3b5dc21b323c..288942b44db6 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -5526,6 +5526,29 @@ static ssize_t shmem_enabled_store(struct kobject *k= obj, struct kobj_attribute shmem_enabled_attr =3D __ATTR_RW(shmem_enabled); static DEFINE_SPINLOCK(huge_shmem_orders_lock); =20 +enum huge_mode { + HUGE_SHMEM_ENABLED_ALWAYS =3D 0, + HUGE_SHMEM_ENABLED_INHERIT, + HUGE_SHMEM_ENABLED_WITHIN_SIZE, + HUGE_SHMEM_ENABLED_ADVISE, + HUGE_SHMEM_ENABLED_NEVER, +}; + +static const char * const huge_mode_strings[] =3D { + [HUGE_SHMEM_ENABLED_ALWAYS] =3D "always", + [HUGE_SHMEM_ENABLED_INHERIT] =3D "inherit", + [HUGE_SHMEM_ENABLED_WITHIN_SIZE] =3D "within_size", + [HUGE_SHMEM_ENABLED_ADVISE] =3D "advise", + [HUGE_SHMEM_ENABLED_NEVER] =3D "never", +}; + +static unsigned long * const huge_mode_orders[] =3D { + [HUGE_SHMEM_ENABLED_ALWAYS] =3D &huge_shmem_orders_always, + [HUGE_SHMEM_ENABLED_INHERIT] =3D &huge_shmem_orders_inherit, + [HUGE_SHMEM_ENABLED_WITHIN_SIZE] =3D &huge_shmem_orders_within_size, + [HUGE_SHMEM_ENABLED_ADVISE] =3D &huge_shmem_orders_madvise, +}; + static ssize_t thpsize_shmem_enabled_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf) { @@ -5546,63 +5569,53 @@ static ssize_t thpsize_shmem_enabled_show(struct ko= bject *kobj, return sysfs_emit(buf, "%s\n", output); } =20 +static bool set_shmem_enabled_mode(int order, enum huge_mode mode) +{ + bool changed =3D false; + enum huge_mode idx; + + spin_lock(&huge_shmem_orders_lock); + for (idx =3D 0; idx < ARRAY_SIZE(huge_mode_orders); idx++) { + if (idx =3D=3D mode) + changed |=3D !__test_and_set_bit(order, huge_mode_orders[idx]); + else + changed |=3D __test_and_clear_bit(order, huge_mode_orders[idx]); + } + spin_unlock(&huge_shmem_orders_lock); + + return changed; +} + static ssize_t thpsize_shmem_enabled_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) { int order =3D to_thpsize(kobj)->order; - ssize_t ret =3D count; - - if (sysfs_streq(buf, "always")) { - spin_lock(&huge_shmem_orders_lock); - clear_bit(order, &huge_shmem_orders_inherit); - clear_bit(order, &huge_shmem_orders_madvise); - clear_bit(order, &huge_shmem_orders_within_size); - set_bit(order, &huge_shmem_orders_always); - spin_unlock(&huge_shmem_orders_lock); - } else if (sysfs_streq(buf, "inherit")) { - /* Do not override huge allocation policy with non-PMD sized mTHP */ - if (shmem_huge =3D=3D SHMEM_HUGE_FORCE && !is_pmd_order(order)) - return -EINVAL; + int mode; =20 - spin_lock(&huge_shmem_orders_lock); - clear_bit(order, &huge_shmem_orders_always); - clear_bit(order, &huge_shmem_orders_madvise); - clear_bit(order, &huge_shmem_orders_within_size); - set_bit(order, &huge_shmem_orders_inherit); - spin_unlock(&huge_shmem_orders_lock); - } else if (sysfs_streq(buf, "within_size")) { - spin_lock(&huge_shmem_orders_lock); - clear_bit(order, &huge_shmem_orders_always); - clear_bit(order, &huge_shmem_orders_inherit); - clear_bit(order, &huge_shmem_orders_madvise); - set_bit(order, &huge_shmem_orders_within_size); - spin_unlock(&huge_shmem_orders_lock); - } else if (sysfs_streq(buf, "advise")) { - spin_lock(&huge_shmem_orders_lock); - clear_bit(order, &huge_shmem_orders_always); - clear_bit(order, &huge_shmem_orders_inherit); - clear_bit(order, &huge_shmem_orders_within_size); - set_bit(order, &huge_shmem_orders_madvise); - spin_unlock(&huge_shmem_orders_lock); - } else if (sysfs_streq(buf, "never")) { - spin_lock(&huge_shmem_orders_lock); - clear_bit(order, &huge_shmem_orders_always); - clear_bit(order, &huge_shmem_orders_inherit); - clear_bit(order, &huge_shmem_orders_within_size); - clear_bit(order, &huge_shmem_orders_madvise); - spin_unlock(&huge_shmem_orders_lock); - } else { - ret =3D -EINVAL; - } + mode =3D sysfs_match_string(huge_mode_strings, buf); + if (mode < 0) + return mode; =20 - if (ret > 0) { - int err =3D start_stop_khugepaged(); + /* Do not override huge allocation policy with non-PMD sized mTHP */ + if (mode =3D=3D HUGE_SHMEM_ENABLED_INHERIT && + shmem_huge =3D=3D SHMEM_HUGE_FORCE && !is_pmd_order(order)) + return -EINVAL; =20 + if (set_shmem_enabled_mode(order, mode)) { + int err =3D start_stop_khugepaged(); if (err) - ret =3D err; + return err; + } else { + /* + * Recalculate watermarks even when the mode hasn't changed + * to preserve the legacy behavior, as this is always called + * inside start_stop_khugepaged(). + */ + set_recommended_min_free_kbytes(); } - return ret; + + return count; } =20 struct kobj_attribute thpsize_shmem_enabled_attr =3D --=20 2.25.1 From nobody Mon Jun 8 23:57:49 2026 Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.3]) (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 89FEC3559DF for ; Mon, 25 May 2026 10:29:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=117.135.210.3 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779704965; cv=none; b=OyC+Q/Qd1oV1BhPwWosPrxe2CAfusspyID89KGAHW8MEZWmBYwnAxAw4rk7mwLm20RJwTiteNvNRfwIuPLjmRNmaP9kE2+5s5qnKAWK1MVoqI9+MnC+ANyerzAu2veMTA6mIgPwu4nFlSlletHI3rRuFjcaysdJHjhmJVKuLGHM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779704965; c=relaxed/simple; bh=4pYZbjW6czUJ+8JdgtQSwIOvKYXWwkcW+g10qgQql68=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=NVbP8F7ZWJ3COcB0Unx99m8tbYA1n3ngYqJoNUu7zvxmC1fiC/mzVlzAMpBydC9jLB68XbIxRmVpAbQimSPLx2lFaYc/9Jd3+P0Vb0IbP36wUoHrBXguig8w8h0S69RYq93Q/Pk9LF7wVwDoGzJcXdKldzbmkJj+dQHElX1ubo4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com; spf=pass smtp.mailfrom=163.com; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b=mjGAC55V; arc=none smtp.client-ip=117.135.210.3 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=163.com header.i=@163.com header.b="mjGAC55V" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=g+ 6csErv+Pv1Qi+AE0pn2ZKMaZU84igUPr4oR5FIA3g=; b=mjGAC55VKZq5OAW9Qn K6kIwbQ/JOZ1zMhkkJAq6IrIhKWKhhuyf76uJUdMBiMbTjJOryuZecgTsuZYoT4p JIVzMARepXOx8SmPT1HIYqp4vJu+IaZl0OOQepVSG80NcgeGDfRsdgLJibqH7YX2 F1SeyzsrcT+wowWd34a3iMZ/0= Received: from ubuntu24-z.. (unknown []) by gzga-smtp-mtada-g1-4 (Coremail) with SMTP id _____wD3V9L2IxRqXqahDg--.22926S4; Mon, 25 May 2026 18:27:07 +0800 (CST) From: ranxiaokai627@163.com To: ljs@kernel.org, hughd@google.com, baolin.wang@linux.alibaba.com, akpm@linux-foundation.org Cc: leitao@debian.org, ziy@nvidia.com, liam@infradead.org, npache@redhat.com, ryan.roberts@arm.com, dev.jain@arm.com, baohua@kernel.org, lance.yang@linux.dev, david@kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, ran.xiaokai@zte.com.cn, ranxiaokai627@163.com Subject: [PATCH v4 2/2] mm: shmem: refactor thpsize_shmem_enabled_show() with helper arrays Date: Mon, 25 May 2026 10:27:00 +0000 Message-ID: <20260525102700.68707-3-ranxiaokai627@163.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260525102700.68707-1-ranxiaokai627@163.com> References: <20260525102700.68707-1-ranxiaokai627@163.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-CM-TRANSID: _____wD3V9L2IxRqXqahDg--.22926S4 X-Coremail-Antispam: 1Uf129KBjvJXoW7Zw4UZryfZryfXFW5Xr4xJFb_yoW8KFW3pF WfCasIyrW8AFykKrWxWFW8tryFgr4fJ3W5trZrJayrZ3Z8Jrn09F92k34rZ34UA3yrGw4a gr1xuF9FgFyDtw7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0zRwjjkUUUUU= X-CM-SenderInfo: xudq5x5drntxqwsxqiywtou0bp/xtbC7hum8moUI-utfwAA3V Content-Type: text/plain; charset="utf-8" From: Ran Xiaokai Replace the hardcoded if/else chain of test_bit() calls and string literals in thpsize_shmem_enabled_show() with a loop over huge_shmem_orders_by_mode[] and huge_shmem_enabled_mode_strings[] arrays. This makes thpsize_shmem_enabled_show() consistent with thpsize_shmem_enabled_store() and eliminates duplicated mode name strings. Signed-off-by: Ran Xiaokai Reviewed-by: Baolin Wang Reviewed-by: Barry Song Reviewed-by: Breno Leitao Reviewed-by: Lorenzo Stoakes Tested-by: Baolin Wang Tested-by: Lance Yang Acked-by: David Hildenbrand (arm) --- mm/shmem.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/mm/shmem.c b/mm/shmem.c index 288942b44db6..d9b499860433 100644 --- a/mm/shmem.c +++ b/mm/shmem.c @@ -5553,20 +5553,30 @@ static ssize_t thpsize_shmem_enabled_show(struct ko= bject *kobj, struct kobj_attribute *attr, char *buf) { int order =3D to_thpsize(kobj)->order; - const char *output; - - if (test_bit(order, &huge_shmem_orders_always)) - output =3D "[always] inherit within_size advise never"; - else if (test_bit(order, &huge_shmem_orders_inherit)) - output =3D "always [inherit] within_size advise never"; - else if (test_bit(order, &huge_shmem_orders_within_size)) - output =3D "always inherit [within_size] advise never"; - else if (test_bit(order, &huge_shmem_orders_madvise)) - output =3D "always inherit within_size [advise] never"; - else - output =3D "always inherit within_size advise [never]"; + int active =3D HUGE_SHMEM_ENABLED_NEVER; + int len =3D 0; + int i; + + for (i =3D 0; i < ARRAY_SIZE(huge_mode_orders); i++) { + if (test_bit(order, huge_mode_orders[i])) { + active =3D i; + break; + } + } + + for (i =3D 0; i < ARRAY_SIZE(huge_mode_strings); i++) { + if (i =3D=3D active) + len +=3D sysfs_emit_at(buf, len, "[%s] ", + huge_mode_strings[i]); + else + len +=3D sysfs_emit_at(buf, len, "%s ", + huge_mode_strings[i]); + } + + /* Replace trailing space with newline */ + buf[len - 1] =3D '\n'; =20 - return sysfs_emit(buf, "%s\n", output); + return len; } =20 static bool set_shmem_enabled_mode(int order, enum huge_mode mode) --=20 2.25.1