[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <fmuw35nyxafum22ni7ozhybjaarf77baqq774czok4k6y6eqqb@4oabqflx7ryz>
Date: Sat, 27 Dec 2025 08:21:16 +0100
From: Mateusz Guzik <mjguzik@...il.com>
To: 李喆 <lizhe.67@...edance.com>
Cc: muchun.song@...ux.dev, osalvador@...e.de, david@...nel.org,
akpm@...ux-foundation.org, fvdl@...gle.com, linux-mm@...ck.org,
linux-kernel@...r.kernel.org
Subject: Re: [PATCH 0/8] Introduce a huge-page pre-zeroing mechanism
On Thu, Dec 25, 2025 at 04:20:51PM +0800, 李喆 wrote:
> From: Li Zhe <lizhe.67@...edance.com>
>
> This patchset is based on this commit[1]("mm/hugetlb: optionally
> pre-zero hugetlb pages").
>
> Fresh hugetlb pages are zeroed out when they are faulted in,
> just like with all other page types. This can take up a good
> amount of time for larger page sizes (e.g. around 40
> milliseconds for a 1G page on a recent AMD-based system).
>
> This normally isn't a problem, since hugetlb pages are typically
> mapped by the application for a long time, and the initial
> delay when touching them isn't much of an issue.
>
> However, there are some use cases where a large number of hugetlb
> pages are touched when an application (such as a VM backed by these
> pages) starts. For 256 1G pages and 40ms per page, this would take
> 10 seconds, a noticeable delay.
>
> To accelerate the above scenario, this patchset exports a per-node,
> read-write zeroable_hugepages interface for every hugepage size.
> This interface reports how many hugepages on that node can currently
> be pre-zeroed and allows user space to request that any integer number
> in the range [0, max] be zeroed in a single operation.
>
> This mechanism offers the following advantages:
>
> (1) User space gains full control over when zeroing is triggered,
> enabling it to minimize the impact on both CPU and cache utilization.
>
> (2) Applications can spawn as many zeroing processes as they need,
> enabling concurrent background zeroing.
>
> (3) By binding the process to specific CPUs, users can confine zeroing
> threads to cores that do not run latency-critical tasks, eliminating
> interference.
>
> (4) A zeroing process can be interrupted at any time through standard
> signal mechanisms, allowing immediate cancellation.
>
> (5) The CPU consumption incurred by zeroing can be throttled and contained
> with cgroups, ensuring that the cost is not borne system-wide.
>
> On an AMD Milan platform, each 1 GB huge-page fault is shortened by at
> least 25628 us (figure inherited from the test results cited herein[1]).
>
> In user space, we can use system calls such as epoll and write to zero
> huge pages as they become available, and sleep when none are ready. The
> following pseudocode illustrates this approach. The pseudocode spawns
> eight threads that wait for huge pages on node 0 to become eligible for
> zeroing; whenever such pages are available, the threads clear them in
> parallel.
>
> static void thread_fun(void)
> {
> epoll_create();
> epoll_ctl();
> while (1) {
> val = read("/sys/devices/system/node/node0/hugepages/hugepages-1048576kB/zeroable_hugepages");
> if (val > 0)
> system("echo max > /sys/devices/system/node/node0/hugepages/hugepages-1048576kB/zeroable_hugepages");
> epoll_wait();
> }
> }
>
> static void start_pre_zero_thread(int thread_num)
> {
> create_pre_zero_threads(thread_num, thread_fun)
> }
>
> int main(void)
> {
> start_pre_zero_thread(8);
> }
>
In the name of "provide tools, not policy" making userspace call the
shots is the right approach, which I advocated for in the original
thread.
I do have concerns about the specific interface as I think it is a
little too limited.
Suppose vastly different deployments with different needs. For example
one may want to keep at least n pages ready to use, RAM permitting.
At the same time it perhaps would like to balance CPU usage vs other
tasks, so for example it would control parallelism based on observed
churn rate.
So a toolset I would consider viable would need to provide an extensible
interface to future-proof it.
As for an immediate need not met with the current patchset, there is no
configurable threshold for free zeroed page count to generate a wake up.
I suspect a bunch of ioctls would be needed here.
I don't know if sysfs is viable at all for this. Worst case a device (or
a set of per-node devices) can be created with the same goal.
For illustrative purposes perhaps something like this:
I'm assuming centralized file/device. the node parameter can be dropped
otherwise.
struct hugepage_zero_req {
int version; /* version of the struct for extensibility purposes; alternatively different versions can use differrent ioctls */
int node; /* numa node to zero in */
int pages; /* max pages to zero out in this call */
}
then interested threads can do:
struct hugepage_zero_req hzr { .node = 0; pages = UINT_MAX; }
pages = ioctl(hfd, HUGEPAGE_ZERO_PERFORM, &hzr); /* return pages zeroed */
struct hugepage_zero_configure {
int version;
int node; /* numa node to watch, open the device more times for other nodes */
int minfree; /* issue a wake up if free pages drop below this value */
}
and of course:
struct hugepage_zero_query {
int version;
int node;
size_t total_pages; /* all pages installed in the domain */
size_t free_pages; /* total free pages */
size_t free_huge_pages; /* total free huge pages */
size_t zeroed_huge_pages; /* huge pages ready to use */
size_t pad[MEDIUM_NUM]; /* optionally make extensible without abi breakage, version handling and ioctl renumbering? */
.... /* whatever else useful, note it can be added later */
}
Then I would imagine a userspace daemon with arbitrary policies can be
written just fine.
Consider this pseudo-code which spawns 8 threads on domain 1 and
dispatches some number of them to zero stuff based on what it sees.
#define THREADS 8
int main(void)
{
bind_to_domain(1);
start_pre_zero_threads(THREADS); /* create a thread pool */
epoll_create();
struct hugepage_zero_configure hzc { .version = MAGIC_VERSION, .node = 1; minfree = SMALL_NUM; }
ioctl(hfd, HUGEPAGE_ZERO_CONFIGURE, &hzc);
hfd = open("/dev/hugepagectl", O_RDWR);
epoll_ctl();
for (;;) {
epoll_wait();
struct hugepage_zero_query hzq { .version = MAGIC_VERSION, .node = 1 };
ioctl(hfd, HUGEPAGE_ZERO_QUERY, &hzq);
if (hzq.free_huge_pages == 0)
/* nothing which can be done */
continue;
tozero = tune_the_request(&hzq);
/*
* get up to THREADS workers zeroing in parallel based on magic policy
*/
dispatch(tozero);
}
}
If one wants one daemon handling multiple domains it can use open the
file one time per domain to cover it.
Powered by blists - more mailing lists