From patchwork Wed Mar 13 03:34:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pasha Tatashin X-Patchwork-Id: 13590943 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 4030BC54E58 for ; Wed, 13 Mar 2024 03:34:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CDFBB94000B; Tue, 12 Mar 2024 23:34:33 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C8F85940009; Tue, 12 Mar 2024 23:34:33 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B587494000B; Tue, 12 Mar 2024 23:34:33 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id A35EF940009 for ; Tue, 12 Mar 2024 23:34:33 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 7A5B4A1702 for ; Wed, 13 Mar 2024 03:34:33 +0000 (UTC) X-FDA: 81890598426.16.CCFB13C Received: from mail-qk1-f182.google.com (mail-qk1-f182.google.com [209.85.222.182]) by imf14.hostedemail.com (Postfix) with ESMTP id CFAC9100005 for ; Wed, 13 Mar 2024 03:34:30 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=soleen-com.20230601.gappssmtp.com header.s=20230601 header.b="ZRoW/Jba"; spf=pass (imf14.hostedemail.com: domain of pasha.tatashin@soleen.com designates 209.85.222.182 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=1710300871; 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: references:dkim-signature; bh=Sli3mfWx0lYFMlw598vEsdEvssG10grtOqJ1aTNbNWY=; b=dPeFMSqMl0R9Jcjo87ig2C52M99vnHieNIupsEC90pqcPh5hBsjEpd5sad670xP+8F+Uux eazUK3Mhb1Qpk5zl8B6l+gM/Wj8wqGZUAlzlP+J6YDFmL25wbRfhImifLoGX5FPQtB4BLR Gbny23eHuVXTxwQiEdcrz6FuIUIyjc0= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=soleen-com.20230601.gappssmtp.com header.s=20230601 header.b="ZRoW/Jba"; spf=pass (imf14.hostedemail.com: domain of pasha.tatashin@soleen.com designates 209.85.222.182 as permitted sender) smtp.mailfrom=pasha.tatashin@soleen.com; dmarc=pass (policy=none) header.from=soleen.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1710300871; a=rsa-sha256; cv=none; b=FRPtitvSJiYv16SY4fbs962jIjYJxKYQkBZ5P0zNfw9d0SzHqcXeOCBAay9uYf+gq6artn c6ri5+laEOYswgHXYcCRrNbJGwO3xdSFffqwQ1U1RuDAe7lzjM+lfNMH/VdHk93XSMBeVT yT+eOoqNe1GOpDHfo+mT/b/sXHTUdrI= Received: by mail-qk1-f182.google.com with SMTP id af79cd13be357-7884124b06aso19906685a.2 for ; Tue, 12 Mar 2024 20:34:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=soleen-com.20230601.gappssmtp.com; s=20230601; t=1710300870; x=1710905670; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=Sli3mfWx0lYFMlw598vEsdEvssG10grtOqJ1aTNbNWY=; b=ZRoW/JbaX99bBGGRG969tXXsYCwQuVa/F3J/fEpInhYLpn3yrWJwCeZ9puTBP6+92h HcRhLnhifH/syrVjSMPebOFx/QJfKD9hhtdxl6Gp3o+YUqtRzxEax3oArWCSrhpFecay zV/HcYOC9ff7CoyyXSgO6mWUsbsaw8OLsayIjSR25tpB2AtAicwocphpKp++qN2d+577 BnWdZdUlkop0PwkH1+nFDkXgNmDrA8IleQIDOGu/eZCFLBy0gAmBotin0s07JsUU6i0s JkmJvX4gXU7st8g3hnOyC0wP6b3zhn+fqZlVmhrSq59QIv3v6FLjAqyJZz8tta8jYazQ SWYg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710300870; x=1710905670; 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=Sli3mfWx0lYFMlw598vEsdEvssG10grtOqJ1aTNbNWY=; b=QyMgRo0UGfOizlrsBVQp0qqCvjoJX2yX6j/+8isQFF1qSKTWEjOn2nWfyO7kybPVgZ 4f5d1Ay6QdNDwbzApI85xXwjydMD3shsYP1BU16BSdxUPaRkM18SzfKD7B4LzeBeSeN2 yq4gxPFqLqGN/jfhW59SglB7g71BBaCidP8SHpJDCZU7O2YgdYtcJAAqD+cgTh3/+mmD /0Z7LN058WAInupWzWxHyvX35HYsTKDcr7ER7WwqgpcLWb96Xgs7/r7ThiaEDbudqV0g gkrvD13VixfDamNiC5pxXKr2iTC3kZqpD6+ZbotCjNo1M7ukExNBnBEuxKNk3deB9TPn JLSA== X-Forwarded-Encrypted: i=1; AJvYcCXLqAfI1g2yel0t4pXXyfdZpKVCbmLsfi69k20W1l6J/OrQKLju3Hl2fH9wXwCJi7Ou/zo6ohP0mHPt3Mh6UYwjG50= X-Gm-Message-State: AOJu0Yxw46CF3mAgd10Z+kDe3FBATdi/YRwcvBg/lejZsWo3IkXz/HpT FjbnNvQ+suGk5HKKQ64SEh/Y4Be2geIwD6MrQZTpV45vKMlwQK93racfRYIvbZQ= X-Google-Smtp-Source: AGHT+IGSuWTJqFlidzOR25k5TK28N3coug262snCXpyJ/gX21nMvv8zZXtrjObRgQD1ep9GW89F9FQ== X-Received: by 2002:a05:620a:8dc:b0:789:c83d:c5f5 with SMTP id z28-20020a05620a08dc00b00789c83dc5f5mr973105qkz.20.1710300869839; Tue, 12 Mar 2024 20:34:29 -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 bs44-20020a05620a472c00b00787fed5c768sm4295509qkb.72.2024.03.12.20.34.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Mar 2024 20:34:29 -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] vmstat: Keep count of the maximum page reached by the kernel stack Date: Wed, 13 Mar 2024 03:34:17 +0000 Message-ID: <20240313033417.447216-1-pasha.tatashin@soleen.com> X-Mailer: git-send-email 2.44.0.278.ge034bb2e1d-goog MIME-Version: 1.0 X-Rspamd-Queue-Id: CFAC9100005 X-Rspam-User: X-Stat-Signature: km8ufm3hwwdh4tfy8zrm9hc7jb9ji8y9 X-Rspamd-Server: rspam01 X-HE-Tag: 1710300870-34016 X-HE-Meta: U2FsdGVkX197xC4WeT1fpa/7jwLKrUHerCqSwE5JSQV5GYwW5MI2K+DW0m0/fChuyxch2I7isysyLZz4ui8XgxKN9Izz1zItTVPd0OzazOHkdGolxZjkbKKkAUpQj/k0tLjiTVqCWdo3F1Fw2zvFcu2LXXV75JVm8EbSSCTBKSg2ulyA8Cu+yEw0S4AafjN4lauGB2mW9y70ZXS8OLvN/s3Q4j4CirNlCQXpp1kedAP9vbMm64vOiY5K0b7Vfua+eDWpqKaLMVprcZKt1sPi9p/vvmpjRS+VbToltggaB0BU6ONZyNegKl7r9yaI8Lg7+4CPgxO7sI70+ufnu1dMLNJrNElGFmYy06ysy1uHaqGHPbral5NMOABOh7H1D0kd6K7c/dGbOh3fRRFisQ4FyGCfQwzziP7basc4CfVdYheouKd+hLiqE4nBL/UvoNHxIfUmqLZjrWagl5nCwJiq5lD51eHxKVzMxEUoiOuaKXZN8MUgQ518g8oN6vGIk7kfmlhsHggolUmpLYRF9moCtcfo1n0N7v8ZhTPsw8t+5XIJ++S1J7U9iHHN+cVWEGNogQtZ6xuIqzBKXVfU8lTWfdnkYiFBnXQwLAWkUDbzXaYolsoSp2G/FMlCPDb78XW3/ks4AC7uQ1YZE0N6P8Qy0O5qx6kJD8ugZl/AYmt3FTCuaAeX9H5LzSiizBbj0F4pTquv55vq3gWBUNAQMi8YLJou7EwXoJReX50ZlPUWY1hHCv8eneSbWvuVpmiN7MDFmgWJgWGRJSKnOirLhu6lx6Xcxep+i0fWnw/d6pTuT2JQaalRF951Nj9TepmGICv0fH9xR0HD8tzaO8boXw5k5+TDi+dfwoV3ifMetZLVq1hWt7VJED+sIg4iV2PgmJo/7SZTuTcY4rfMzYiH2X+ypYisiSQo4JCaMDczQxWmPW5qwiTgnapumc7Z9ilCS7Wnr+A9EtDyoZm50+Yq1NU Ca3yTKSK 8VlkqGYLiz9YnjA/FxTR+mShP/ck8f1DNedfekaC4sEBHL59d9NbzbW7bTMZgfu2lu7hnhdo9vcbpWOpSBLd4DTud+EjLssTR2dzCfmw55+19wQzeLmhTsBm6N4eMuBKkJYx2FIPE1YvSiTRGyOQ9j7ZRTeSEzsGRVwGk/0NIe7bV67hbYeUEJf8anw== 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: CONFIG_DEBUG_STACK_USAGE provides a mechanism to know the minimum amount of memory that was left in stack. Every time the new anti-record is reached a message is printed to the console. However, this is not useful to know how much each page within stack was actually used. Provide a mechanism to count the number of time each stack page was reached throughout the live of the stack: $ grep kstack /proc/vmstat kstack_page_1 19974 kstack_page_2 94 kstack_page_3 0 kstack_page_4 0 In the above example only out of ~20K threads that ever exited on that machine only 94 touched second page of the stack, and none touched pages three and four. Signed-off-by: Pasha Tatashin --- include/linux/sched/task_stack.h | 39 ++++++++++++++++++++++++++++++-- include/linux/vm_event_item.h | 29 ++++++++++++++++++++++++ include/linux/vmstat.h | 16 ------------- mm/vmstat.c | 11 +++++++++ 4 files changed, 77 insertions(+), 18 deletions(-) diff --git a/include/linux/sched/task_stack.h b/include/linux/sched/task_stack.h index ccd72b978e1f..7ff7f9997266 100644 --- a/include/linux/sched/task_stack.h +++ b/include/linux/sched/task_stack.h @@ -95,9 +95,41 @@ 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_5]); +#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 +140,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 */