From mboxrd@z Thu Jan 1 00:00:00 1970 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 B3475C83F10 for ; Thu, 10 Jul 2025 02:48:36 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4C8CD6B00A3; Wed, 9 Jul 2025 22:48:36 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4A07F6B00A4; Wed, 9 Jul 2025 22:48:36 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3DDC96B00A5; Wed, 9 Jul 2025 22:48:36 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 2E5D46B00A3 for ; Wed, 9 Jul 2025 22:48:36 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 9A3FA1A0411 for ; Thu, 10 Jul 2025 02:48:35 +0000 (UTC) X-FDA: 83646821790.26.E61DF9D Received: from nyc.source.kernel.org (nyc.source.kernel.org [147.75.193.91]) by imf06.hostedemail.com (Postfix) with ESMTP id 2C257180002 for ; Thu, 10 Jul 2025 02:48:34 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=eZnmLeDy; spf=pass (imf06.hostedemail.com: domain of alx@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=alx@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1752115714; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=FG5EU/f/hB5bUkYkfvMhOxqTQ8gFlOM0lPZIWF+q+cc=; b=28yDRouCIKt/Z/+bSrttv0BGogI1GksU8yBkVrs10YIf+Svs/TVnjELdhSTtMbGmFbL1fU J0tHtSLHS61tFa7S+5+cz02spcnd/df0cBiUrlN4r0FwHyrbQLZvK+L/ef0dYVsBjJvZo1 QFjTjT6onkOMBpC3+nB+rucnEHc1ong= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=eZnmLeDy; spf=pass (imf06.hostedemail.com: domain of alx@kernel.org designates 147.75.193.91 as permitted sender) smtp.mailfrom=alx@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1752115714; a=rsa-sha256; cv=none; b=OR8nOLX/adihx1srERbCxh+M+n/6ehxWh75CxSGRNOebbCVx+GKVBp96O8968opcsdPiL8 WTzl3KZ8QeckmOWjsu8m9GFP0w4bPZN8Vw/lnIQETsxTlwR098cH6WmO9tQ5gaHhdAPeRB BF6g/UVda/JmiCjygs6k1MbY2MqRe48= Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by nyc.source.kernel.org (Postfix) with ESMTP id 9E976A534D3; Thu, 10 Jul 2025 02:48:33 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2ED4FC4CEEF; Thu, 10 Jul 2025 02:48:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1752115713; bh=KFeH759S2pXUEgcinWQTZzWHu8P8w4ivkJjMDXMeyRQ=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=eZnmLeDy6ISGhj7UxlzL16OXqtnL01sGVlHeOdJlX0OUfb3Vl8JDoi+of/VZywuuh eA4l+ydES65zs59icoEwIzPa0icurtsUfqRKSm2KuAQf2xF50qR1eN/ev+xt8F9ahJ 9Tsdl+GS29Mv8zVA5i3qb+rp8NWC/cIrbyTvxNs4kJrgFOQYHyYlV4SOxwj8fJV6JA 1kWMJWb04PLyDr7aXahB9NBtLgwR/0ZATlmtNEeG4wc8ntomD0LFS0HspaM9qWAbIJ 9LG7P2Yc+Kp9urBgAuRLebi8OzTHKhyzcYGQ0sHY6uyue/LSIDpH95D6k3zmsTx0we opt3OmQtc3uZg== Date: Thu, 10 Jul 2025 04:48:23 +0200 From: Alejandro Colomar To: linux-mm@kvack.org, linux-hardening@vger.kernel.org Cc: Alejandro Colomar , Kees Cook , Christopher Bazley , shadow <~hallyn/shadow@lists.sr.ht>, linux-kernel@vger.kernel.org, Andrew Morton , kasan-dev@googlegroups.com, Dmitry Vyukov , Alexander Potapenko , Marco Elver , Christoph Lameter , David Rientjes , Vlastimil Babka , Roman Gushchin , Harry Yoo , Andrew Clayton , Rasmus Villemoes , Michal Hocko , Linus Torvalds , Al Viro Subject: [RFC v4 2/7] stacktrace, stackdepot: Add sprintf_end()-like variants of functions Message-ID: <894d02b08056c59b5acb79af73a1a698d56016f5.1752113247.git.alx@kernel.org> X-Mailer: git-send-email 2.50.0 References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: X-Rspamd-Queue-Id: 2C257180002 X-Stat-Signature: y15wph1361zy4zmrtfudbtfwnowfk4xj X-Rspam-User: X-Rspamd-Server: rspam07 X-HE-Tag: 1752115714-879812 X-HE-Meta: U2FsdGVkX1+N9gq7siuaIXk7HcbpZnVBRHe4WoB7GD07NNuYpk2y+7oIeLMuyuzjnegGhznMCW9SitFkA7sCJ4FQVk5dR+DNSIPcRazsdL0wHK5TUSbxMlnnCvs5LOeNbsv6j7CK7UhItD2hgnTQuMPhVtyc0kPHau9yr/KoP1el8dLc1PECXLVFpU1fESi4+1U1HWEQh30MIPFqRQhPLkNeG08612ccJVlaTpwKBjtVKZ7n/2MLTQ0db7r4agFszpsvpHMwwLZoYujHv4+YML3rJ0o+lSyCFAh+ngNDQW/xFu36CMiTFTLF2ahbRBlj3zTTVBrqZbPBCeQZROuTMbmY0YszkPJ8COaUSNg1vRSjxBrCb9xrUEVnQ1tftYBd9nDbvrDJuTgM53BqLp3LNs6Lt94RSuPjxjEGpARO1+DTR0lYn/MZdwkhxflnMr5cgfHx1oW8Ih7AxqTCfVL6OQ6JGmE0Qg1LwhKIb5ZO/wo0QMJg5D70JEQnm5zk+WGvsIDsETze+XA9fMXB3Is66rftp437+sCOlfp3uEUnYHi6ma/alVEVyj56ebPw9oCt4YkmuAZOC0E3p6d3DuJQ9262ZVn1jjWghvD82IoNJfg2Utgd2TGBF5r2j5nJAHLe+bP+szdUjQAtShxvQ1zlw5k18SURJeqz1muOKM0rzfrGDrZHwiPr+8gQJ94NKmfXe+Koex9cj/BFWI8l9HgW16td4iZVpTEiuXaU/hYOxdOIXW2rk6fOMcYfCZGJc+hCnWutc9hKfFghV/3Jy1o1majmiwv/yKelKnDdgUKvcDni3jCVU4fkTxs5+wYpH+cONueJV42V2LErHvYsJJkykidQ/Ht8JunbmyU6xZ9DC8poouxl+Co6WEbKH02T47z+FbuKG6EKo3aDfAM81WFhJnBDX00PanNvd6EXzBwlQ/G7qrtbjCcP7fwprFx6uflKVgqfFsvJFtGoZXJX7WQ AN8nlQVp 4BWfYNr9pHtT22c9DmbwV/4qOJgkk0eEG1Iuw8k0eRz2sZ3EtsM/sba/TPtCoA3N1CaZKQOlTdTeS7yk= 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: Cc: Kees Cook Cc: Christopher Bazley Cc: Rasmus Villemoes Cc: Marco Elver Cc: Michal Hocko Cc: Linus Torvalds Cc: Al Viro Signed-off-by: Alejandro Colomar --- include/linux/stackdepot.h | 13 +++++++++++++ include/linux/stacktrace.h | 3 +++ kernel/stacktrace.c | 28 ++++++++++++++++++++++++++++ lib/stackdepot.c | 13 +++++++++++++ 4 files changed, 57 insertions(+) diff --git a/include/linux/stackdepot.h b/include/linux/stackdepot.h index 2cc21ffcdaf9..76182e874f67 100644 --- a/include/linux/stackdepot.h +++ b/include/linux/stackdepot.h @@ -219,6 +219,19 @@ void stack_depot_print(depot_stack_handle_t stack); int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size, int spaces); +/** + * stack_depot_sprint_end - Print a stack trace from stack depot into a buffer + * + * @handle: Stack depot handle returned from stack_depot_save() + * @p: Pointer to the print buffer + * @end: Pointer to one past the last element in the buffer + * @spaces: Number of leading spaces to print + * + * Return: Pointer to trailing '\0'; or NULL on truncation + */ +char *stack_depot_sprint_end(depot_stack_handle_t handle, char *p, + const char end[0], int spaces); + /** * stack_depot_put - Drop a reference to a stack trace from stack depot * diff --git a/include/linux/stacktrace.h b/include/linux/stacktrace.h index 97455880ac41..79ada795d479 100644 --- a/include/linux/stacktrace.h +++ b/include/linux/stacktrace.h @@ -67,6 +67,9 @@ void stack_trace_print(const unsigned long *trace, unsigned int nr_entries, int spaces); int stack_trace_snprint(char *buf, size_t size, const unsigned long *entries, unsigned int nr_entries, int spaces); +char *stack_trace_sprint_end(char *p, const char end[0], + const unsigned long *entries, + unsigned int nr_entries, int spaces); unsigned int stack_trace_save(unsigned long *store, unsigned int size, unsigned int skipnr); unsigned int stack_trace_save_tsk(struct task_struct *task, diff --git a/kernel/stacktrace.c b/kernel/stacktrace.c index afb3c116da91..f389647d8e44 100644 --- a/kernel/stacktrace.c +++ b/kernel/stacktrace.c @@ -70,6 +70,34 @@ int stack_trace_snprint(char *buf, size_t size, const unsigned long *entries, } EXPORT_SYMBOL_GPL(stack_trace_snprint); +/** + * stack_trace_sprint_end - Print the entries in the stack trace into a buffer + * @p: Pointer to the print buffer + * @end: Pointer to one past the last element in the buffer + * @entries: Pointer to storage array + * @nr_entries: Number of entries in the storage array + * @spaces: Number of leading spaces to print + * + * Return: Pointer to the trailing '\0'; or NULL on truncation. + */ +char *stack_trace_sprint_end(char *p, const char end[0], + const unsigned long *entries, unsigned int nr_entries, + int spaces) +{ + unsigned int i; + + if (WARN_ON(!entries)) + return 0; + + for (i = 0; i < nr_entries; i++) { + p = sprintf_end(p, end, "%*c%pS\n", 1 + spaces, ' ', + (void *)entries[i]); + } + + return p; +} +EXPORT_SYMBOL_GPL(stack_trace_sprint_end); + #ifdef CONFIG_ARCH_STACKWALK struct stacktrace_cookie { diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 73d7b50924ef..48e5c0ff37e8 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -771,6 +771,19 @@ int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size, } EXPORT_SYMBOL_GPL(stack_depot_snprint); +char *stack_depot_sprint_end(depot_stack_handle_t handle, char *p, + const char end[0], int spaces) +{ + unsigned long *entries; + unsigned int nr_entries; + + nr_entries = stack_depot_fetch(handle, &entries); + return nr_entries ? + stack_trace_sprint_end(p, end, entries, nr_entries, spaces) + : sprintf_end(p, end, ""); +} +EXPORT_SYMBOL_GPL(stack_depot_sprint_end); + depot_stack_handle_t __must_check stack_depot_set_extra_bits( depot_stack_handle_t handle, unsigned int extra_bits) { -- 2.50.0