From patchwork Mon Mar 11 16:46:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pasha Tatashin X-Patchwork-Id: 13589089 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id BD249C5475B for ; Mon, 11 Mar 2024 16:47:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5473D6B00DA; Mon, 11 Mar 2024 12:47:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4F5756B00DB; Mon, 11 Mar 2024 12:47:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2D34D6B00DC; Mon, 11 Mar 2024 12:47:00 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 199B66B00DA for ; Mon, 11 Mar 2024 12:47:00 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id CE5591A0C00 for ; Mon, 11 Mar 2024 16:46:59 +0000 (UTC) X-FDA: 81885337758.14.83B7AE6 Received: from mail-qk1-f171.google.com (mail-qk1-f171.google.com [209.85.222.171]) by imf17.hostedemail.com (Postfix) with ESMTP id 0854C40007 for ; Mon, 11 Mar 2024 16:46:57 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=soleen-com.20230601.gappssmtp.com header.s=20230601 header.b=DUoR+5Wu; spf=pass (imf17.hostedemail.com: domain of pasha.tatashin@soleen.com designates 209.85.222.171 as permitted sender) smtp.mailfrom=pasha.tatashin@soleen.com; dmarc=pass (policy=none) header.from=soleen.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1710175618; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=q6FpZ49BpunKdsFtEl370XBCgostMe6YS8Mf7VMqlwM=; b=7wRGdQKgCsFpYRcQRsutD13eIjTlhjXgIBE5a/EWv1wsRp6hnHwSjx++w8uKT/qczeMXko NgrcJBbAtYhoe1rHoxCOk/WJqATG6bqC9q7FxiXC9jIOVf16B0PpfeNvvZ3X6lszYSWiz/ NDkHmIpWiGwUAfGwF/bf0/NpVJQiaQE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710175618; a=rsa-sha256; cv=none; b=V77AlYoAxiBsH51wEBdVnsjip/Vse6smx5+a3dk4LIz2Tt+9lUv2zFTsITKSfjLcY5LLXP hWtHXYqqmox5rGbisRA5LvIyfeovBz27QLQvJ4q6al6vK/PUHWDwuyz5zkb8TumuZGsV1E m2idN/jIu4y5752cS+/KT1DzQEtxqp0= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=soleen-com.20230601.gappssmtp.com header.s=20230601 header.b=DUoR+5Wu; spf=pass (imf17.hostedemail.com: domain of pasha.tatashin@soleen.com designates 209.85.222.171 as permitted sender) smtp.mailfrom=pasha.tatashin@soleen.com; dmarc=pass (policy=none) header.from=soleen.com Received: by mail-qk1-f171.google.com with SMTP id af79cd13be357-7884a9a47a5so122047685a.2 for ; Mon, 11 Mar 2024 09:46:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen-com.20230601.gappssmtp.com; s=20230601; t=1710175617; x=1710780417; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=q6FpZ49BpunKdsFtEl370XBCgostMe6YS8Mf7VMqlwM=; b=DUoR+5WuqfpiAeuoIg+PMvE8hq7GJTKv7U0E2zh+qgw9eIC7A2Pq8iSe1g/xGzogXh dWBBptybEXLZcEFIDRLB04Ll/Zjk6PNRRqk7727GS3SjA5DNyE4fCJFxIkwzO9Xvieaf 1/IHOt+m/ngNQJ1oSVocibvU2J478hSmmJ1JHzqVMleAlhi/oCQd3ELdJonY93VC0bq9 GYveYV8paNWAwUY5IWNJGeZXY4kJR2Es1iLSQ3bbNbYI/VH6/oXG8le5mVDyRj8fo91f w3w2mmgM23dBZg5E2GylvPomKg9Zh9Lno1WuXagxjdsb9CHxLTXKz83ZgHsImH6hPrkm xTiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710175617; x=1710780417; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=q6FpZ49BpunKdsFtEl370XBCgostMe6YS8Mf7VMqlwM=; b=no7sRJmW39DmAA0PnwjZ+LOxrFF+W3D/2OWvE12HR00BLkwmuv0vtiTCNA5StA1p+E 3Krv+iwSClKVllyF45J4ljO5zjtvQLCYt8humlfjFA+VaovN2b2Yn3RDn7YfOMvMATc7 gqreYv9ziKfSZQMh30NPnZl+JUxutj9as/eHV6xFKTd2SMsDErPWnU7AspDGiaN79euJ ixRSa84/792ZT/+JR0WvoMNu5Y0SXg/AhXz4VaF2CRolXI+lLflEdnMXb01f6Jg4PjDq OHBFe/hoQ7miN+XuauOJ0J4g63A1BYcvQ71RmDGzh881Dz+2l1q7ICsZ7cFmu4PC2ftj MZlQ== X-Forwarded-Encrypted: i=1; AJvYcCU97nF9M0Omay1vxsQ7sT2Dlqj4b/4tCI0EGa4wntdsByYaLUWu0RghZflqi2PydeTu6Da1PQ4464AfwwQr244pojY= X-Gm-Message-State: AOJu0YxBjbepH6tC3dwReAHykDd6nr1uimceNYQVztOplUbPvdheNiX3 ZxD+UIInMYIUPhoqcAwr9hn9mQBZAmt4YXRqZIKxyMvIO/fXGFNlULhu3/vIUqU= X-Google-Smtp-Source: AGHT+IGBBQHGTyj0HK6a6nT9QQX2CzgLifOnSoon3yUl5MxZhI+SOhyqTpiNJHmFfzsFPzFEmk6ojA== X-Received: by 2002:ae9:e701:0:b0:788:2e8a:1731 with SMTP id m1-20020ae9e701000000b007882e8a1731mr6728530qka.17.1710175617252; Mon, 11 Mar 2024 09:46:57 -0700 (PDT) Received: from soleen.c.googlers.com.com (150.254.86.34.bc.googleusercontent.com. [34.86.254.150]) by smtp.gmail.com with ESMTPSA id d27-20020a05620a137b00b00788228fbe05sm2851589qkl.17.2024.03.11.09.46.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Mar 2024 09:46:56 -0700 (PDT) From: Pasha Tatashin To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, x86@kernel.org, bp@alien8.de, brauner@kernel.org, bristot@redhat.com, bsegall@google.com, dave.hansen@linux.intel.com, dianders@chromium.org, dietmar.eggemann@arm.com, eric.devolder@oracle.com, hca@linux.ibm.com, hch@infradead.org, hpa@zytor.com, jacob.jun.pan@linux.intel.com, jgg@ziepe.ca, jpoimboe@kernel.org, jroedel@suse.de, juri.lelli@redhat.com, kent.overstreet@linux.dev, kinseyho@google.com, kirill.shutemov@linux.intel.com, lstoakes@gmail.com, luto@kernel.org, mgorman@suse.de, mic@digikod.net, michael.christie@oracle.com, mingo@redhat.com, mjguzik@gmail.com, mst@redhat.com, npiggin@gmail.com, peterz@infradead.org, pmladek@suse.com, rick.p.edgecombe@intel.com, rostedt@goodmis.org, surenb@google.com, tglx@linutronix.de, urezki@gmail.com, vincent.guittot@linaro.org, vschneid@redhat.com, pasha.tatashin@soleen.com Subject: [RFC 14/14] fork: Dynamic Kernel Stack accounting Date: Mon, 11 Mar 2024 16:46:38 +0000 Message-ID: <20240311164638.2015063-15-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog In-Reply-To: <20240311164638.2015063-1-pasha.tatashin@soleen.com> References: <20240311164638.2015063-1-pasha.tatashin@soleen.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 0854C40007 X-Rspam-User: X-Stat-Signature: 97hk11qyaahkm3qs9riyhiadt5r3ir9d X-Rspamd-Server: rspam03 X-HE-Tag: 1710175617-381031 X-HE-Meta: U2FsdGVkX1+T3PWynvk8NYiDkYkcDDNobitU6b5iiwRcOT9W1e46FKLEWkUwhriSXGOg392UQ9fBbKweLy5rvDQQ3CLk9PjXx1gfOabZ3pAG04N2HpQ9y4MP1zUGeRcNBHQ498pVJZ+whbinJA29g0M5o+d4xV1TyPMs6r+c5MuT0eNAYQkDtRBPxgAXX/jzYAoJPxvuHQ97pXq+LHFbtV9iqBbPkR/C6RV0xl09VcGw+3d6gbToEMcU6BoE6oCt5sRpAvqZB37LMpFUeBSXm/8f0c9S4ZtklPrhFYCV1BdlFs0fbphzSdHVB+OaOtmjGfeBo95GNpmygaR6x6wYecdN8tKQXmbbirjLNnePHjhFHQs6frd9d56nXNpHG49MiAiHGHTYd1RWfz3S3ZD+6ViBiT5iODeyiHcgot7mBgPTYFoEzR+DvGLMFJCLVgrU5fRtDHYI7xtrkSnIziuHP1CGUUoNAThknjEv5eVIAT8slnhcC7EWZzHaQWfNTAlY52wrf8pLheoDIF5vrvme5HvkH40qCBZxR1s2v0i/4SY+nSdN5m7JKpwZEaX5xnHtPVcFOGALTe5azqYxejo0qzJgfqAOfVpIcx6I/tKLfJOw0QmAuxKW3+r7kvR2Frr4bOeK05BMnox//Ut0Zd3EgPfukJgbdj10RPlcjrwxlMM6C6QGHbR+U1Us71dp7lhOtJnSKwRQGyBCr+RZzr+5ZSliwI+1PWIYvqLxJP474Pegk/KnH1j7TOPVsJMEJXNDxQdE7sHcsXiWxReihQ6JmksofP7FrJMR6a75H5v0JD7QltIX3xnPWiSoYxayhFwTwEHCyKCPl5OYH+JROs16TcxtDNFCiuogvwZPiBt5Mfte40WRSbN/nIFgbroycYMIvr1ZKMYeSYWgKzPZJ/nf/6goISV3kQReF+JMT6c6byI8/vNNaO9gX/0J+m2kpvZFDgGmGlYu9iRzZPoyN5m AQbRMAiL AWLNzQsvT4eL8KXWxIIHmUe6k0TrjitHdFY320OjxMbTXwJNFlYCYi7CHGk9eH17QpNbzS4f/AY5LeVHwzebW/q02gvmbKVlkWc2+3nHDfbn9KqKxt2Wgvj8WDczfZH0UhUokgCeHOZPJhOGZD1m12P1IX8ikKjkviTGEmZrNzJahwqYtuz/LyhntholWf+EJwTwNlFIDwYlSlOtAaysBPO42x3FwIfBqVjmu5Jvt3r5c0bX2XDRibAj4T6bUooG1Eka0kfqMB2rqujgS3rBpqN1421amv4LkuVpn45VD7CLrKfQgtDy0k5Zql91DuYWa0f7F+cWHZCE0z89FTm4qrHmRKuTYdhm6NOono36fUgGjk1w5JDy18S/qPuu6Y5DJIdX9K6A5o5Z5Tty2LkW8BHtlEarVJ6Ikep/zzRax1auRsS8vqd07V08KUIxXtSN9Lmp07IZuBrDN0io= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Add an accounting of amount of stack pages that has been faulted is currently in use. Example use case: $ cat /proc/vmstat | grep stack nr_kernel_stack 18684 nr_dynamic_stacks_faults 156 The above shows that the kernel stacks use total 18684KiB, out of which 156KiB were faulted in. Given that the pre-allocated stacks are 4KiB, we can determine the total number of tasks: tasks = (nr_kernel_stack - nr_dynamic_stacks_faults) / 4 = 4632. The amount of kernel stack memory without dynamic stack on this machine woud be: 4632 * 16 KiB = 74,112 KiB Therefore, in this example dynamic stacks save: 55,428 KiB Signed-off-by: Pasha Tatashin --- include/linux/mmzone.h | 3 +++ kernel/fork.c | 13 ++++++++++++- mm/vmstat.c | 3 +++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index a497f189d988..ba4f1d148c3f 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -198,6 +198,9 @@ enum node_stat_item { NR_FOLL_PIN_ACQUIRED, /* via: pin_user_page(), gup flag: FOLL_PIN */ NR_FOLL_PIN_RELEASED, /* pages returned via unpin_user_page() */ NR_KERNEL_STACK_KB, /* measured in KiB */ +#ifdef CONFIG_DYNAMIC_STACK + NR_DYNAMIC_STACKS_FAULTS_KB, /* KiB of faulted kernel stack memory */ +#endif #if IS_ENABLED(CONFIG_SHADOW_CALL_STACK) NR_KERNEL_SCS_KB, /* measured in KiB */ #endif diff --git a/kernel/fork.c b/kernel/fork.c index 63e1fd661e17..2520583d160a 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -343,6 +343,9 @@ void dynamic_stack_refill_pages(void) mod_lruvec_page_state(page, NR_KERNEL_STACK_KB, PAGE_SIZE / 1024); + mod_lruvec_page_state(page, + NR_DYNAMIC_STACKS_FAULTS_KB, + PAGE_SIZE / 1024); page = alloc_pages(THREADINFO_GFP & ~__GFP_ACCOUNT, 0); if (unlikely(!page)) @@ -771,9 +774,17 @@ static void account_kernel_stack(struct task_struct *tsk, int account) int i, nr_pages; nr_pages = vm->nr_pages; - for (i = 0; i < nr_pages; i++) + for (i = 0; i < nr_pages; i++) { mod_lruvec_page_state(vm->pages[i], NR_KERNEL_STACK_KB, account * (PAGE_SIZE / 1024)); +#ifdef CONFIG_DYNAMIC_STACK + if (i >= THREAD_PREALLOC_PAGES) { + mod_lruvec_page_state(vm->pages[i], + NR_DYNAMIC_STACKS_FAULTS_KB, + account * (PAGE_SIZE / 1024)); + } +#endif + } } else { void *stack = task_stack_page(tsk); diff --git a/mm/vmstat.c b/mm/vmstat.c index db79935e4a54..1ad6eede3d85 100644 --- a/mm/vmstat.c +++ b/mm/vmstat.c @@ -1237,6 +1237,9 @@ const char * const vmstat_text[] = { "nr_foll_pin_acquired", "nr_foll_pin_released", "nr_kernel_stack", +#ifdef CONFIG_DYNAMIC_STACK + "nr_dynamic_stacks_faults", +#endif #if IS_ENABLED(CONFIG_SHADOW_CALL_STACK) "nr_shadow_call_stack", #endif