From nobody Tue Apr 7 23:42:30 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 674DF3C9ECC for ; Wed, 11 Mar 2026 10:19:47 +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=1773224389; cv=none; b=sG+VaEEph/RLzRwD0hvd4OywU4HiI2fajUdSwOQ/wzep6P7l7Fl4UV/6irp6Ohj/7Qvozn2rLBwe+tNC67v8MsK+8twO6AmmGh/EAfzA42SdAK4nd7WEu+ddNbqOt/kBLfzI5k4HRjgQo/THILUc8wQu+yjM9Z5ggKvmSQfQUn8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773224389; c=relaxed/simple; bh=t2LGZ41pcWRU3yvwZ5U6d39fdwdrml2WrYQvffEHhZc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=nhj5acqr2uzojPqRQL5eNvWBx8oZ2nDEehGUvs5LVq+nFWD0e66WfVKoKd8iFGe98mJ/ND+luweGQ3+tncth4Q9DBkKYQUPwfjAQrXSuB7ku2/3qe5a3FrH8PJm1YGGuTcIdY6QN7PxDdYRcmA9WSLeM3g+xxePjpXKht9V4Nhk= 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=LMh+0ell; 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="LMh+0ell" 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=5yreLjK2erbVMXiW7KuM89LaFb1/wtMtolE8GC0kxjU=; b=LMh+0elltPbfwMimFH88WYC0r+ fah3mKQfMhkJHQoysEZ/5O5eMzzfV/Er9Wkt4vnwL3zRxwsEu9kCOVXckWPxRIowK3Z9ZAClA/B4X beUvhxI6BfCG3Wb/XAwuynIQbos1Al2XzZrj8TnewjRaz79yTwObeYx3rf0cfV+RGPnkdxrMd+UuZ RCCBfB5UoAZs+Q39+ssD4aMmZeEMg57X1mBau4RNBk4m13QrWp/r+GcXJ/qsRH0TNgRe/3frZdqRm mFzw7Q9SNUFG/4Z6wCTn0VBUNieHYMDbBh06hA0iV548mVtqHcQ9BkQ3Tu35dG21xqLu51g6YZqim jfQJxckQ==; 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 1w0Gex-003hKa-9G; Wed, 11 Mar 2026 10:19:23 +0000 From: Breno Leitao Date: Wed, 11 Mar 2026 03:17:48 -0700 Subject: [PATCH v6 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: <20260311-thp_logs-v6-2-421e30d881e0@debian.org> References: <20260311-thp_logs-v6-0-421e30d881e0@debian.org> In-Reply-To: <20260311-thp_logs-v6-0-421e30d881e0@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=t2LGZ41pcWRU3yvwZ5U6d39fdwdrml2WrYQvffEHhZc=; b=owEBbQKS/ZANAwAIATWjk5/8eHdtAcsmYgBpsUGZitv6aRdp3z+vgNYyxni53MMg3FfbFI4Qi HH5hHhgdcqJAjMEAAEIAB0WIQSshTmm6PRnAspKQ5s1o5Of/Hh3bQUCabFBmQAKCRA1o5Of/Hh3 bbVTD/9JwPnc8xhW6wjSWEGZW9FPCmHYyKOIkyhmrooZSH9MOcwFws8CJ1ZYvpGQh8Q4cxfgJSB gI/cMYFoMOOBR8UJR9L4DE7ZPKMCMhhbdgkXohP5Jo1XgNBAKII0ApXyiWjcsu1YU++XTQqgCKy kAnDxnr5keYydPqw0N7HVOWbcKX0WA5hWXhvyxsMa/T+fRWqH1yAnQ6AbatOkifzY3yumabdFpV 3HHZ5usiH9n5CO0z1mWbSIekpHh3xguim1k5t5J1xWuQ+qm0ZWLAZVH9SJVqD+d5Jccp17sSGUO Bbm0kBi4HakJmoGrM9QgCe7XEos/urbQYez7cDlaNql6WKgTK4C3EimXNiSAlSA57eymaMf9iRk J6l2f/vKBxWIUqwzoP5ej+X4MvvL8cUup6jt3fnzc/HpkvjWTaFuuUpzD5tuvwcrDfoB6It0AH2 JgEGZtq4pTK8W4aL4j70pOillgHv8v7efzJQVakhbi2wi99A7eseGm31PpV/JYZUqiiB7pM2/zs DyslufFzXDkGEHJpBDlOr8ON68u5IcBDmIE20r+svoSwPggFluhaFAiXHAnqxrN0IdhvMF5IG6U bX/pg/Xyoz3DdzN3KP//Ewk5GvZf/8NI6TiFM73E60Iy6W/yqtBL0llF5NqsfPFnbkmZxNzboIr C7OaZzEobKJ+oxg== 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) Acked-by: David Hildenbrand (Arm) Tested-by: Lance Yang --- 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..f6af90e6cf05d 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_INHERIT =3D 1, + ANON_ENABLED_MADVISE =3D 2, + ANON_ENABLED_NEVER =3D 3, +}; + +static const char * const anon_enabled_mode_strings[] =3D { + [ANON_ENABLED_ALWAYS] =3D "always", + [ANON_ENABLED_INHERIT] =3D "inherit", + [ANON_ENABLED_MADVISE] =3D "madvise", + [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_inherit, + &huge_anon_orders_madvise, + }; + 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