Received: by 2002:ab2:710b:0:b0:1ef:a325:1205 with SMTP id z11csp1471841lql; Tue, 12 Mar 2024 20:34:40 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCX2Hyry9cImI1rCiKfCwBR6+iDC5YaAYHmhKLKtR7DgE2imNgnuaQ3ozP82GJCE1Rz3LqyfofajMBCkqspeScsqLPpWMmJ2Ih6mOrZDAQ== X-Google-Smtp-Source: AGHT+IFxhV1Xqv1qZ/rBPFVxMpAC/QBgULWwYKxu992LvlRREawAlBMf4Otlm/NX2PytNMifjINj X-Received: by 2002:a50:d7dd:0:b0:567:3c07:8bbc with SMTP id m29-20020a50d7dd000000b005673c078bbcmr7907765edj.21.1710300880412; Tue, 12 Mar 2024 20:34:40 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1710300880; cv=pass; d=google.com; s=arc-20160816; b=1KfXyqu2rHkr6Q07ilynMJ2i66MclYvLv6XxDz9CjCLjUxHPuR0BQIXMQ8cz8P6Rsj w9GJCS0YoksPn952AkcN0uBbxxplepA9q+Cqayid42zXtbH3l8vdDnbqKhiyroBWTg2f Bgim0v0NPv1MILIYdazlT6vTqFM3COmRhiKsRFX60uiwZmLmK/3c4DiQ2ZSy4tGnKzR+ bMmw7RIq0lF0jvnSmFKiM1HLqQEYStoxNupxdyD+l/3xjSBYtOuDvp84TfW/eHJlDZIU r6eGdNpztiviIEv9AfwZUsoyWdad/ni1NjaLEb4NkTIvz/p5xZWrTFM9PVfbnvTPLFZE 8uEw== 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=Sli3mfWx0lYFMlw598vEsdEvssG10grtOqJ1aTNbNWY=; fh=kzsuAHhry6+IEgavn/nzskczjsYTnCzRytsjKH8O7+I=; b=Ry1HWChkooliogMUZRk9L3OlwosTJS17HeN6ITDyC2wd7KL7mnh7gxNu2CT2BjaVNG ZMyDq46Ov5T05n+qQTzOk37Sx2PG1+AMfeYN9YScyF12i26OaR3coabHtywDCuHGuQp0 SEy304EaMf9HhiwK56Lm1USpxnc0xYP8zQBIa0CAjobMFCWi0+K/3XBPZ7YsFiZqpuAJ lkYyukMyXErErTbPNaVdBYRprsoOeHQBAEgV25e36ZmCPbr8gYzze5CRYO6Mla5byD/6 zz2NSIOruX9iQabPuZbwWP06zmVLs+YuDxl60Gi3aBnUkfzd9KrUXrIOoCKzu6M8QVqa WzDA==; 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=Bn7P49KN; 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-101077-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-101077-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=soleen.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id n12-20020a05640205cc00b005666d5f394fsi4287706edx.231.2024.03.12.20.34.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Mar 2024 20:34:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-101077-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@soleen-com.20230601.gappssmtp.com header.s=20230601 header.b=Bn7P49KN; 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-101077-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-101077-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 am.mirrors.kernel.org (Postfix) with ESMTPS id 89D8E1F22523 for ; Wed, 13 Mar 2024 03:34:39 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D993F10A13; Wed, 13 Mar 2024 03:34:33 +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="Bn7P49KN" Received: from mail-qk1-f181.google.com (mail-qk1-f181.google.com [209.85.222.181]) (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 E9C8D10953 for ; Wed, 13 Mar 2024 03:34:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.181 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710300872; cv=none; b=nDVpIocYP6dcKvDc4EuREU3uDXO9SOoX7iaM+XnrFl6G3TNoqj3TCeZ6uxtFuVPaAXbchExm9XpwJQkIfweGfy4OTnhz6SyYtwybbEr1lD2zaSGmH46avyVuNWK+wBiFOmGPEhiaTlijoVtqBnXoQ5NQt5oLL3irzWe4jawLKQE= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1710300872; c=relaxed/simple; bh=0AnQsIoGRXEiWmpNu1y3bPVdadUrUGdZZ7FkjytxHLo=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=S9asfuUEw+EjbRpLhE8grsnt+KLnjuN+b1pD5+LITvM/4Hnunb3NUKDT3uheEM/NZpEP5F/Hixp45i6Mqzn78VuIdMY97ScUhG8Czz0lpDpebCRdoHXY7SqtNPHTAMx7GV4JWQKBcgpOa776nknfJAFtJOsnclapXjhTfDQEEzk= 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=Bn7P49KN; arc=none smtp.client-ip=209.85.222.181 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-f181.google.com with SMTP id af79cd13be357-7882d75e766so24442585a.1 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=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=Sli3mfWx0lYFMlw598vEsdEvssG10grtOqJ1aTNbNWY=; b=Bn7P49KNinuHKkiNplCz0fE3ZMxMw1g+fSzND62rw2GNdJE5+dF1p5t3XOYek7AtZ3 U8ZAQzvnBpRYuHrIXcZ0rrn++Fs7YcrfiWpHIAEbIjUxYPaAV+PR2L0z1+/nfwhF/2z+ JoW3m4Ceubk/qfi8gaH+yLC/0aqcdyxi1jT7PXPF/Wpo80riFvvXUJZvE2BDenrKUpZV JYjPuJxw55rlVdaq3t5a3BJuBay/VBkfe9/+rSCuy7EcqvldC07Kl5nfb2x83m/NvObY eHGSqOBPjU0qHDvHpExa/Dg5wtQZEKMZEbPK8smZNu+bkTjyugXqbuuiV5nLRKXNAioW z1ZA== 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=Bgjc3KyY7FWsdsFyw5CCnMWRTGldqfx5zpD+jtHAEMCmQUq8Zdd9F3TGXl7aj38BG4 q2fPU1y8zk1gNKp52bMBwyzZOo7/T+kUgPT97WkWIqb/3ucvxpynSlo01AnSvza+2De2 FbItGSfpAyazCgCw4n+ZKjZblnKPtzu8YvYFAGDZNQb8YnWON5MibYcXxVolc6xw2l2F xdp9AYrSjdoG2IwSFyWAVNtto8uPoo5bb+7TrUTYhv+bJbFAvDlHXSa+5ktgBR5ySAh0 vlB+X0OkKxDjFDcMUM1Pqn1Fl7UhqK+gk5nRfJmO5EMg7pql6Fh722fyfNk32lg7kjPw wtfA== X-Forwarded-Encrypted: i=1; AJvYcCVerFlcn170546we/MuErsHQyhToWjmhe+S8qhrkhbkTo35JDvRm37E0q5txWrveOXgT9OHPyis/AnDi8j3iGoZ/8LP/sRYPNxlm30p X-Gm-Message-State: AOJu0Yzrn5JmswKkLu9xx7OpCFn1smr6KUgM9Y4AaGYtrlp5WKf5wMIF QdMROw1/KlJH6nk/o+VPmOR2DUfCMXM7J5nz6MYHqrP30sJMiVSHRCPR7fP2i5s= 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 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 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 */ -- 2.44.0.278.ge034bb2e1d-goog