Skip to contentSkip to conversation

mm/hugetlb: refactor sysfs/sysctl interfaces

PATCH Superseded Hui Zhu 3 participants 5 messages Nov 5, 2025

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Hui Zhu

author
#0 lore raw

v4 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:
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          | 852 +-----------------------------------------
 mm/hugetlb_internal.h | 114 ++++++
 mm/hugetlb_sysctl.c   | 134 +++++++
 mm/hugetlb_sysfs.c    | 630 +++++++++++++++++++++++++++++++
 6 files changed, 890 insertions(+), 844 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 v4 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:
- hugetlb page demote functions (demote_free_hugetlb_folios, demote_pool_huge_page)
- 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()
- __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          | 720 +-----------------------------------------
 mm/hugetlb_internal.h | 108 +++++++
 mm/hugetlb_sysfs.c    | 630 ++++++++++++++++++++++++++++++++++++
 5 files changed, 749 insertions(+), 712 deletions(-)
 create mode 100644 mm/hugetlb_internal.h
 create mode 100644 mm/hugetlb_sysfs.c
MAINTAINERS +1 -0
index 3240a705d474..36d5e025ff68 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 +9 -711

785 lines not on this page · raw message

mm/hugetlb_internal.h new file +108 -0

108 lines not on this page · raw message

mm/hugetlb_sysfs.c new file +630 -0

630 lines not on this page · raw message

Signature · 1 line
2.43.0
author
#2 lore raw

PATCH v4 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 36d5e025ff68..fa21836c15ae 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 5343448ed145..4a90fdadcd4c 100644
@@ -105,4 +105,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
maintainer
#3 lore raw
In reply to Hui Zhu
Quoted 30 lines · Hui Zhu

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:
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.

Quoted 3 lines

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

I am replying here as a response to https://lore.kernel.org/linux-mm/[email protected]/T/#mf694af3f5a6ca56b76adf66352cbb88d022fe36c

So, taking a look at patch#1 as an example,which moves sysfs stuff into hugetlb_sysfs.c. I have the feeling we are moving too much stuff. It is true that drawing a line is not easy, but e.g: you mention

The following components are moved to mm/hugetlb_sysfs.c:
- hugetlb page demote functions (demote_free_hugetlb_folios, demote_pool_huge_page)

I __think__ that moving demote_store() into hugetlb_sysfs.c is fine, but although demote_pool_huge_page and demote_free_hugetlb_folios are only called from there, they look more than a sysfs interface and more like hugetlb generic code.

Again, drawing a like might be difficult, but I think that e.g: we should only move sysfs entry points functions into hugetlb_sysfs.c

Does that make sense?

Signature · 2 lines
Oscar Salvador
SUSE Labs
#4 lore raw
Quoted 45 lines · Oscar Salvador
Quoted 30 lines · Hui Zhu

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:
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.

Quoted 3 lines

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

I am replying here as a response to https://lore.kernel.org/linux-mm/[email protected]/T/#mf694af3f5a6ca56b76adf66352cbb88d022fe36c

So, taking a look at patch#1 as an example,which moves sysfs stuff into hugetlb_sysfs.c. I have the feeling we are moving too much stuff. It is true that drawing a line is not easy, but e.g: you mention

The following components are moved to mm/hugetlb_sysfs.c:
- hugetlb page demote functions (demote_free_hugetlb_folios, demote_pool_huge_page)

Quoted 6 lines

I __think__ that moving demote_store() into hugetlb_sysfs.c is fine, but although demote_pool_huge_page and demote_free_hugetlb_folios are only called from there, they look more than a sysfs interface and more like hugetlb generic code.

Again, drawing a like might be difficult, but I think that e.g: we should only move sysfs entry points functions into hugetlb_sysfs.c

That matches my comments from [1], right?

[1] https://lore.kernel.org/linux-mm/[email protected]/T/#mda7f5c2ea07233857af094367b573475011bf0d4

Signature · 3 lines
Cheers

David