Re: [PATCH v2 00/13] Dynamic Kernel Stacks

Pasha Tatashin
2026-04-24T23:06:24Z

On 04-24 23:26, David Laight wrote:
> On Fri, 24 Apr 2026 21:35:20 +0000
> Pasha Tatashin <pasha.tatashin@soleen.com> wrote:
>
> > On 04-24 12:41, Dave Hansen wrote:
> > > On 4/24/26 12:14, David Stevens wrote:
> > > > The question is then: is this approach something that is fundamentally
> > > > untenable in the kernel
> > >
> > > Yes. Fundamentally untenable.
> > >
> > > Not allowing stack faults has been a wonderful simplification. It's one
> > > of those things that just plain makes the kernel easier to maintain.
> > > Saving low single digits of system memory is not exactly making me eager
> > > to go back to the harder-to-maintain days.
> > >
> > > I seriously doubt that this 1% is the lowest hanging fruit for memory
> > > bloat on these systems. ;)
> >
> > This true until, in a fleet of millions of machines, you encounter a
> > one-in-a-billion chance of a stack overflow. You are then forced to
> > double the statically allocated kernel stacks on every machine, paying a
> > memory tax even though 99.999..% of threads never exceed 4K. This
> > overhead accumulates to petabytes of wasted capacity.
>
> And then you hit a stack fault in some path where you can't sleep and
> there isn't any available kernel memory.

Well, at least if we hit this rare case, we can simply double a buffer
of pre-reserved stack memory per CPU. This still saves significant
memory compared to wasting it on every single thread.

> An alternative idea is to arrange for some system calls to sleep in
> userspace, so when the thread is woken it re-executes the system call.
> It then makes sense to assign the kernel stack to the process when
> it enters the kernel.
> That might mean that you don't need a kernel stack for all the threads
> sleeping in futex() - it might even be possible to do the retry in
> userspace saving the second kernel entry most of the time.
> It is all 'hard and difficult' though.

I was thinking about a similar approach as well—sort of multiplexing the
kernel stacks. But honestly, when trying to cover all the edge cases, I
didn't find it to be any better or easier than just using dynamic kernel
stacks.

An alternative approach, which was proposed at LSFMM by Willy, is to add
an explicit deep stack calls. When we enter a path that we know is
exceptionally deep, only then do we extend the stack, keeping the
default (say, 8K) everywhere else.

> The easier solution is to rewrite the system code so it doesn't have
> 1000s of threads :-)

That ship sailed in the early 90s of the previous millennium. Nowadays,
we have high end workstations with almost 200 hardware threads.
Rewriting system code to reduce thread counts simply isn't an option for
our storage machines, which have millions of threads per unit.

+CC Matthew Wilcox