ntent-disposition:mime-version:references:message-id :subject:cc:to:from:date:sender:from:to:cc:subject:date:message-id :reply-to; bh=MrSUVTjoVk0VSnwpA0F2u4tk/xi0WuYZiS7D/oqHkuc=; b=DHqG6TDyvcjo5Txr3CYyYUa27LGHgzVFiOo/t6iOMgEf4OODA+uDcZiSc3ALql06mJ V+WPwOORvceL0EzaecQc0F2sCODH6qhocwtDvy2j6VD/x+tKStoxbfTq5NBaYnf3UL/Z XapW7pfEaB8V7Mc8YlPOYbJZcJhh+1hasmwvXQz4SZwNCUwMh/N4LDldjdCany8jZ0Pk RrJdevKCpjSnybYkMYAC0p3RNcA0QOJdtGgSBalkb68YXLCg5L77Ci901/KImtA2MiVL ISJSNnQqQa2f6tlVD/ky9SuE+ul9ibjmN+W+ckwO6wwN1F3e0gUokCUYtn5OwjzIBb/s CoBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717451932; x=1718056732; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:sender:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MrSUVTjoVk0VSnwpA0F2u4tk/xi0WuYZiS7D/oqHkuc=; b=p+n96Tv2OCTul2P2NBxuNv3UQyzzIztVFw3+RcjLHrWYW5+NxZajJOHftmN7TTY8/V EJIpQBp3l4qzbyydLehrSXRhW2vbPbe0prqOEyv9Lbgpp+M46Q/wf4VF/Xh31IlZwCaZ EGWd0/lPP+kpBm17DvCQluvGX5G1dJP5xkPaHiO4sKTOShr3MiAhovfm9LMswbvBdss6 06GuxPAYDRdCKdqSp+hBf76jKJqtR7rnZevXOy3xn0g2JyQHJ3fV38f3S+kUzOf3yjRB zcdzaqKqsbAnZcOeHcLmiQUYkrTiiVgXgERgVV6wasMr+N+ibkBTMsUc7Eekt/Y/dpO9 OdZg== X-Gm-Message-State: AOJu0YxosySfJW4ClBEm76dfEe2DtQ134/seeKj7fkMnEdOEe/1hD/hi nF0Jw8j6aHOg6++RVMV8M9wGMwFlKGvGAH/ZIkqo6r94/zQSoGLo X-Google-Smtp-Source: AGHT+IExfF+GSvYfGF6CMU7QZAeuzmjw7a/iu/iScpbELvZ1BeWqJuSu33IoSd7oD5s35ZCz9DS/AQ== X-Received: by 2002:a05:6808:2029:b0:3c8:61a9:629 with SMTP id 5614622812f47-3d1e34730cbmr12455256b6e.1.1717451931549; Mon, 03 Jun 2024 14:58:51 -0700 (PDT) Received: from framework ([208.72.125.4]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6af5056dd3esm20973706d6.96.2024.06.03.14.58.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Jun 2024 14:58:50 -0700 (PDT) Date: Mon, 3 Jun 2024 17:58:53 -0400 From: Mark Johnston To: Konstantin Belousov Cc: freebsd-arch@freebsd.org 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-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:15169, ipnet:2607:f8b0::/32, country:US] X-Rspamd-Queue-Id: 4VtSKn12mgz4wK7 On Tue, Jun 04, 2024 at 12:11:25AM +0300, 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, > which is not too stressed system. I remember that very long time ago > Peter ran tests with several hundreds of k threads, which is more realistic > high load, e.g. from typical java code (at least it was so several years > ago). Those threads are completely idle? > For kernel stack to be swapped, normally thread must sleep for at least > 10 secs. so a latency for next thread running moment should be not too > important. This isn't true in general. A daemon which responds to requests should do so with low latency even if it's been idle for a long time. If syslogd sleeps for 10 seconds and then receives a burst of messages, it should be scheduled as quickly as possible. > Having 1MB of essentially free memory is nice for system survival. > Being able to swap out pcb as well could be useful, IMO. There are many things we could do to shrink the kernel when under memory pressure. There is no pressure to shrink the buffer cache, or vnode or name caches, for instance. If we wanted to optimize the system in this direction, there is a lot of lower-hanging fruit to pick. I'm sure there are special cases where stack swapping might help in principle, but in practice it is far more common to see a small number of threads get swapped out, quickly followed by OOM kills. > > > > At the BSDCan devsummit I proposed removing support for kernel stack > > swapping and got only positive feedback. Does anyone here have any > > comments or objections?