From nobody Wed Apr 8 04:46:07 2026 Received: from stravinsky.debian.org (stravinsky.debian.org [82.195.75.108]) (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 DAA573B7B61 for ; Tue, 10 Mar 2026 17:58:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=82.195.75.108 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773165502; cv=none; b=cqtb7VX7EFTPEbNRdTcHphn9VfwvFIHGY6937GyMjpvYVv53mrPSwQhXUzW1Xtk5o2AVmFwZIIQro9zi9BpN6hqxDETNEhDQmmaVIz60bA7BXyfd23OmF+8jMuOS+7znhb5TxDYwEJudyhibHtAKov7X/8IlVOtJrLaJZ8YPECo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773165502; c=relaxed/simple; bh=W0pmwY7DpMzVDcPqXGmH/VcJ1h6MSXt/labrb03moRc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=JYuqzReUnNkNLBlhLPOO81oRHXr+FUCY2DLW7ob/19umNKKeYdWFok8OTh2Ue0JbgokBxm8xr8/rGXGuOUjC/7mjYgAFdMzRi7HY7p7nQ9eU7apl1hd1lbpDmBo0j6Qtr/G+P+5Na4gCsSNITs3Y7iye8TjsKR7TrnT7uyXqTEw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=debian.org; spf=none smtp.mailfrom=debian.org; dkim=pass (2048-bit key) header.d=debian.org header.i=@debian.org header.b=qHUYXrdG; arc=none smtp.client-ip=82.195.75.108 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=debian.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=debian.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=debian.org header.i=@debian.org header.b="qHUYXrdG" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debian.org; s=smtpauto.stravinsky; h=X-Debian-User:Cc:To:In-Reply-To:References: Message-Id:Content-Transfer-Encoding:Content-Type:MIME-Version:Subject:Date: From:Reply-To:Content-ID:Content-Description; bh=wuDf6OJ8QUnJ8gfCrD1Pap3z37T2KxqK0+T2vb4JLZk=; b=qHUYXrdGbS8JLo9Z2odggADZmV Qh23xRIiiavV7kgPHSRVV6T5NzCiWjqEf7Xalfj+D8A1p6jSuxZPLG9YVwaocYoeOiu9WKIRqgyIC 61QYgR0vGFjx/i92vfztDJO1hysO+kRAfVY52j+XC39tazR/x1VwbZBYhTZDExCQzOM7RnWKJA3HL WKu3gPS+Dhz1mUTtNqIXUEMiEvKCXRF39ODy/QOGSs6XsEMVSzgWVy1OoLZWux/PG/KPhanyZGPfI IsvoK3/4vN9gjL5ENAFkAYUGs3GsHz464pNAWi1sERBgCqU+x3VZIZcDfmsIiZT8VPWG9zlJPy8An 10PyWNGQ==; Received: from authenticated user by stravinsky.debian.org with esmtpsa (TLS1.3:ECDHE_X25519__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.94.2) (envelope-from ) id 1w01LK-003AZX-M2; Tue, 10 Mar 2026 17:58:06 +0000 From: Breno Leitao Date: Tue, 10 Mar 2026 10:57:08 -0700 Subject: [PATCH v5 2/4] mm: huge_memory: refactor anon_enabled_store() with change_anon_orders() 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 Message-Id: <20260310-thp_logs-v5-2-686099175bf6@debian.org> References: <20260310-thp_logs-v5-0-686099175bf6@debian.org> In-Reply-To: <20260310-thp_logs-v5-0-686099175bf6@debian.org> To: Andrew Morton , David Hildenbrand , Lorenzo Stoakes , Zi Yan , Baolin Wang , "Liam R. Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Vlastimil Babka , Suren Baghdasaryan , Michal Hocko , Brendan Jackman , Johannes Weiner , Mike Rapoport Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, usamaarif642@gmail.com, kas@kernel.org, kernel-team@meta.com, Breno Leitao , "Lorenzo Stoakes (Oracle)" X-Mailer: b4 0.15-dev-363b9 X-Developer-Signature: v=1; a=openpgp-sha256; l=4470; i=leitao@debian.org; h=from:subject:message-id; bh=W0pmwY7DpMzVDcPqXGmH/VcJ1h6MSXt/labrb03moRc=; b=owEBbQKS/ZANAwAIATWjk5/8eHdtAcsmYgBpsFubY8Fq8JCtehLEcWZp56AKZ24T+7XEXQM24 7Vo9qImBG2JAjMEAAEIAB0WIQSshTmm6PRnAspKQ5s1o5Of/Hh3bQUCabBbmwAKCRA1o5Of/Hh3 bd0XEACvvOzGt+2EHXj4MyyuyZzPvTkSZ+Z9cKHtKT6+fOAAsVPpRzLX0pe1j+k7sNoxoMsogkv 591ZDzPfcUd14WHhuF8RJk6Y0utn5b9WlDNFduTUzaeroOF1oasvUqJ/3tQgJf/vmZR1ivwqDcZ gM/J/zabonqKwI4wXwWCzF9sgZhEsXFE+Jty15ip5o6KnOzBFZp1LvwheVLB+pNKP7NDrcKF4Rc XbB9Gr6zK0oont2/PRMslOr7aeYJUlFFqn/nqZ1BdFNly83uGKgPg2OISVaKZ24yIyiC4yZdrtA qMM9pUbZqkyHVPmSalm0Z/NtdxIxTVk3M2YIcRBG5Vyisqh4NGZWSwc24pz/Hd9zRurAGOKdmUt 2RugkypRkI6j2ZtzEI5TdvJe9AhXjqsHIZQGv4mlMx/gXAZ11Ihlhdvn4W3xvy7L7/Z5Bvpyqur lo2WBj/tl8IwVz5jdca5JxU3ejm1P7zB37Ixvgu9iaaJGodfVklbJiI6XqttzkHvQJWqF26wk1g susxa1pX/st3V+K2yaf5ZN+vRysBMIoyWuqTMHO6xqdMYPx/QKE962oapmrfckhthgAr4rVcUmn sh7h0YQRwL50dc3WYfYOWGM++hFvnnTFFg0roi+zarn5a6AhAvw7caaQYdmLEU5qdr1JOY+9tS4 YQQh0wVY5S6BZ6A== X-Developer-Key: i=leitao@debian.org; a=openpgp; fpr=AC8539A6E8F46702CA4A439B35A3939FFC78776D X-Debian-User: leitao Consolidate the repeated spin_lock/set_bit/clear_bit pattern in anon_enabled_store() into a new change_anon_orders() helper that loops over an orders[] array, setting the bit for the selected mode and clearing the others. Introduce enum anon_enabled_mode and anon_enabled_mode_strings[] for the per-order anon THP setting. Use sysfs_match_string() with the anon_enabled_mode_strings[] table to replace the if/else chain of sysfs_streq() calls. The helper uses test_and_set_bit()/test_and_clear_bit() to track whether the state actually changed, so start_stop_khugepaged() is only called when needed. When the mode is unchanged, set_recommended_min_free_kbytes() is called directly to preserve the watermark recalculation behavior of the original code. Signed-off-by: Breno Leitao Reviewed-by: Lorenzo Stoakes (Oracle) Reviewed-by: Baolin Wang Reviewed-by: Lance Yang Reviewed-by: Wei Yang Reviewed-by: Zi Yan --- mm/huge_memory.c | 84 +++++++++++++++++++++++++++++++++++-----------------= ---- 1 file changed, 52 insertions(+), 32 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 8e2746ea74adf..e19dda5aaf195 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -316,6 +316,20 @@ static ssize_t enabled_show(struct kobject *kobj, return sysfs_emit(buf, "%s\n", output); } =20 +enum anon_enabled_mode { + ANON_ENABLED_ALWAYS =3D 0, + ANON_ENABLED_MADVISE =3D 1, + ANON_ENABLED_INHERIT =3D 2, + ANON_ENABLED_NEVER =3D 3, +}; + +static const char * const anon_enabled_mode_strings[] =3D { + [ANON_ENABLED_ALWAYS] =3D "always", + [ANON_ENABLED_MADVISE] =3D "madvise", + [ANON_ENABLED_INHERIT] =3D "inherit", + [ANON_ENABLED_NEVER] =3D "never", +}; + static ssize_t enabled_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count) @@ -515,48 +529,54 @@ static ssize_t anon_enabled_show(struct kobject *kobj, return sysfs_emit(buf, "%s\n", output); } =20 +static bool set_anon_enabled_mode(int order, enum anon_enabled_mode mode) +{ + static unsigned long *enabled_orders[] =3D { + &huge_anon_orders_always, + &huge_anon_orders_madvise, + &huge_anon_orders_inherit, + }; + enum anon_enabled_mode m; + bool changed =3D false; + + spin_lock(&huge_anon_orders_lock); + for (m =3D 0; m < ARRAY_SIZE(enabled_orders); m++) { + if (m =3D=3D mode) + changed |=3D !__test_and_set_bit(order, enabled_orders[m]); + else + changed |=3D __test_and_clear_bit(order, enabled_orders[m]); + } + spin_unlock(&huge_anon_orders_lock); + + return changed; +} + static ssize_t anon_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; + int mode; =20 - if (sysfs_streq(buf, "always")) { - spin_lock(&huge_anon_orders_lock); - clear_bit(order, &huge_anon_orders_inherit); - clear_bit(order, &huge_anon_orders_madvise); - 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); - 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); - set_bit(order, &huge_anon_orders_madvise); - 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); - spin_unlock(&huge_anon_orders_lock); - } else - ret =3D -EINVAL; + mode =3D sysfs_match_string(anon_enabled_mode_strings, buf); + if (mode < 0) + return -EINVAL; =20 - if (ret > 0) { - int err; + if (set_anon_enabled_mode(order, mode)) { + int err =3D start_stop_khugepaged(); =20 - err =3D start_stop_khugepaged(); if (err) - ret =3D err; + return err; + } else { + /* + * Recalculate watermarks even when the mode didn't + * change, as the previous code always called + * start_stop_khugepaged() which does this internally. + */ + set_recommended_min_free_kbytes(); } - return ret; + + return count; } =20 static struct kobj_attribute anon_enabled_attr =3D --=20 2.52.0