From nobody Mon Jun 03 21:58:13 2024 X-Original-To: freebsd-arch@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4VtSK92hy3z5LLxt for ; Mon, 03 Jun 2024 21:58:21 +0000 (UTC) (envelope-from kostikbel@gmail.com) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4VtSK85Fl5z4vyZ; Mon, 3 Jun 2024 21:58:20 +0000 (UTC) (envelope-from kostikbel@gmail.com) Authentication-Results: mx1.freebsd.org; none Received: from tom.home (kib@localhost [127.0.0.1] (may be forged)) by kib.kiev.ua (8.18.1/8.18.1) with ESMTP id 453LwD03086468; Tue, 4 Jun 2024 00:58:17 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.10.3 kib.kiev.ua 453LwD03086468 Received: (from kostik@localhost) by tom.home (8.18.1/8.18.1/Submit) id 453LwDkN086467; Tue, 4 Jun 2024 00:58:13 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Tue, 4 Jun 2024 00:58:13 +0300 From: Konstantin Belousov To: Jessica Clarke Cc: Mark Johnston , freebsd-arch Subject: Re: removing support for kernel stack swapping Message-ID: References: List-Id: Discussion related to FreeBSD architecture List-Archive: https://lists.freebsd.org/archives/freebsd-arch List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-arch@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-1.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FORGED_GMAIL_RCVD,FREEMAIL_FROM, NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on tom.home X-Spamd-Bar: ---- X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; ASN(0.00)[asn:6939, ipnet:2001:470::/32, country:US] X-Rspamd-Queue-Id: 4VtSK85Fl5z4vyZ On Mon, Jun 03, 2024 at 10:41:22PM +0100, Jessica Clarke wrote: > On 3 Jun 2024, at 22:39, Konstantin Belousov wrote: > > > > On Mon, Jun 03, 2024 at 10:15:15PM +0100, Jessica Clarke wrote: > >> On 3 Jun 2024, at 22:11, Konstantin Belousov wrote: > >>> > >>> On Sun, Jun 02, 2024 at 07:57:04PM -0400, Mark Johnston wrote: > >>>> FreeBSD will, when free pages are scarce, try to swap out the kernel > >>>> stacks (typically 16KB per thread) of sleeping user threads. I'm told > >>>> that this mechanism was first implemented in BSD for the VAX port and > >>>> that stabilizing it was quite an endeavour. > >>>> > >>>> This feature has wide-ranging implications for code in the kernel. For > >>>> instance, if a thread allocates a structure on its stack, links it into > >>>> some data structure visible to other threads, and goes to sleep, it must > >>>> use PHOLD to ensure that the stack doesn't get swapped out while > >>>> sleeping. A missing PHOLD can thus result in a kernel panic, but this > >>>> kind of mistake is very easy to make and hard to catch without thorough > >>>> stress testing. The kernel stack allocator also requires a fair bit of > >>>> code to implement this feature, and we've had multiple bugs in that > >>>> area, especially in relation to NUMA support. Moreover, this feature > >>>> will leave threads swapped out after the system has recovered, resulting > >>>> in high scheduling latency once they're ready to run again. > >>>> > >>>> In a very stressed system, it's possible that we can free up something > >>>> like 1MB of RAM using this mechanism. I argue that this mechanism is > >>>> not worth it on modern systems: it isn't going to make the difference > >>>> between a graceful recovery from memory pressure and a catatonic state > >>>> which forces a reboot. The complexity and resulting bugs it induces is > >>>> not worth it. > >>> On amd64, 1MB of physical memory for stacks is consumed by 64k threads, > >> > >> To avoid any confusion, you mean 64 kthreads here, right? At least that > >> makes sense for the story and the maths. > > I mean 65535 threads (each of which must have kernel stack). > > At 16 KiB each that would be 1 GiB total, not 1 MiB? Indeed my math was incorrect, 1MB is consumed by 64 threads. 64k threads eat 1GB for stacks today.