Skip to contentSkip to conversation

mm/hugetlb: refactor sysfs/sysctl interfaces

PATCH New Hui Zhu 1 participant 3 messages Nov 6, 2025

Subsystems (2)
lore
Share & tools
lore

Hui Zhu

author
#0 lore raw

v5 cover mm/hugetlb: refactor sysfs/sysctl interfaces

From: Hui Zhu <[email protected]>

The hugetlb.c file has grown significantly and become difficult to maintain. This patch series extracts the sysfs and sysctl interface code into separate dedicated files to improve code organization.

The refactoring includes:
- Patch 1: Extract sysfs interface into mm/hugetlb_sysfs.c
- Patch 2: Extract sysctl interface into mm/hugetlb_sysctl.c

No functional changes are introduced in this series. The code is moved as-is, with only minor formatting adjustments for code style consistency. This should make future maintenance and enhancements to the hugetlb subsystem easier.

Testing: The patch series has been compile-tested and maintains the same functionality as the original code.

Changelog:
v5:
According to the comments of David Hildenbrand and Oscar Salvador, move demote_free_hugetlb_folios and demote_pool_huge_page back to hugetlb.c.
v4:
According to the comments of David Hildenbrand, add copyright of hugetlb.c to hugetlb_internal.h, hugetlb_sysctl.c and hugetlb_sysfs.c. v3:
According to the comments of SeongJae Park, updated MAINTAINERS to add new files.
Removed the wrong copyright in hugetlb_internal.h.
v2:
According to the comments of David Hildenbrand, removed the wrong copyright in the file headers.

Hui Zhu (2):
mm/hugetlb: extract sysfs into hugetlb_sysfs.c mm/hugetlb: extract sysctl into hugetlb_sysctl.c

 MAINTAINERS           |   2 +
 mm/Makefile           |   2 +-
 mm/hugetlb.c          | 728 +-----------------------------------------
 mm/hugetlb_internal.h | 117 +++++++
 mm/hugetlb_sysctl.c   | 134 ++++++++
 mm/hugetlb_sysfs.c    | 502 +++++++++++++++++++++++++++++
 6 files changed, 767 insertions(+), 718 deletions(-)
 create mode 100644 mm/hugetlb_internal.h
 create mode 100644 mm/hugetlb_sysctl.c
 create mode 100644 mm/hugetlb_sysfs.c
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v5 1/2 mm/hugetlb: extract sysfs into hugetlb_sysfs.c

From: Hui Zhu <[email protected]>

Currently, hugetlb.c contains both core management logic and sysfs interface implementations, making it difficult to maintain. This patch extracts the sysfs-related code into a dedicated file to improve code organization.

The following components are moved to mm/hugetlb_sysfs.c:
- sysfs attribute definitions and handlers
- sysfs kobject management functions
- NUMA per-node hstate attribute registration

Several inline helper functions and macros are moved to mm/hugetlb_internal.h:
- hstate_is_gigantic_no_runtime()
- next_node_allowed()
- get_valid_node_allowed()
- hstate_next_node_to_alloc()
- hstate_next_node_to_free()
- for_each_node_mask_to_alloc/to_free macros

To support code sharing, these functions are changed from static to exported symbols:
- remove_hugetlb_folio()
- add_hugetlb_folio()
- init_new_hugetlb_folio()
- prep_and_add_allocated_folios()
- demote_pool_huge_page()
- __nr_hugepages_store_common()

The Makefile is updated to compile hugetlb_sysfs.o when CONFIG_HUGETLBFS is enabled. This maintains all existing functionality while improving maintainability by separating concerns.

MAINTAINERS is updated to add new file hugetlb_sysfs.c.

---
 MAINTAINERS           |   1 +
 mm/Makefile           |   2 +-
 mm/hugetlb.c          | 596 +-----------------------------------------
 mm/hugetlb_internal.h | 111 ++++++++
 mm/hugetlb_sysfs.c    | 502 +++++++++++++++++++++++++++++++++++
 5 files changed, 626 insertions(+), 586 deletions(-)
 create mode 100644 mm/hugetlb_internal.h
 create mode 100644 mm/hugetlb_sysfs.c
MAINTAINERS +1 -0
index 5a666f4927a4..f318e9bec566 100644
@@ -11539,6 +11539,7 @@ F:	mm/hugetlb.c
 F:	mm/hugetlb_cgroup.c
 F:	mm/hugetlb_cma.c
 F:	mm/hugetlb_cma.h
+F:	mm/hugetlb_sysfs.c
 F:	mm/hugetlb_vmemmap.c
 F:	mm/hugetlb_vmemmap.h
 F:	tools/testing/selftests/cgroup/test_hugetlb_memcg.c
mm/Makefile +1 -1
index 21abb3353550..b9edfce6c202 100644
@@ -78,7 +78,7 @@ endif
 obj-$(CONFIG_SWAP)	+= page_io.o swap_state.o swapfile.o
 obj-$(CONFIG_ZSWAP)	+= zswap.o
 obj-$(CONFIG_HAS_DMA)	+= dmapool.o
-obj-$(CONFIG_HUGETLBFS)	+= hugetlb.o
+obj-$(CONFIG_HUGETLBFS)	+= hugetlb.o hugetlb_sysfs.o
 ifdef CONFIG_CMA
 obj-$(CONFIG_HUGETLBFS)	+= hugetlb_cma.o
 endif
mm/hugetlb.c +11 -585

667 lines not on this page · raw message

mm/hugetlb_internal.h new file +111 -0

111 lines not on this page · raw message

mm/hugetlb_sysfs.c new file +502 -0

502 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v5 2/2 mm/hugetlb: extract sysctl into hugetlb_sysctl.c

In reply to Hui Zhu

From: Hui Zhu <[email protected]>

Following the extraction of sysfs code, this patch moves the sysctl interface implementation into a dedicated file to further improve code organization and maintainability of the hugetlb subsystem.

The following components are moved to mm/hugetlb_sysctl.c:
- proc_hugetlb_doulongvec_minmax()
- hugetlb_sysctl_handler_common()
- hugetlb_sysctl_handler()
- hugetlb_mempolicy_sysctl_handler() (CONFIG_NUMA)
- hugetlb_overcommit_handler()
- hugetlb_table[] sysctl table definition
- hugetlb_sysctl_init()

The hugetlb_internal.h header file is updated to declare the sysctl initialization function with proper #ifdef guards for configurations without CONFIG_SYSCTL support.

The Makefile is updated to compile hugetlb_sysctl.o when CONFIG_HUGETLBFS is enabled. This refactoring reduces the size of hugetlb.c and logically separates the sysctl interface from core hugetlb management code.

MAINTAINERS is updated to add new file hugetlb_sysctl.c.

No functional changes are introduced; all code is moved as-is from hugetlb.c with consistent formatting.

---
 MAINTAINERS           |   1 +
 mm/Makefile           |   2 +-
 mm/hugetlb.c          | 132 -----------------------------------------
 mm/hugetlb_internal.h |   6 ++
 mm/hugetlb_sysctl.c   | 134 ++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 142 insertions(+), 133 deletions(-)
 create mode 100644 mm/hugetlb_sysctl.c
MAINTAINERS +1 -0
index f318e9bec566..fa519cbc4b88 100644
@@ -11539,6 +11539,7 @@ F:	mm/hugetlb.c
 F:	mm/hugetlb_cgroup.c
 F:	mm/hugetlb_cma.c
 F:	mm/hugetlb_cma.h
+F:	mm/hugetlb_sysctl.c
 F:	mm/hugetlb_sysfs.c
 F:	mm/hugetlb_vmemmap.c
 F:	mm/hugetlb_vmemmap.h
mm/Makefile +1 -1
index b9edfce6c202..00ceb2418b64 100644
@@ -78,7 +78,7 @@ endif
 obj-$(CONFIG_SWAP)	+= page_io.o swap_state.o swapfile.o
 obj-$(CONFIG_ZSWAP)	+= zswap.o
 obj-$(CONFIG_HAS_DMA)	+= dmapool.o
-obj-$(CONFIG_HUGETLBFS)	+= hugetlb.o hugetlb_sysfs.o
+obj-$(CONFIG_HUGETLBFS)	+= hugetlb.o hugetlb_sysfs.o hugetlb_sysctl.o
 ifdef CONFIG_CMA
 obj-$(CONFIG_HUGETLBFS)	+= hugetlb_cma.o
 endif
mm/hugetlb.c +0 -132

150 lines not on this page · raw message

mm/hugetlb_internal.h +6 -0
index 5ea372500de7..1d2f870deccf 100644
@@ -108,4 +108,10 @@ extern ssize_t __nr_hugepages_store_common(bool obey_mempolicy,
 
 extern void hugetlb_sysfs_init(void) __init;
 
+#ifdef CONFIG_SYSCTL
+extern void hugetlb_sysctl_init(void);
+#else
+static inline void hugetlb_sysctl_init(void) { }
+#endif
+
 #endif /* _LINUX_HUGETLB_INTERNAL_H */
mm/hugetlb_sysctl.c new file +134 -0

134 lines not on this page · raw message

Signature · 1 line
2.43.0