Received: by 2002:ab2:2994:0:b0:1ef:ca3e:3cd5 with SMTP id n20csp155689lqb; Thu, 14 Mar 2024 07:55:21 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCV2VkeyHQaYTqHEfQwqe6gHvx96zfGudkiYia01g4ZD8nyLc6/U13I2p7voOiKMfxKLq/YDY5nw52EzBlgZmpzywmHpJQHa0XgjSUIZgw== X-Google-Smtp-Source: AGHT+IF9+fg+bzwFpNboZsBzA81llchZPhz/XtoAYcWxsUKS0zvJkQO0B0llJXacoilfjOSgLNm8 X-Received: by 2002:a05:6870:82a4:b0:221:9792:8448 with SMTP id q36-20020a05687082a400b0022197928448mr2391591oae.25.1710428120928; Thu, 14 Mar 2024 07:55:20 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710428120; cv=pass; d=google.com; s=arc-20160816; b=wS2KNcugEQEczTWyjnbwQnktH2hUUMTkZNWGsdAmD+xw4470d3vaJ2UN15wbS8chiJ jynUsYCygID6WvgiAx9Bp64xBCJu40I/7IcOYlu2kTyKYP0d6zzYVf4Nii8lPZuiGFrS yeY2jUo4yJoM1znZI2iq/7icDFba0DfXqwhRA8L8XJ46n24EJMSp5r9r3uiEjXMmwJTe dUAjfyWl0bBiX92xW4dARGuYX2pdWkLCCk9Ca7ljDBj+S/k+54mmvEsfftsFd9XKQptU Zbg7gLMivknu1MOsLNLP5HBMgbBK+a6rGuR0gLbq4MfZlBpGDLh4vTnYNXyiyt1hXCLn iMZA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:to:from :dkim-signature; bh=wW+2zHFZQjqfYIpLd4QibSf5RSIcFb17pxrKGVZfg7k=; fh=DfaX7VIaPWYB7iByWMYMCAjjP7Ro0zZ7hdgWpbUAhMA=; b=gnXs9FrN/MUhgjBJSiQJbpgDHm9FOpzkbwJX84krhbVDRndwGGe5C/tMXCa+t2CTQ+ CVcUFheeZD/BGzUc1ma6nQ6Jt5aTV+3qguO3/U/RAklxqIv8CaXLh2VMO4UJLeaHijhE zs3f5l0fmPA4bSs1WsqZDC/PPzxVBhwbinXq6P1AEPB4EJoMAAcFCPD1MPLKcClzJjBU 7LPcD0mBLfFUGEUQoU3gpY1dzPn0nEV3BmS4oayt3iwWdKOFabGuwTS4nc92PjX6OtFi 3TyNUEJhV5UX9a+EX3VxvRuzKiQKHqUFnqW8fsiXrJaBCCEycClcEske+Jum7VqeCbt8 RM7g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@soleen-com.20230601.gappssmtp.com header.s=20230601 header.b=vgTQzZaO; arc=pass (i=1 spf=pass spfdomain=soleen.com dkim=pass dkdomain=soleen-com.20230601.gappssmtp.com dmarc=pass fromdomain=soleen.com); spf=pass (google.com: domain of linux-kernel+bounces-103439-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-103439-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=soleen.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id h17-20020a632111000000b005dc4fbfb9acsi721721pgh.583.2024.03.14.07.55.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Mar 2024 07:55:20 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-103439-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@soleen-com.20230601.gappssmtp.com header.s=20230601 header.b=vgTQzZaO; arc=pass (i=1 spf=pass spfdomain=soleen.com dkim=pass dkdomain=soleen-com.20230601.gappssmtp.com dmarc=pass fromdomain=soleen.com); spf=pass (google.com: domain of linux-kernel+bounces-103439-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-103439-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=soleen.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 8DC9D2852AD for ; Thu, 14 Mar 2024 14:55:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3BCA271727; Thu, 14 Mar 2024 14:55:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=soleen-com.20230601.gappssmtp.com header.i=@soleen-com.20230601.gappssmtp.com header.b="vgTQzZaO" Received: from mail-qk1-f180.google.com (mail-qk1-f180.google.com [209.85.222.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 31BBC6FE11 for ; Thu, 14 Mar 2024 14:55:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.180 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710428113; cv=none; b=pgalm/zq8x5cHI8U8E70m6DI2gBV4jLT7GkiyUoOcuE3bZRJeb9s+R4wCOO7nWt39NOBswAvWVz1thDLLhfaeGnjQgBl0+pVJJRvKCP1DTDM3sKg08Q6TDSKkQ3LE3kd2lAMwcl9orWsAN74B1Ce1sig2EtlsJ6/x3jMc4wlBx0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710428113; c=relaxed/simple; bh=No3CcZI0R4w6avOsnxeH0/5MK3Se1i3ukG4F2P0PWMY=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=LWBAGV4UWiH3UnoHmlVMcvqbOEbbzVtDQ/89t30Prl3po0MdbsNWua1w0VQsccQxN8PZgEkbOr/MM2rRUXUr10m3KsQXdqe9lmz0lYE1Tt38dJwKW8ATcwR9Ucn1EB8xQjhEZIxZUPwfX5Y0Dz/+PW6Ve+wAj6s7CoJg4Mwfm4Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=soleen.com; spf=pass smtp.mailfrom=soleen.com; dkim=pass (2048-bit key) header.d=soleen-com.20230601.gappssmtp.com header.i=@soleen-com.20230601.gappssmtp.com header.b=vgTQzZaO; arc=none smtp.client-ip=209.85.222.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=soleen.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=soleen.com Received: by mail-qk1-f180.google.com with SMTP id af79cd13be357-788303f317eso48169885a.2 for ; Thu, 14 Mar 2024 07:55:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen-com.20230601.gappssmtp.com; s=20230601; t=1710428110; x=1711032910; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=wW+2zHFZQjqfYIpLd4QibSf5RSIcFb17pxrKGVZfg7k=; b=vgTQzZaO7QxySNh5iuvzWZw7klEAYZButavIHT3qd1Z71MEZaynC7F4ddmAESF+j7f 1rs4A57TtEFemy7CDRs3NLg5kv/IhNRspbEfVfrmzdfzUzv8vn7uorhAX3rNp/We3+aE A8B78+cRJPn9rVov88eWqHmQR2VGYm76OmcR4QHolr5gUflwUJI1v6mTGL1Ey056De0f VXAZSEC2yEol/ytOz0KEB0SgGnqqG18TTTOZJW0+Cr3u4LWDvcF0d3PVYfkOlKNSzmj2 zjPmlViQrvTXld2jgHJWGCjLUJlRorWTbkA4OZMBmNNS57ZYmbPoxn2d3wLmb2ncRvA1 8S/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710428110; x=1711032910; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=wW+2zHFZQjqfYIpLd4QibSf5RSIcFb17pxrKGVZfg7k=; b=RiIJj2RS2fmuqjmfX9S3zYWHx4D/7lODspaCbyF1uehAT8xbL7c1huBYK0XJmzUKEr VS3QOW5dpBvgv/In4hEEebyAjtignV4SVtmR/S/4qIwmT1UHdox7n6ABSNJcrzcyXE6d 8epkw8DP+fUB/6/VgMvnG4CUlTS/bppNA72p7T195KZVhcb1fmCJvVNP8r5/id00/AtN oGL8AB0xnOXdYyVo4b/6vPXYanhJPJOskk4wTO31VW2ql36rifGjwLrfBzcVRfZXj4a6 +Pd1hZYmUmUBPQLvuROCMMUNEWJsohAWAm0Cd5zOASLUfzK5KG/6zZh0I3hXHGcXBKNx Rkyg== X-Forwarded-Encrypted: i=1; AJvYcCUWvcXHPQcscMREKXRxpWaxXweFtrbbd2VHnnDgIZ0rVGQfI0ErJyVeCnOLMDye5TK5wm43MZdEOqqvZ3p+iFXTfwNS7Q4YwZKsidyI X-Gm-Message-State: AOJu0YyE/rlpevyMjhFJvKJoxb803Bi0fOY72dfeeJU6sdllhjo+bB9W ZTdvu0Zg9jVvICIDcS+UMnpz27h2912Ptktak7L/BZO7ltLoqUK51LjQeeIl8mE= X-Received: by 2002:a05:620a:2943:b0:789:e044:eb08 with SMTP id n3-20020a05620a294300b00789e044eb08mr739775qkp.24.1710428110047; Thu, 14 Mar 2024 07:55:10 -0700 (PDT) Received: from soleen.c.googlers.com (150.254.86.34.bc.googleusercontent.com. [34.86.254.150]) by smtp.gmail.com with ESMTPSA id pi20-20020a05620a379400b00788406f9c7dsm904821qkn.101.2024.03.14.07.55.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Mar 2024 07:55:09 -0700 (PDT) From: Pasha Tatashin To: akpm@linux-foundation.org, jpoimboe@kernel.org, pasha.tatashin@soleen.com, kent.overstreet@linux.dev, peterz@infradead.org, nphamcs@gmail.com, cerasuolodomenico@gmail.com, surenb@google.com, lizhijian@fujitsu.com, willy@infradead.org, shakeel.butt@linux.dev, vbabka@suse.cz, ziy@nvidia.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v2] vmstat: Keep count of the maximum page reached by the kernel stack Date: Thu, 14 Mar 2024 14:54:57 +0000 Message-ID: <20240314145457.1106299-1-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit CONFIG_DEBUG_STACK_USAGE provides a mechanism to determine the minimum amount of memory left in a stack. Every time a new low-memory record is reached, a message is printed to the console. However, this doesn't reveal how many pages within each stack were actually used. Introduce a mechanism that keeps count the number of times each of the stack's pages were reached: $ grep kstack /proc/vmstat kstack_page_1 19974 kstack_page_2 94 kstack_page_3 0 kstack_page_4 0 In the above example, out of 20,068 threads that exited on this machine, only 94 reached the second page of their stack, and none touched pages three or four. In fleet environments with millions of machines, this data can help optimize kernel stack sizes. Signed-off-by: Pasha Tatashin --- Changelog: v2: - Fixed enum name KSTACK_PAGE_5 ->KSTACK_PAGE_REST. - Improved commit message based on Christophe Leroy comment. include/linux/sched/task_stack.h | 40 ++++++++++++++++++++++++++++++-- include/linux/vm_event_item.h | 29 +++++++++++++++++++++++ include/linux/vmstat.h | 16 ------------- mm/vmstat.c | 11 +++++++++ 4 files changed, 78 insertions(+), 18 deletions(-) diff --git a/include/linux/sched/task_stack.h b/include/linux/sched/task_stack.h index ccd72b978e1f..09e6874c2ced 100644 --- a/include/linux/sched/task_stack.h +++ b/include/linux/sched/task_stack.h @@ -95,9 +95,42 @@ static inline int object_is_on_stack(const void *obj) extern void thread_stack_cache_init(void); #ifdef CONFIG_DEBUG_STACK_USAGE +#ifdef CONFIG_VM_EVENT_COUNTERS +#include + +/* Count the maximum pages reached in kernel stacks */ +static inline void count_kstack_page(int stack_max_page) +{ + switch (stack_max_page) { + case 1: + this_cpu_inc(vm_event_states.event[KSTACK_PAGE_1]); + break; + case 2: + this_cpu_inc(vm_event_states.event[KSTACK_PAGE_2]); + break; +#if THREAD_SIZE >= (4 * PAGE_SIZE) + case 3: + this_cpu_inc(vm_event_states.event[KSTACK_PAGE_3]); + break; + case 4: + this_cpu_inc(vm_event_states.event[KSTACK_PAGE_4]); + break; +#endif +#if THREAD_SIZE > (4 * PAGE_SIZE) + default: + this_cpu_inc(vm_event_states.event[KSTACK_PAGE_REST]); + break; +#endif + } +} +#else /* !CONFIG_VM_EVENT_COUNTERS */ +static inline void count_kstack_page(int stack_max_page) {} +#endif /* CONFIG_VM_EVENT_COUNTERS */ + static inline unsigned long stack_not_used(struct task_struct *p) { unsigned long *n = end_of_stack(p); + unsigned long unused_stack; do { /* Skip over canary */ # ifdef CONFIG_STACK_GROWSUP @@ -108,10 +141,13 @@ static inline unsigned long stack_not_used(struct task_struct *p) } while (!*n); # ifdef CONFIG_STACK_GROWSUP - return (unsigned long)end_of_stack(p) - (unsigned long)n; + unused_stack = (unsigned long)end_of_stack(p) - (unsigned long)n; # else - return (unsigned long)n - (unsigned long)end_of_stack(p); + unused_stack = (unsigned long)n - (unsigned long)end_of_stack(p); # endif + count_kstack_page(((THREAD_SIZE - unused_stack) >> PAGE_SHIFT) + 1); + + return unused_stack; } #endif extern void set_task_stack_end_magic(struct task_struct *tsk); diff --git a/include/linux/vm_event_item.h b/include/linux/vm_event_item.h index 747943bc8cc2..1dbfe47ff048 100644 --- a/include/linux/vm_event_item.h +++ b/include/linux/vm_event_item.h @@ -153,10 +153,39 @@ enum vm_event_item { PGPGIN, PGPGOUT, PSWPIN, PSWPOUT, VMA_LOCK_ABORT, VMA_LOCK_RETRY, VMA_LOCK_MISS, +#endif +#ifdef CONFIG_DEBUG_STACK_USAGE + KSTACK_PAGE_1, + KSTACK_PAGE_2, +#if THREAD_SIZE >= (4 * PAGE_SIZE) + KSTACK_PAGE_3, + KSTACK_PAGE_4, +#endif +#if THREAD_SIZE > (4 * PAGE_SIZE) + KSTACK_PAGE_REST, +#endif #endif NR_VM_EVENT_ITEMS }; +#ifdef CONFIG_VM_EVENT_COUNTERS +/* + * Light weight per cpu counter implementation. + * + * Counters should only be incremented and no critical kernel component + * should rely on the counter values. + * + * Counters are handled completely inline. On many platforms the code + * generated will simply be the increment of a global address. + */ + +struct vm_event_state { + unsigned long event[NR_VM_EVENT_ITEMS]; +}; + +DECLARE_PER_CPU(struct vm_event_state, vm_event_states); +#endif + #ifndef CONFIG_TRANSPARENT_HUGEPAGE #define THP_FILE_ALLOC ({ BUILD_BUG(); 0; }) #define THP_FILE_FALLBACK ({ BUILD_BUG(); 0; }) diff --git a/include/linux/vmstat.h b/include/linux/vmstat.h index 343906a98d6e..18d4a97d3afd 100644 --- a/include/linux/vmstat.h +++ b/include/linux/vmstat.h @@ -41,22 +41,6 @@ enum writeback_stat_item { }; #ifdef CONFIG_VM_EVENT_COUNTERS -/* - * Light weight per cpu counter implementation. - * - * Counters should only be incremented and no critical kernel component - * should rely on the counter values. - * - * Counters are handled completely inline. On many platforms the code - * generated will simply be the increment of a global address. - */ - -struct vm_event_state { - unsigned long event[NR_VM_EVENT_ITEMS]; -}; - -DECLARE_PER_CPU(struct vm_event_state, vm_event_states); - /* * vm counters are allowed to be racy. Use raw_cpu_ops to avoid the * local_irq_disable overhead. diff --git a/mm/vmstat.c b/mm/vmstat.c index db79935e4a54..737c85689251 100644 --- a/mm/vmstat.c +++ b/mm/vmstat.c @@ -1413,6 +1413,17 @@ const char * const vmstat_text[] = { "vma_lock_retry", "vma_lock_miss", #endif +#ifdef CONFIG_DEBUG_STACK_USAGE + "kstack_page_1", + "kstack_page_2", +#if THREAD_SIZE >= (4 * PAGE_SIZE) + "kstack_page_3", + "kstack_page_4", +#endif +#if THREAD_SIZE > (4 * PAGE_SIZE) + "kstack_page_rest", +#endif +#endif #endif /* CONFIG_VM_EVENT_COUNTERS || CONFIG_MEMCG */ }; #endif /* CONFIG_PROC_FS || CONFIG_SYSFS || CONFIG_NUMA || CONFIG_MEMCG */ -- 2.44.0.278.ge034bb2e1d-goog