From patchwork Mon Nov 20 17:47:14 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 13461830 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 93999C197A0 for ; Mon, 20 Nov 2023 17:49:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1B89A6B036B; Mon, 20 Nov 2023 12:49:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 11A8B6B0370; Mon, 20 Nov 2023 12:49:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DC47A6B0374; Mon, 20 Nov 2023 12:49:43 -0500 (EST) 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 C59D76B036B for ; Mon, 20 Nov 2023 12:49:43 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 95D9B1CADE7 for ; Mon, 20 Nov 2023 17:49:43 +0000 (UTC) X-FDA: 81479070246.06.AC9E8E6 Received: from out-173.mta0.migadu.com (out-173.mta0.migadu.com [91.218.175.173]) by imf04.hostedemail.com (Postfix) with ESMTP id 9902B40027 for ; Mon, 20 Nov 2023 17:49:41 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="rZ/C2Hcs"; spf=pass (imf04.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.173 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700502581; 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-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=91QxaYvk4RHHEJwI4Sc7Oo7z2rlFVPgOoXOer3eAY+g=; b=59NqEnXIxqmDsvqgo//ebEPhfTWVpBP6RaYiI+NvmqedhyF9fZw0n+VwVCM/3jHkU7Qaly zsbnleyzIdQQHe3TnSgW0xe7VrqYHc9J2JmFcYZUmrK6nvZ9m8BbJOiTUNgTkBkbqef+2o gsicMMLgmA+NPE6SVwMD8/fvQvuE3eU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502581; a=rsa-sha256; cv=none; b=ZJ/GN85JjtvuqAMPytg8XrEL3REGUitYSzW5vh4fmSnnxKojSD03LIfbA760nDcVU6UAqf g9OMgrQUJmMore8FfQIn7vmCvNHll6v2YV3lFmbJigZ9JZilpkw6Q08scG+RV/WDhieE6s YuzCYnv2q7DNwpJI4V2MG8U+jREojNY= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="rZ/C2Hcs"; spf=pass (imf04.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.173 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1700502580; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=91QxaYvk4RHHEJwI4Sc7Oo7z2rlFVPgOoXOer3eAY+g=; b=rZ/C2HcsNzvMwpX5O9VOx1lNSC2GHR0kSTsgEzMvFKq6y+hKBKp3/9Ua+zCoBCJmYeieov wMbZyfFLCbQY6lOshDlPQuKUW5//srMlo9hBj2CFZiAjPs39TBjkS5qlASK6p+8fCOmgfm q8MQh7TtmnvvOGnA2P/0s6mRGFe8vjU= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 16/22] lib/stackdepot: add refcount for records Date: Mon, 20 Nov 2023 18:47:14 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 9902B40027 X-Rspam-User: X-Stat-Signature: jirhg1azoyzpfrq8rupa18sde78br1sf X-Rspamd-Server: rspam03 X-HE-Tag: 1700502581-631065 X-HE-Meta: U2FsdGVkX1+bS/x+3cRLuADBlCCIFX1ULQR7lNm8Uv24vmbs/ka/Iqe5KByHsBZGUldCfdI5YvZXukw6k69Gyck5LPNBlWgrwPhGpUZcsN/GPNNSNXVCzKfDcNNi1BkJ+9vj+epRx5PckJjl6+G5O37FRSreqFfiP6O23tw4e4yB9yRYqoDwwSbyzHRAqUBe+DK2WeQgigI9fwGCi4DP1o86oXCRQqqEJzI27usih7zwAUTmXVkKpjaxxq0EkcWZJNVt+eVJNKuwGWNDaZKur9WhN0wV72UJdkgEvmsmxYhylXrw5lP3/tu5cb12f4df17vwPQwN4vcx3qOVviK51ZBqdtL7LyaD/aSq2s8WAslz5WdzIKJ0jc1KRZE+5VhpnLTfrr4/KjmFsMWx069ur0KWd4VC6yHeO0Da58w2fL7H5qfCgiSUnKXpiaf680AJt2U85UbZe6NffHAnlnnZNJN09E4RaPzDJTvydCMXC0h9HoSTzv0VzR5mujDor0unw1ptCrn758kYMlKxZI/DjV+EXT5ivvA95nLWNEak0AmVeyXrL8Qsc1XxXoS95QfGjlwumJYG3+88CrC1si+2MzI1eQG9PdLK87ihrD07MrGuxFUeBPAZNFLApg/gbIRf4pv8USMDrP96f+pO28Hmo08wepgvTTF278ZE3NRYoB+Pv2GAytqevpTrmBcASYQbJBrvqn7oZG6Jqwl3wU5XuT2NGK7WUxlrRelWPe5J4BjPR1TMnanu6iODTcoq0xDhY6fC91EtvgVbKBzMvV6hA1LXbhhoOMQ5cYIQVZwEaufWt9ublSxX4hY9XS9s8RLvgZJ+72d6X7q3jHD9LlGlevo0OkGY82VfE+GHzfEobfEP3sS5RGCCVFePYb/alDZgRvFhogcUIdcBIc/obbb9VtQAQV+yLsLkk9xNYTzWCGp+yz55puKUaOvDJMjL1S15KepM54dZdEh2vTtfG9V /6G/9ibV 0/E6nE8yW7xYW0J7bMqFqHGI1Fi3b2AVnrSg25l2noIRb+RjcPdvtzH2VLw== 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: From: Andrey Konovalov Add a reference counter for how many times a stack records has been added to stack depot. Add a new STACK_DEPOT_FLAG_GET flag to stack_depot_save_flags that instructs the stack depot to increment the refcount. Do not yet decrement the refcount; this is implemented in one of the following patches. Do not yet enable any users to use the flag to avoid overflowing the refcount. This is preparatory patch for implementing the eviction of stack records from the stack depot. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- Changes v1->v2: - Add forgotten refcount_inc() under write lock. - Add STACK_DEPOT_FLAG_GET flag for stack_depot_save_flags. --- include/linux/stackdepot.h | 13 ++++++++++--- lib/stackdepot.c | 12 ++++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/include/linux/stackdepot.h b/include/linux/stackdepot.h index 0b262e14144e..611716702d73 100644 --- a/include/linux/stackdepot.h +++ b/include/linux/stackdepot.h @@ -39,8 +39,9 @@ typedef u32 depot_flags_t; * to its declaration for more details. */ #define STACK_DEPOT_FLAG_CAN_ALLOC ((depot_flags_t)0x0001) +#define STACK_DEPOT_FLAG_GET ((depot_flags_t)0x0002) -#define STACK_DEPOT_FLAGS_NUM 1 +#define STACK_DEPOT_FLAGS_NUM 2 #define STACK_DEPOT_FLAGS_MASK ((depot_flags_t)((1 << STACK_DEPOT_FLAGS_NUM) - 1)) /* @@ -94,6 +95,9 @@ static inline int stack_depot_early_init(void) { return 0; } * flags of @alloc_flags). Otherwise, stack depot avoids any allocations and * fails if no space is left to store the stack trace. * + * If STACK_DEPOT_FLAG_GET is set in @depot_flags, stack depot will increment + * the refcount on the saved stack trace if it already exists in stack depot. + * * If the provided stack trace comes from the interrupt context, only the part * up to the interrupt entry is saved. * @@ -116,8 +120,11 @@ depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, * @nr_entries: Number of frames in the stack * @alloc_flags: Allocation GFP flags * - * Context: Contexts where allocations via alloc_pages() are allowed. - * See stack_depot_save_flags() for more details. + * Does not increment the refcount on the saved stack trace; see + * stack_depot_save_flags() for more details. + * + * Context: Contexts where allocations via alloc_pages() are allowed; + * see stack_depot_save_flags() for more details. * * Return: Handle of the stack trace stored in depot, 0 on failure */ diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 59d61d5c09a7..911dee11bf39 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -60,6 +61,7 @@ struct stack_record { u32 hash; /* Hash in hash table */ u32 size; /* Number of stored frames */ union handle_parts handle; + refcount_t count; unsigned long entries[CONFIG_STACKDEPOT_MAX_FRAMES]; /* Frames */ }; @@ -373,6 +375,7 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) stack->hash = hash; stack->size = size; /* stack->handle is already filled in by depot_init_pool(). */ + refcount_set(&stack->count, 1); memcpy(stack->entries, entries, flex_array_size(stack, entries, size)); /* @@ -489,6 +492,8 @@ depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, /* Fast path: look the stack trace up without full locking. */ found = find_stack(bucket, entries, nr_entries, hash); if (found) { + if (depot_flags & STACK_DEPOT_FLAG_GET) + refcount_inc(&found->count); read_unlock_irqrestore(&pool_rwlock, flags); goto exit; } @@ -528,12 +533,15 @@ depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, list_add(&new->list, bucket); found = new; } - } else if (prealloc) { + } else { + if (depot_flags & STACK_DEPOT_FLAG_GET) + refcount_inc(&found->count); /* * Stack depot already contains this stack trace, but let's * keep the preallocated memory for future. */ - depot_keep_new_pool(&prealloc); + if (prealloc) + depot_keep_new_pool(&prealloc); } write_unlock_irqrestore(&pool_rwlock, flags);