From: Pasha Tatashin > Sent: 16 March 2024 19:18 ... > Expanding on Mathew's idea of an interface for dynamic kernel stack > sizes, here's what I'm thinking: > > - Kernel Threads: Create all kernel threads with a fully populated > THREAD_SIZE stack. (i.e. 16K) > - User Threads: Create all user threads with THREAD_SIZE kernel stack > but only the top page mapped. (i.e. 4K) > - In enter_from_user_mode(): Expand the thread stack to 16K by mapping > three additional pages from the per-CPU stack cache. This function is > called early in kernel entry points. > - exit_to_user_mode(): Unmap the extra three pages and return them to > the per-CPU cache. This function is called late in the kernel exit > path. Isn't that entirely horrid for TLB use and so will require a lot of IPI? Remember, if a thread sleeps in 'extra stack' and is then resheduled on a different cpu the extra pages get 'pumped' from one cpu to another. I also suspect a stack_probe() is likely to end up being a cache miss and also slow??? So you wouldn't want one on all calls. I'm not sure you'd want a conditional branch either. The explicit request for 'more stack' can be required to be allowed to sleep - removing a lot of issues. It would also be portable to all architectures. I'd also suspect that any thread that needs extra stack is likely to need to again. So while the memory could be recovered, I'd bet is isn't worth doing except under memory pressure. The call could also return 'no' - perhaps useful for (broken) code that insists on being recursive. David - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)