From nobody Fri Dec 19 12:32:39 2025 Received: from out-176.mta1.migadu.com (out-176.mta1.migadu.com [95.215.58.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5FF097260F for ; Wed, 5 Nov 2025 02:43:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762310592; cv=none; b=K7e3PJCt/EeekdF632rRiu4EXwqMqJvJxpNRyCqK/zia5vrI0U7DKjyDalbkMNA29kjGqMsG40xQgh1vrMi146WVBnzOhNX5AXj8azLFueeTKe5gBEWTF7Sy6RRa6WZ39Ifm5tlw5Sg3Prf4mFNxEALXJ7apJ3ArmZajg10zfv4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1762310592; c=relaxed/simple; bh=UvliTGGHK4uJ5iaKxs65hPPhv8c0ub5IE4LH9sOKTa8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=BrwoO90ieZwEhq9bJXU//tfpidu4LYZ1AfS+7Ve2gtCC2iHhvWtGd05IsCpIhJCr6Hgv8MTY2bDsP8mIDBkeBqxKkr0GyDqIH4DGi+gs5io4yteqcwvttvmdEfJupeqS/bYBJVgD2V7tUjaHcXb2RwF3HpddVxeeiZqfCATUHGI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=jSyo9SFU; arc=none smtp.client-ip=95.215.58.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="jSyo9SFU" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1762310588; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vkc5Ewd2xrQK+NHTdzKpoLGQuxTK8qbsSefExMKhLTA=; b=jSyo9SFUD8RonEN7d/d5aMXETvEB0rxgU3F4xZPrlqqgJy+CbwxU951cwrDLuIFmgAvnns MRK49sERNCV/0R24rCK0WTi0MF93ZpJBLgGZx+a4TlnTaILFy3/lgZtWt0Zy0ro10ZRhqV z+ubB1Nl9brFrw5TsinfoFia+d040cI= From: Hui Zhu To: Andrew Morton , Muchun Song , Oscar Salvador , David Hildenbrand , linux-kernel@vger.kernel.org, linux-mm@kvack.org Cc: Hui Zhu , Geliang Tang Subject: [PATCH v4 2/2] mm/hugetlb: extract sysctl into hugetlb_sysctl.c Date: Wed, 5 Nov 2025 10:42:44 +0800 Message-ID: In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" From: Hui Zhu 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. Signed-off-by: Geliang Tang Signed-off-by: Hui Zhu --- 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 diff --git a/MAINTAINERS b/MAINTAINERS index 36d5e025ff68..fa21836c15ae 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -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 diff --git a/mm/Makefile b/mm/Makefile index b9edfce6c202..00ceb2418b64 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -78,7 +78,7 @@ endif obj-$(CONFIG_SWAP) +=3D page_io.o swap_state.o swapfile.o obj-$(CONFIG_ZSWAP) +=3D zswap.o obj-$(CONFIG_HAS_DMA) +=3D dmapool.o -obj-$(CONFIG_HUGETLBFS) +=3D hugetlb.o hugetlb_sysfs.o +obj-$(CONFIG_HUGETLBFS) +=3D hugetlb.o hugetlb_sysfs.o hugetlb_sysctl.o ifdef CONFIG_CMA obj-$(CONFIG_HUGETLBFS) +=3D hugetlb_cma.o endif diff --git a/mm/hugetlb.c b/mm/hugetlb.c index cb4d4f86f7f9..a5c9061c6813 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -7,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -3983,12 +3982,6 @@ ssize_t __nr_hugepages_store_common(bool obey_mempol= icy, return err ? err : len; } =20 -#ifdef CONFIG_SYSCTL -static void hugetlb_sysctl_init(void); -#else -static inline void hugetlb_sysctl_init(void) { } -#endif - static int __init hugetlb_init(void) { int i; @@ -4421,131 +4414,6 @@ static unsigned int allowed_mems_nr(struct hstate *= h) return nr; } =20 -#ifdef CONFIG_SYSCTL -static int proc_hugetlb_doulongvec_minmax(const struct ctl_table *table, i= nt write, - void *buffer, size_t *length, - loff_t *ppos, unsigned long *out) -{ - struct ctl_table dup_table; - - /* - * In order to avoid races with __do_proc_doulongvec_minmax(), we - * can duplicate the @table and alter the duplicate of it. - */ - dup_table =3D *table; - dup_table.data =3D out; - - return proc_doulongvec_minmax(&dup_table, write, buffer, length, ppos); -} - -static int hugetlb_sysctl_handler_common(bool obey_mempolicy, - const struct ctl_table *table, int write, - void *buffer, size_t *length, loff_t *ppos) -{ - struct hstate *h =3D &default_hstate; - unsigned long tmp =3D h->max_huge_pages; - int ret; - - if (!hugepages_supported()) - return -EOPNOTSUPP; - - ret =3D proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos, - &tmp); - if (ret) - goto out; - - if (write) - ret =3D __nr_hugepages_store_common(obey_mempolicy, h, - NUMA_NO_NODE, tmp, *length); -out: - return ret; -} - -static int hugetlb_sysctl_handler(const struct ctl_table *table, int write, - void *buffer, size_t *length, loff_t *ppos) -{ - - return hugetlb_sysctl_handler_common(false, table, write, - buffer, length, ppos); -} - -#ifdef CONFIG_NUMA -static int hugetlb_mempolicy_sysctl_handler(const struct ctl_table *table,= int write, - void *buffer, size_t *length, loff_t *ppos) -{ - return hugetlb_sysctl_handler_common(true, table, write, - buffer, length, ppos); -} -#endif /* CONFIG_NUMA */ - -static int hugetlb_overcommit_handler(const struct ctl_table *table, int w= rite, - void *buffer, size_t *length, loff_t *ppos) -{ - struct hstate *h =3D &default_hstate; - unsigned long tmp; - int ret; - - if (!hugepages_supported()) - return -EOPNOTSUPP; - - tmp =3D h->nr_overcommit_huge_pages; - - if (write && hstate_is_gigantic_no_runtime(h)) - return -EINVAL; - - ret =3D proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos, - &tmp); - if (ret) - goto out; - - if (write) { - spin_lock_irq(&hugetlb_lock); - h->nr_overcommit_huge_pages =3D tmp; - spin_unlock_irq(&hugetlb_lock); - } -out: - return ret; -} - -static const struct ctl_table hugetlb_table[] =3D { - { - .procname =3D "nr_hugepages", - .data =3D NULL, - .maxlen =3D sizeof(unsigned long), - .mode =3D 0644, - .proc_handler =3D hugetlb_sysctl_handler, - }, -#ifdef CONFIG_NUMA - { - .procname =3D "nr_hugepages_mempolicy", - .data =3D NULL, - .maxlen =3D sizeof(unsigned long), - .mode =3D 0644, - .proc_handler =3D &hugetlb_mempolicy_sysctl_handler, - }, -#endif - { - .procname =3D "hugetlb_shm_group", - .data =3D &sysctl_hugetlb_shm_group, - .maxlen =3D sizeof(gid_t), - .mode =3D 0644, - .proc_handler =3D proc_dointvec, - }, - { - .procname =3D "nr_overcommit_hugepages", - .data =3D NULL, - .maxlen =3D sizeof(unsigned long), - .mode =3D 0644, - .proc_handler =3D hugetlb_overcommit_handler, - }, -}; - -static void __init hugetlb_sysctl_init(void) -{ - register_sysctl_init("vm", hugetlb_table); -} -#endif /* CONFIG_SYSCTL */ - void hugetlb_report_meminfo(struct seq_file *m) { struct hstate *h; diff --git a/mm/hugetlb_internal.h b/mm/hugetlb_internal.h index 5343448ed145..4a90fdadcd4c 100644 --- a/mm/hugetlb_internal.h +++ b/mm/hugetlb_internal.h @@ -105,4 +105,10 @@ extern ssize_t __nr_hugepages_store_common(bool obey_m= empolicy, =20 extern void hugetlb_sysfs_init(void) __init; =20 +#ifdef CONFIG_SYSCTL +extern void hugetlb_sysctl_init(void); +#else +static inline void hugetlb_sysctl_init(void) { } +#endif + #endif /* _LINUX_HUGETLB_INTERNAL_H */ diff --git a/mm/hugetlb_sysctl.c b/mm/hugetlb_sysctl.c new file mode 100644 index 000000000000..bd3077150542 --- /dev/null +++ b/mm/hugetlb_sysctl.c @@ -0,0 +1,134 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * HugeTLB sysfs interfaces. + * (C) Nadia Yvette Chambers, April 2004 + */ + +#include + +#include "hugetlb_internal.h" + +#ifdef CONFIG_SYSCTL +static int proc_hugetlb_doulongvec_minmax(const struct ctl_table *table, i= nt write, + void *buffer, size_t *length, + loff_t *ppos, unsigned long *out) +{ + struct ctl_table dup_table; + + /* + * In order to avoid races with __do_proc_doulongvec_minmax(), we + * can duplicate the @table and alter the duplicate of it. + */ + dup_table =3D *table; + dup_table.data =3D out; + + return proc_doulongvec_minmax(&dup_table, write, buffer, length, ppos); +} + +static int hugetlb_sysctl_handler_common(bool obey_mempolicy, + const struct ctl_table *table, int write, + void *buffer, size_t *length, loff_t *ppos) +{ + struct hstate *h =3D &default_hstate; + unsigned long tmp =3D h->max_huge_pages; + int ret; + + if (!hugepages_supported()) + return -EOPNOTSUPP; + + ret =3D proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos, + &tmp); + if (ret) + goto out; + + if (write) + ret =3D __nr_hugepages_store_common(obey_mempolicy, h, + NUMA_NO_NODE, tmp, *length); +out: + return ret; +} + +static int hugetlb_sysctl_handler(const struct ctl_table *table, int write, + void *buffer, size_t *length, loff_t *ppos) +{ + + return hugetlb_sysctl_handler_common(false, table, write, + buffer, length, ppos); +} + +#ifdef CONFIG_NUMA +static int hugetlb_mempolicy_sysctl_handler(const struct ctl_table *table,= int write, + void *buffer, size_t *length, loff_t *ppos) +{ + return hugetlb_sysctl_handler_common(true, table, write, + buffer, length, ppos); +} +#endif /* CONFIG_NUMA */ + +static int hugetlb_overcommit_handler(const struct ctl_table *table, int w= rite, + void *buffer, size_t *length, loff_t *ppos) +{ + struct hstate *h =3D &default_hstate; + unsigned long tmp; + int ret; + + if (!hugepages_supported()) + return -EOPNOTSUPP; + + tmp =3D h->nr_overcommit_huge_pages; + + if (write && hstate_is_gigantic_no_runtime(h)) + return -EINVAL; + + ret =3D proc_hugetlb_doulongvec_minmax(table, write, buffer, length, ppos, + &tmp); + if (ret) + goto out; + + if (write) { + spin_lock_irq(&hugetlb_lock); + h->nr_overcommit_huge_pages =3D tmp; + spin_unlock_irq(&hugetlb_lock); + } +out: + return ret; +} + +static const struct ctl_table hugetlb_table[] =3D { + { + .procname =3D "nr_hugepages", + .data =3D NULL, + .maxlen =3D sizeof(unsigned long), + .mode =3D 0644, + .proc_handler =3D hugetlb_sysctl_handler, + }, +#ifdef CONFIG_NUMA + { + .procname =3D "nr_hugepages_mempolicy", + .data =3D NULL, + .maxlen =3D sizeof(unsigned long), + .mode =3D 0644, + .proc_handler =3D &hugetlb_mempolicy_sysctl_handler, + }, +#endif + { + .procname =3D "hugetlb_shm_group", + .data =3D &sysctl_hugetlb_shm_group, + .maxlen =3D sizeof(gid_t), + .mode =3D 0644, + .proc_handler =3D proc_dointvec, + }, + { + .procname =3D "nr_overcommit_hugepages", + .data =3D NULL, + .maxlen =3D sizeof(unsigned long), + .mode =3D 0644, + .proc_handler =3D hugetlb_overcommit_handler, + }, +}; + +void __init hugetlb_sysctl_init(void) +{ + register_sysctl_init("vm", hugetlb_table); +} +#endif /* CONFIG_SYSCTL */ --=20 2.43.0