Skip to contentSkip to conversation

mm/hugetlb: refactor sysfs/sysctl interfaces

PATCH Superseded Hui Zhu 3 participants 8 messages Nov 3, 2025 – Nov 4, 2025

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Hui Zhu

author
#0 lore raw

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.

Geliang Tang (1):
mm/hugetlb: extract sysfs into hugetlb_sysfs.c

Hui Zhu (1):
mm/hugetlb: extract sysctl into hugetlb_sysctl.c

 mm/Makefile           |   2 +-
 mm/hugetlb.c          | 852 +-----------------------------------------
 mm/hugetlb_internal.h | 116 ++++++
 mm/hugetlb_sysctl.c   | 136 +++++++
 mm/hugetlb_sysfs.c    | 632 +++++++++++++++++++++++++++++++
 5 files changed, 894 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 1/2 mm/hugetlb: extract sysfs into hugetlb_sysfs.c

From: Geliang Tang <[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.

---
 mm/Makefile           |   2 +-
 mm/hugetlb.c          | 720 +-----------------------------------------
 mm/hugetlb_internal.h | 110 +++++++
 mm/hugetlb_sysfs.c    | 632 ++++++++++++++++++++++++++++++++++++
 4 files changed, 752 insertions(+), 712 deletions(-)
 create mode 100644 mm/hugetlb_internal.h
 create mode 100644 mm/hugetlb_sysfs.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 +110 -0

110 lines not on this page · raw message

mm/hugetlb_sysfs.c new file +632 -0

632 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/2] mm/hugetlb: extract sysfs into hugetlb_sysfs.c

Quoted 38 lines · Hui Zhu

From: Geliang Tang <[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.

---

[...]

mm/hugetlb_internal.h line 7

Quoted 11 lines

index 000000000000..63ab13cfb072

mm/hugetlb_internal.h new file +7 -0
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Internal HugeTLB definitions.
+ *
+ * Copyright (C) 2025 KylinSoft Corporation.
+ * Author: Geliang Tang <[email protected]>
+ */

So, you move some code and suddenly have copyright and authored that code.

Especially given the cove letter says "The code is moved as-is, with only minor formatting adjustments for code style consistency."

?

Signature · 3 lines
Cheers

David
#3 lore raw

Re: [PATCH 1/2] mm/hugetlb: extract sysfs into hugetlb_sysfs.c

2025年11月4日 00:28, "David Hildenbrand (Red Hat)" <[email protected] mailto:[email protected]?to=%22David%20Hildenbrand%20(Red%20Hat)%22%20%3Cdavid%40kernel.org%3E > 写到:

Quoted 52 lines · Hui Zhu
Quoted 35 lines

From: Geliang Tang <[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.

---

[...]

Quoted 13 lines

index 000000000000..63ab13cfb072
--- /dev/null
+++ b/mm/hugetlb_internal.h
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Internal HugeTLB definitions.
+ *
+ * Copyright (C) 2025 KylinSoft Corporation.
+ * Author: Geliang Tang <[email protected]>
+ */

Quoted 7 lines

So, you move some code and suddenly have copyright and authored that code.

Especially given the cove letter says "The code is moved as-is, with only minor formatting adjustments for code style consistency."

?

Thanks for your remind.
Remove the wrong copyright according to your comments.

Best,
Hui

Quoted 5 lines

-- Cheers

David

#4 lore raw

Re: [PATCH 1/2] mm/hugetlb: extract sysfs into hugetlb_sysfs.c

Quoted 57 lines · On 04.11.25 03:41, [email protected] wrote:

2025年11月4日 00:28, "David Hildenbrand (Red Hat)" <[email protected] mailto:[email protected]?to=%22David%20Hildenbrand%20(Red%20Hat)%22%20%3Cdavid%40kernel.org%3E > 写到:

Quoted 54 lines · Hui Zhu
Quoted 35 lines

From: Geliang Tang <[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.

---

[...]

Quoted 13 lines

index 000000000000..63ab13cfb072
--- /dev/null
+++ b/mm/hugetlb_internal.h
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Internal HugeTLB definitions.
+ *
+ * Copyright (C) 2025 KylinSoft Corporation.
+ * Author: Geliang Tang <[email protected]>
+ */

So, you move some code and suddenly have copyright and authored that code.

Quoted 8 lines
Quoted 5 lines

Especially given the cove letter says "The code is moved as-is, with only minor formatting adjustments for code style consistency."

?

Thanks for your remind.
Remove the wrong copyright according to your comments.

You should keep/use the ones from where you move the code originally. Do the same for patch #2, obviously.

Signature · 3 lines
Cheers

David
author
#5 lore raw

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

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

---
 mm/Makefile           |   2 +-
 mm/hugetlb.c          | 132 ----------------------------------------
 mm/hugetlb_internal.h |   6 ++
 mm/hugetlb_sysctl.c   | 136 ++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 143 insertions(+), 133 deletions(-)
 create mode 100644 mm/hugetlb_sysctl.c
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 63ab13cfb072..8c303d666fd8 100644
@@ -107,4 +107,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 +136 -0

136 lines not on this page · raw message

Signature · 1 line
2.43.0
#6 lore raw
In reply to Hui Zhu
Quoted 24 lines · On Mon, 3 Nov 2025 16:22:07 +0800 Hui Zhu <[email protected]> wrote:

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.

Geliang Tang (1):
mm/hugetlb: extract sysfs into hugetlb_sysfs.c

Hui Zhu (1):
mm/hugetlb: extract sysctl into hugetlb_sysctl.c

Quoted 9 lines
 mm/Makefile           |   2 +-
 mm/hugetlb.c          | 852 +-----------------------------------------
 mm/hugetlb_internal.h | 116 ++++++
 mm/hugetlb_sysctl.c   | 136 +++++++
 mm/hugetlb_sysfs.c    | 632 +++++++++++++++++++++++++++++++
 5 files changed, 894 insertions(+), 844 deletions(-)
 create mode 100644 mm/hugetlb_internal.h
 create mode 100644 mm/hugetlb_sysctl.c
 create mode 100644 mm/hugetlb_sysfs.c

I think MAINTAINERS file should also be updated for the newly added files.

Thanks,
SJ

[...]

#7 lore raw

2025年11月4日 12:43, "SeongJae Park" <[email protected] mailto:[email protected]?to=%22SeongJae%20Park%22%20%3Csj%40kernel.org%3E > 写到:

Quoted 38 lines · On Mon, 3 Nov 2025 16:22:07 +0800 Hui Zhu <[email protected]> wrote:
Quoted 26 lines

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.

Geliang Tang (1):
mm/hugetlb: extract sysfs into hugetlb_sysfs.c

Hui Zhu (1):
mm/hugetlb: extract sysctl into hugetlb_sysctl.c

Quoted 10 lines
 mm/Makefile | 2 +-
 mm/hugetlb.c | 852 +-----------------------------------------
 mm/hugetlb_internal.h | 116 ++++++
 mm/hugetlb_sysctl.c | 136 +++++++
 mm/hugetlb_sysfs.c | 632 +++++++++++++++++++++++++++++++
 5 files changed, 894 insertions(+), 844 deletions(-)
 create mode 100644 mm/hugetlb_internal.h
 create mode 100644 mm/hugetlb_sysctl.c
 create mode 100644 mm/hugetlb_sysfs.c
Quoted 2 lines

I think MAINTAINERS file should also be updated for the newly added files.

Updated MAINTAINERS to v3 version according your comments.

Best,
Hui

Quoted 5 lines

Thanks,
SJ

[...]