Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

mm: huge_memory: refactor enabled_store() with set_global_enabled_mode()

Refactor enabled_store() to use a new set_global_enabled_mode() helper.
Introduce a separate enum global_enabled_mode and
global_enabled_mode_strings[], mirroring the anon_enabled_mode pattern
from the previous commit.

A separate enum is necessary because the global THP setting does not
support "inherit", only "always", "madvise", and "never". Reusing
anon_enabled_mode would leave a NULL gap in the string array, causing
sysfs_match_string() to stop early and fail to match entries after the
gap.

The helper uses the same loop pattern as set_anon_enabled_mode(),
iterating over an array of flag bit positions and using
test_and_set_bit()/test_and_clear_bit() to track whether the state
actually changed.

Link: https://lkml.kernel.org/r/20260317-thp_logs-v7-3-31eb98fa5a8b@debian.org
Signed-off-by: Breno Leitao <leitao@debian.org>
Reviewed-by: Lorenzo Stoakes (Oracle) <ljs@kernel.org>
Reviewed-by: Zi Yan <ziy@nvidia.com>
Reviewed-by: Baolin Wang <baolin.wang@linux.alibaba.com>
Reviewed-by: Wei Yang <richard.weiyang@gmail.com>
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
Cc: Barry Song <baohua@kernel.org>
Cc: Brendan Jackman <jackmanb@google.com>
Cc: Dev Jain <dev.jain@arm.com>
Cc: Johannes Weiner <hannes@cmpxchg.org>
Cc: Lance Yang <lance.yang@linux.dev>
Cc: Liam Howlett <liam.howlett@oracle.com>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Mike Rapoport <rppt@kernel.org>
Cc: Nico Pache <npache@redhat.com>
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: Suren Baghdasaryan <surenb@google.com>
Cc: Usama Arif <usamaarif642@gmail.com>
Cc: Vlastimil Babka <vbabka@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Breno Leitao and committed by
Andrew Morton
35a01d94 82d9ff64

+48 -15
+48 -15
mm/huge_memory.c
··· 330 330 [ANON_ENABLED_NEVER] = "never", 331 331 }; 332 332 333 + enum global_enabled_mode { 334 + GLOBAL_ENABLED_ALWAYS = 0, 335 + GLOBAL_ENABLED_MADVISE = 1, 336 + GLOBAL_ENABLED_NEVER = 2, 337 + }; 338 + 339 + static const char * const global_enabled_mode_strings[] = { 340 + [GLOBAL_ENABLED_ALWAYS] = "always", 341 + [GLOBAL_ENABLED_MADVISE] = "madvise", 342 + [GLOBAL_ENABLED_NEVER] = "never", 343 + }; 344 + 345 + static bool set_global_enabled_mode(enum global_enabled_mode mode) 346 + { 347 + static const unsigned long thp_flags[] = { 348 + TRANSPARENT_HUGEPAGE_FLAG, 349 + TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, 350 + }; 351 + enum global_enabled_mode m; 352 + bool changed = false; 353 + 354 + for (m = 0; m < ARRAY_SIZE(thp_flags); m++) { 355 + if (m == mode) 356 + changed |= !test_and_set_bit(thp_flags[m], 357 + &transparent_hugepage_flags); 358 + else 359 + changed |= test_and_clear_bit(thp_flags[m], 360 + &transparent_hugepage_flags); 361 + } 362 + 363 + return changed; 364 + } 365 + 333 366 static ssize_t enabled_store(struct kobject *kobj, 334 367 struct kobj_attribute *attr, 335 368 const char *buf, size_t count) 336 369 { 337 - ssize_t ret = count; 370 + int mode; 338 371 339 - if (sysfs_streq(buf, "always")) { 340 - clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags); 341 - set_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags); 342 - } else if (sysfs_streq(buf, "madvise")) { 343 - clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags); 344 - set_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags); 345 - } else if (sysfs_streq(buf, "never")) { 346 - clear_bit(TRANSPARENT_HUGEPAGE_FLAG, &transparent_hugepage_flags); 347 - clear_bit(TRANSPARENT_HUGEPAGE_REQ_MADV_FLAG, &transparent_hugepage_flags); 348 - } else 349 - ret = -EINVAL; 372 + mode = sysfs_match_string(global_enabled_mode_strings, buf); 373 + if (mode < 0) 374 + return -EINVAL; 350 375 351 - if (ret > 0) { 376 + if (set_global_enabled_mode(mode)) { 352 377 int err = start_stop_khugepaged(); 378 + 353 379 if (err) 354 - ret = err; 380 + return err; 381 + } else { 382 + /* 383 + * Recalculate watermarks even when the mode didn't 384 + * change, as the previous code always called 385 + * start_stop_khugepaged() which does this internally. 386 + */ 387 + set_recommended_min_free_kbytes(); 355 388 } 356 - return ret; 389 + return count; 357 390 } 358 391 359 392 static struct kobj_attribute enabled_attr = __ATTR_RW(enabled);