From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dy1-f201.google.com (mail-dy1-f201.google.com [74.125.82.201]) (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 E52FA366820 for ; Fri, 24 Apr 2026 19:17:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777058226; cv=none; b=jAxO8bm56jyx4xOkJj7UUVwJfssx4KSpGQ2BezuHCFXZ5N0N+L35QMRc1w2SGqtO5FJNoq3KZLWpXQRY5Xs5onlKsRib0HhYdMu1EOzZlp3DOPIHpCRjzBWo5+kfsADe4F4PKtiDGcNiypEHZMqAFN1o8C83soYgA2oUPfhEqCg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1777058226; c=relaxed/simple; bh=MryoLFz+gAPvMVLjt/bhg2ZLuhTMPgGMCzfEsk6T/Ag=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=RFa5bOdxZXnB/kSCFx/d8kBSqIF2E8pGeefjzUrgsE8ALLenS5xKc9E9NeRlV5rW9eeWrBpZHw2CmYog6Rpfx9gmYWuBZNHV7BBBcU5drqHAS/SwTGb/yOWvJvVp9zvHnIMb8sKTTEHmrColCHW2v0oKwFA7jjfZuOLou1LmPbE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--stevensd.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=LJ2mFNkG; arc=none smtp.client-ip=74.125.82.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--stevensd.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="LJ2mFNkG" Received: by mail-dy1-f201.google.com with SMTP id 5a478bee46e88-2d93379001eso16843532eec.1 for ; Fri, 24 Apr 2026 12:17:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1777058224; x=1777663024; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=bJ923Ki6R5fPmM4gLIIn14hi8wnIAPg7CegKahU1aFY=; b=LJ2mFNkG5i+ePoxWlPeWbi4CHwiaRCLWKB6/j9IEjr2utnDbvsjvdnJa24Xi2aZTeX dShHIDe0B8m+J1fnAijCVLaMQyupDDksLfthz4iaN74dwf5NkK4bmXoPGX/VX5T3/jeN koVsFjTQ3WFArklEmDf/G8lG5Ibr+b6b25OGTNJkokaK5/1grXhjlniNqRTrRO43WxQp fnszr0rzVq9gddvdiulfND+n15Q/HW+MWAdXdoVOOea0FPOCtc/Jle69i27ebQ6NhA1N /sjb4HFtAFZMUeAD6ExKQuYM0EsgxDaAP0joWMnsG9Tnt6lvBTrqW7wW9TmomqTz4ewI uRag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1777058224; x=1777663024; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bJ923Ki6R5fPmM4gLIIn14hi8wnIAPg7CegKahU1aFY=; b=XMfo8oE+BGOINxqysuvWA4QpA8uNXEmld12D1n49cYxVs6T09Vyhpv3+nDzlvJ2f85 RHCI85n5YIMfWz/0BFf6qpxLYGq2+gT7S2v4QiNX3pUqqyxu4QsNr4UdjTBdj4DBR2el s5n4SbJMwBRztAXARGz2Pm5usPez6KOBHUPVFxuUw9viEwQUt9p4BG3gpE/PhQ6da/8K uklnM+58KHWDZl/Z/t0mBynhZ0GoA27KwucNscM44uIWAtGq3L91wL1eGxAxeMVbVtL4 Wiq51eljsKr19JwRZtAhZr6iZRZMtRxabJfiPVXa34p3h+G7i7Kl1FA0bwkyO2rkznwL 98uQ== X-Forwarded-Encrypted: i=1; AFNElJ9e98MyXLPwWpghEBdPy/2QzBZTnJwu4vTinf1QoDyhn78il54lizWXfZfJoKlP7HdP/n1IdZpsBAtdvto=@vger.kernel.org X-Gm-Message-State: AOJu0YwM5lhEFC5KRJ76x1DV8zzAd+j12NBcycZ5mS7B/kMAq47T3LVo Xa3YJp6hinPQx/jee7dqShZ1RSlj2bKwgZ7HTOm8dXbXyjMZbNXM1cNonZH//3IPjUNOrjTl09d T+VxhtXfHnomvxg== X-Received: from dycoy24.prod.google.com ([2002:a05:7301:fc18:b0:2e2:488c:4eba]) (user=stevensd job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:8628:b0:2d8:1efe:51dc with SMTP id 5a478bee46e88-2e464ea8c23mr20065493eec.6.1777058223573; Fri, 24 Apr 2026 12:17:03 -0700 (PDT) Date: Fri, 24 Apr 2026 12:14:51 -0700 In-Reply-To: <20260424191456.2679717-1-stevensd@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260424191456.2679717-1-stevensd@google.com> X-Mailer: git-send-email 2.54.0.rc2.544.gc7ae2d5bb8-goog Message-ID: <20260424191456.2679717-9-stevensd@google.com> Subject: [PATCH v2 08/13] task_stack.h: Add stack_not_used() support for dynamic stack From: David Stevens To: Pasha Tatashin , Linus Walleij , Will Deacon , Quentin Perret , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Andy Lutomirski , Xin Li , Peter Zijlstra , Andrew Morton , David Hildenbrand , Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Uladzislau Rezki , Kees Cook Cc: David Stevens , linux-kernel@vger.kernel.org, linux-mm@kvack.org Content-Type: text/plain; charset="UTF-8" From: Pasha Tatashin CONFIG_DEBUG_STACK_USAGE is enabled by default on most architectures. Its purpose is to determine and print the maximum stack depth on thread exit. The way it works, is it starts from the bottom of the stack and searches the first non-zero word in the stack. With dynamic stack it does not work very well, as it means it faults every pages in every stack. Instead, add a specific version of stack_not_used() for dynamic stacks where instead of starting from the bottom of the stack, we start from the last page mapped in the stack. In addition to not doing unnecessary page faulting, this search is optimized by skipping search through zero pages. Also, because dynamic stack does not end with MAGIC_NUMBER, there is no need to skip the bottom most word in the stack. Signed-off-by: Pasha Tatashin [Rebased, Kasan oneliner needed preserving, rewrote a bit due to bugs] Signed-off-by: Linus Walleij [Handle init_task's use of init_stack, fix typos] Signed-off-by: David Stevens --- arch/Kconfig | 1 - kernel/exit.c | 22 ++++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/arch/Kconfig b/arch/Kconfig index 95ded79f0825..beffe7e01296 100644 --- a/arch/Kconfig +++ b/arch/Kconfig @@ -1542,7 +1542,6 @@ config DYNAMIC_STACK depends on VMAP_STACK depends on INIT_STACK_ALL_ZERO || INIT_STACK_ALL_PATTERN depends on !KASAN - depends on !DEBUG_STACK_USAGE depends on !STACK_GROWSUP depends on !PREEMPT_RT help diff --git a/kernel/exit.c b/kernel/exit.c index ede3117fa7d4..6caf4030e8f4 100644 --- a/kernel/exit.c +++ b/kernel/exit.c @@ -71,6 +71,7 @@ #include #include #include +#include #include @@ -791,6 +792,26 @@ unsigned long stack_not_used(struct task_struct *p) return (unsigned long)end_of_stack(p) - (unsigned long)n; } #else /* !CONFIG_STACK_GROWSUP */ +#ifdef CONFIG_DYNAMIC_STACK +unsigned long stack_not_used(struct task_struct *p) +{ + struct vm_struct *vm_area = task_stack_vm_area(p); + unsigned long stack = (unsigned long)task_stack_page(p); + unsigned long alloc_size, *n; + + /* This is NULL only for init_task, where init_stack is fully allocated. */ + if (likely(vm_area)) + alloc_size = vm_area->nr_pages << PAGE_SHIFT; + else + alloc_size = THREAD_SIZE; + n = (unsigned long *)(stack + THREAD_SIZE - alloc_size); + + while (!*n) + n++; + + return (unsigned long)n - stack; +} +#else unsigned long stack_not_used(struct task_struct *p) { unsigned long *n = end_of_stack(p); @@ -801,6 +822,7 @@ unsigned long stack_not_used(struct task_struct *p) return (unsigned long)n - (unsigned long)end_of_stack(p); } +#endif /* CONFIG_DYNAMIC_STACK */ #endif /* CONFIG_STACK_GROWSUP */ /* Count the maximum pages reached in kernel stacks */ -- 2.54.0.rc2.544.gc7ae2d5bb8-goog