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/hugetlb: extract sysctl into hugetlb_sysctl.c

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.

Link: https://lkml.kernel.org/r/5bbee7ab5be71d0bb1aebec38642d7e83526bb7a.1762398359.git.zhuhui@kylinos.cn
Signed-off-by: Geliang Tang <geliang@kernel.org>
Signed-off-by: Hui Zhu <zhuhui@kylinos.cn>
Cc: David Hildenbrand <david@redhat.com>
Cc: Muchun Song <muchun.song@linux.dev>
Cc: Oscar Salvador <osalvador@suse.de>
Cc: SeongJae Park <sj@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>

authored by

Hui Zhu and committed by
Andrew Morton
cdcb53e1 ecd6703f

+142 -133
+1
MAINTAINERS
··· 11540 11540 F: mm/hugetlb_cgroup.c 11541 11541 F: mm/hugetlb_cma.c 11542 11542 F: mm/hugetlb_cma.h 11543 + F: mm/hugetlb_sysctl.c 11543 11544 F: mm/hugetlb_sysfs.c 11544 11545 F: mm/hugetlb_vmemmap.c 11545 11546 F: mm/hugetlb_vmemmap.h
+1 -1
mm/Makefile
··· 78 78 obj-$(CONFIG_SWAP) += page_io.o swap_state.o swapfile.o 79 79 obj-$(CONFIG_ZSWAP) += zswap.o 80 80 obj-$(CONFIG_HAS_DMA) += dmapool.o 81 - obj-$(CONFIG_HUGETLBFS) += hugetlb.o hugetlb_sysfs.o 81 + obj-$(CONFIG_HUGETLBFS) += hugetlb.o hugetlb_sysfs.o hugetlb_sysctl.o 82 82 ifdef CONFIG_CMA 83 83 obj-$(CONFIG_HUGETLBFS) += hugetlb_cma.o 84 84 endif
-132
mm/hugetlb.c
··· 7 7 #include <linux/init.h> 8 8 #include <linux/mm.h> 9 9 #include <linux/seq_file.h> 10 - #include <linux/sysctl.h> 11 10 #include <linux/highmem.h> 12 11 #include <linux/mmu_notifier.h> 13 12 #include <linux/nodemask.h> ··· 4110 4111 return err ? err : len; 4111 4112 } 4112 4113 4113 - #ifdef CONFIG_SYSCTL 4114 - static void hugetlb_sysctl_init(void); 4115 - #else 4116 - static inline void hugetlb_sysctl_init(void) { } 4117 - #endif 4118 - 4119 4114 static int __init hugetlb_init(void) 4120 4115 { 4121 4116 int i; ··· 4541 4548 4542 4549 return nr; 4543 4550 } 4544 - 4545 - #ifdef CONFIG_SYSCTL 4546 - static int proc_hugetlb_doulongvec_minmax(const struct ctl_table *table, int write, 4547 - void *buffer, size_t *length, 4548 - loff_t *ppos, unsigned long *out) 4549 - { 4550 - struct ctl_table dup_table; 4551 - 4552 - /* 4553 - * In order to avoid races with __do_proc_doulongvec_minmax(), we 4554 - * can duplicate the @table and alter the duplicate of it. 4555 - */ 4556 - dup_table = *table; 4557 - dup_table.data = out; 4558 - 4559 - return proc_doulongvec_minmax(&dup_table, write, buffer, length, ppos); 4560 - } 4561 - 4562 - static int hugetlb_sysctl_handler_common(bool obey_mempolicy, 4563 - const struct ctl_table *table, int write, 4564 - void *buffer, size_t *length, loff_t *ppos) 4565 - { 4566 - struct hstate *h = &default_hstate; 4567 - unsigned long tmp = h->max_huge_pages; 4568 - int ret; 4569 - 4570 - if (!hugepages_supported()) 4571 - return -EOPNOTSUPP; 4572 - 4573 - ret = proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos, 4574 - &tmp); 4575 - if (ret) 4576 - goto out; 4577 - 4578 - if (write) 4579 - ret = __nr_hugepages_store_common(obey_mempolicy, h, 4580 - NUMA_NO_NODE, tmp, *length); 4581 - out: 4582 - return ret; 4583 - } 4584 - 4585 - static int hugetlb_sysctl_handler(const struct ctl_table *table, int write, 4586 - void *buffer, size_t *length, loff_t *ppos) 4587 - { 4588 - 4589 - return hugetlb_sysctl_handler_common(false, table, write, 4590 - buffer, length, ppos); 4591 - } 4592 - 4593 - #ifdef CONFIG_NUMA 4594 - static int hugetlb_mempolicy_sysctl_handler(const struct ctl_table *table, int write, 4595 - void *buffer, size_t *length, loff_t *ppos) 4596 - { 4597 - return hugetlb_sysctl_handler_common(true, table, write, 4598 - buffer, length, ppos); 4599 - } 4600 - #endif /* CONFIG_NUMA */ 4601 - 4602 - static int hugetlb_overcommit_handler(const struct ctl_table *table, int write, 4603 - void *buffer, size_t *length, loff_t *ppos) 4604 - { 4605 - struct hstate *h = &default_hstate; 4606 - unsigned long tmp; 4607 - int ret; 4608 - 4609 - if (!hugepages_supported()) 4610 - return -EOPNOTSUPP; 4611 - 4612 - tmp = h->nr_overcommit_huge_pages; 4613 - 4614 - if (write && hstate_is_gigantic_no_runtime(h)) 4615 - return -EINVAL; 4616 - 4617 - ret = proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos, 4618 - &tmp); 4619 - if (ret) 4620 - goto out; 4621 - 4622 - if (write) { 4623 - spin_lock_irq(&hugetlb_lock); 4624 - h->nr_overcommit_huge_pages = tmp; 4625 - spin_unlock_irq(&hugetlb_lock); 4626 - } 4627 - out: 4628 - return ret; 4629 - } 4630 - 4631 - static const struct ctl_table hugetlb_table[] = { 4632 - { 4633 - .procname = "nr_hugepages", 4634 - .data = NULL, 4635 - .maxlen = sizeof(unsigned long), 4636 - .mode = 0644, 4637 - .proc_handler = hugetlb_sysctl_handler, 4638 - }, 4639 - #ifdef CONFIG_NUMA 4640 - { 4641 - .procname = "nr_hugepages_mempolicy", 4642 - .data = NULL, 4643 - .maxlen = sizeof(unsigned long), 4644 - .mode = 0644, 4645 - .proc_handler = &hugetlb_mempolicy_sysctl_handler, 4646 - }, 4647 - #endif 4648 - { 4649 - .procname = "hugetlb_shm_group", 4650 - .data = &sysctl_hugetlb_shm_group, 4651 - .maxlen = sizeof(gid_t), 4652 - .mode = 0644, 4653 - .proc_handler = proc_dointvec, 4654 - }, 4655 - { 4656 - .procname = "nr_overcommit_hugepages", 4657 - .data = NULL, 4658 - .maxlen = sizeof(unsigned long), 4659 - .mode = 0644, 4660 - .proc_handler = hugetlb_overcommit_handler, 4661 - }, 4662 - }; 4663 - 4664 - static void __init hugetlb_sysctl_init(void) 4665 - { 4666 - register_sysctl_init("vm", hugetlb_table); 4667 - } 4668 - #endif /* CONFIG_SYSCTL */ 4669 4551 4670 4552 void hugetlb_report_meminfo(struct seq_file *m) 4671 4553 {
+6
mm/hugetlb_internal.h
··· 108 108 109 109 extern void hugetlb_sysfs_init(void) __init; 110 110 111 + #ifdef CONFIG_SYSCTL 112 + extern void hugetlb_sysctl_init(void); 113 + #else 114 + static inline void hugetlb_sysctl_init(void) { } 115 + #endif 116 + 111 117 #endif /* _LINUX_HUGETLB_INTERNAL_H */
+134
mm/hugetlb_sysctl.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * HugeTLB sysfs interfaces. 4 + * (C) Nadia Yvette Chambers, April 2004 5 + */ 6 + 7 + #include <linux/sysctl.h> 8 + 9 + #include "hugetlb_internal.h" 10 + 11 + #ifdef CONFIG_SYSCTL 12 + static int proc_hugetlb_doulongvec_minmax(const struct ctl_table *table, int write, 13 + void *buffer, size_t *length, 14 + loff_t *ppos, unsigned long *out) 15 + { 16 + struct ctl_table dup_table; 17 + 18 + /* 19 + * In order to avoid races with __do_proc_doulongvec_minmax(), we 20 + * can duplicate the @table and alter the duplicate of it. 21 + */ 22 + dup_table = *table; 23 + dup_table.data = out; 24 + 25 + return proc_doulongvec_minmax(&dup_table, write, buffer, length, ppos); 26 + } 27 + 28 + static int hugetlb_sysctl_handler_common(bool obey_mempolicy, 29 + const struct ctl_table *table, int write, 30 + void *buffer, size_t *length, loff_t *ppos) 31 + { 32 + struct hstate *h = &default_hstate; 33 + unsigned long tmp = h->max_huge_pages; 34 + int ret; 35 + 36 + if (!hugepages_supported()) 37 + return -EOPNOTSUPP; 38 + 39 + ret = proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos, 40 + &tmp); 41 + if (ret) 42 + goto out; 43 + 44 + if (write) 45 + ret = __nr_hugepages_store_common(obey_mempolicy, h, 46 + NUMA_NO_NODE, tmp, *length); 47 + out: 48 + return ret; 49 + } 50 + 51 + static int hugetlb_sysctl_handler(const struct ctl_table *table, int write, 52 + void *buffer, size_t *length, loff_t *ppos) 53 + { 54 + 55 + return hugetlb_sysctl_handler_common(false, table, write, 56 + buffer, length, ppos); 57 + } 58 + 59 + #ifdef CONFIG_NUMA 60 + static int hugetlb_mempolicy_sysctl_handler(const struct ctl_table *table, int write, 61 + void *buffer, size_t *length, loff_t *ppos) 62 + { 63 + return hugetlb_sysctl_handler_common(true, table, write, 64 + buffer, length, ppos); 65 + } 66 + #endif /* CONFIG_NUMA */ 67 + 68 + static int hugetlb_overcommit_handler(const struct ctl_table *table, int write, 69 + void *buffer, size_t *length, loff_t *ppos) 70 + { 71 + struct hstate *h = &default_hstate; 72 + unsigned long tmp; 73 + int ret; 74 + 75 + if (!hugepages_supported()) 76 + return -EOPNOTSUPP; 77 + 78 + tmp = h->nr_overcommit_huge_pages; 79 + 80 + if (write && hstate_is_gigantic_no_runtime(h)) 81 + return -EINVAL; 82 + 83 + ret = proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos, 84 + &tmp); 85 + if (ret) 86 + goto out; 87 + 88 + if (write) { 89 + spin_lock_irq(&hugetlb_lock); 90 + h->nr_overcommit_huge_pages = tmp; 91 + spin_unlock_irq(&hugetlb_lock); 92 + } 93 + out: 94 + return ret; 95 + } 96 + 97 + static const struct ctl_table hugetlb_table[] = { 98 + { 99 + .procname = "nr_hugepages", 100 + .data = NULL, 101 + .maxlen = sizeof(unsigned long), 102 + .mode = 0644, 103 + .proc_handler = hugetlb_sysctl_handler, 104 + }, 105 + #ifdef CONFIG_NUMA 106 + { 107 + .procname = "nr_hugepages_mempolicy", 108 + .data = NULL, 109 + .maxlen = sizeof(unsigned long), 110 + .mode = 0644, 111 + .proc_handler = &hugetlb_mempolicy_sysctl_handler, 112 + }, 113 + #endif 114 + { 115 + .procname = "hugetlb_shm_group", 116 + .data = &sysctl_hugetlb_shm_group, 117 + .maxlen = sizeof(gid_t), 118 + .mode = 0644, 119 + .proc_handler = proc_dointvec, 120 + }, 121 + { 122 + .procname = "nr_overcommit_hugepages", 123 + .data = NULL, 124 + .maxlen = sizeof(unsigned long), 125 + .mode = 0644, 126 + .proc_handler = hugetlb_overcommit_handler, 127 + }, 128 + }; 129 + 130 + void __init hugetlb_sysctl_init(void) 131 + { 132 + register_sysctl_init("vm", hugetlb_table); 133 + } 134 + #endif /* CONFIG_SYSCTL */