From patchwork Mon Oct 23 16:22:38 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: 13433186 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 5E595C001E0 for ; Mon, 23 Oct 2023 16:24:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C84756B00EF; Mon, 23 Oct 2023 12:24:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C339E6B0100; Mon, 23 Oct 2023 12:24:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AFC8D6B0101; Mon, 23 Oct 2023 12:24:07 -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 9C80B6B00EF for ; Mon, 23 Oct 2023 12:24:07 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 708E04036A for ; Mon, 23 Oct 2023 16:24:07 +0000 (UTC) X-FDA: 81377248134.08.758F99B Received: from out-192.mta1.migadu.com (out-192.mta1.migadu.com [95.215.58.192]) by imf22.hostedemail.com (Postfix) with ESMTP id 96CD8C002B for ; Mon, 23 Oct 2023 16:24:05 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=t+UnmZVx; spf=pass (imf22.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.192 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=1698078245; 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=NZ9DxgJteIUWBZVu3uTAKUKrmlqiVCjIJZsHKffVHuY=; b=MVhH4UEeQ1RzKyPpRdCrk1Mu8QWtU4shAUyIpYCxChV/3PvnVxmd+HWVkhAG0P/EdibRSG FQ1+dQ/3XlPJ/es2kByfzfCvUe0owH6IimTuWTpYvnffNpeKqKUDe0EUXudEKvxGTdPlUH xeBKlSO0RiuV3RxbW4Pa78ZZzNosOIg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078245; a=rsa-sha256; cv=none; b=2m13I93pd2RL2XRrNQKBeIMB16hFLCyjQcTVa6zlH4GqqnkZgmCCiS86DZXdyzU398V3gT O8NKYJkiZcfI40UlDAAgtQ0yIswKjrHaABCK6I04bOEL67/AFziz1DXgjC/WAhDcm8m8Z7 z8BqfPATccQFqbGa2y9elAtprOMTP04= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=t+UnmZVx; spf=pass (imf22.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.192 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=1698078243; 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=NZ9DxgJteIUWBZVu3uTAKUKrmlqiVCjIJZsHKffVHuY=; b=t+UnmZVxxGN5KzWEQTBmXitTlS6kZTAGzoq0dlMyEevn1HxA0s/x7HvVUtWknOMuAkjIeo BUBxiie7hZs3gjhcBL/qrihnoFcL2dZNbSEVqgM/Rho1AztZFu7TnVgjNbIz/DOVY1yTw3 E63W+Bv8C9VotbqWpTxfMOF8IeJvWIk= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v3 07/19] lib/stackdepot: rework helpers for depot_alloc_stack Date: Mon, 23 Oct 2023 18:22:38 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 96CD8C002B X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: oqidznydr88gtezi1fx4bc69yxujdtdj X-HE-Tag: 1698078245-235481 X-HE-Meta: U2FsdGVkX1/MhTf52kSlMFj8SOySJXHvbxak9Zr71brALQKSBcYYFtPPd/j+os2Hp/RAWD+0u2blNbHNerOoZLY16GCSs1z/eWjgtByt5gLipyDzG2+x8Q6tkawxbjIe24O98rRnc3gJrzEzzKbZk0TXJQWNG4CEaz+tVLSiAEqBxhMBgx8ozwPApErTythD9fbZFiQ90YZvlgxVawrJQBnkrzU/B1plRJIOooUy9iRQB7ZbqenFI9X8dGQlU7DkXpSuL3iNPMITyp/9S8If8HN2gBLn56gSX7Miyd2kbaWy6copY4EYd/nwTv9fNOIy9CwSBcB4ae6X7U6mdaLCUKhvVdFr+spdeUsu7xrmT33CC23ltjCenCpZV8topxUtbc8j+pHRl6JcGcMM/M7k6tiO9BbK8v+fiEHalYk7YQ50tZaYXnZIJopJOkAIvGIePXEyLCMFJ6C5BDcy4L8ik8qFZDfqAGWLz6HstfBecaEO25BhgR57+oIL8+DuYvu9BTdd2mNp3Kv1r9v4WsLRNL59M21AwAdf6hDbjgp9Qp8hBMPNE62AEVmZjQJH9Msu2s6ioMxpj3aOrvhSo/UCDa6sRvBKPB/KIIbiVKhadFVSWBl+CnJHwRUmMAexps2mN7DHTWPlnKWVb6LNY9Ae417CHz57S9APBmSVwsdM8IQMKoUi55gqOKHzHLL5qeun2D9dcfRbFgqVkY3qHiDTfZEPZpAIoEOoJIy9ShXk1dfaZPbiVStYqqXRs37XNsGFBWrCocd/rgNd+f5qVUUUUE6MsOix4dZVen15Tu1HYKl1IANSyw95Y4DuYIyixj+L/1F7ZNKq2Mlnppwiq2b4J258Pg2gTMTHR2Sk9IfMfZGfQMXZTcGvxKTiDYQHkBh9gFOnkDoJ4F1yS8PcbzkcJDbwE+kQ3JBwdQGdeefOWGjowaZcLWJf/0loG3cqMdHGy1XG7LF+Xx5gzasW0Y4 t7rDWdi7 L/jG8Fpp/4PxmqeC2zvd9TqwPhNWZa1m1J6+V/g1m+MbRqpD/4WcLe9D6q6LG3O9zwNH4fVp0bD3Kzez8iqsMig9Z4BEvAJaUlhF+vnPxiGCOdtOxyEaUEoi5RQ5G1yeEzzbI/pJF/p1AOaTBHZnwya9PrQ== 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 Split code in depot_alloc_stack and depot_init_pool into 3 functions: 1. depot_keep_next_pool that keeps preallocated memory for the next pool if required. 2. depot_update_pools that moves on to the next pool if there's no space left in the current pool, uses preallocated memory for the new current pool if required, and calls depot_keep_next_pool otherwise. 3. depot_alloc_stack that calls depot_update_pools and then allocates a stack record as before. This makes it somewhat easier to follow the logic of depot_alloc_stack and also serves as a preparation for implementing the eviction of stack records from the stack depot. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- Changes v2->v3: - Add parentheses when referring to function calls in comments. --- lib/stackdepot.c | 86 +++++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 37 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 60aea549429a..3f921aaae44a 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -219,11 +219,11 @@ int stack_depot_init(void) } EXPORT_SYMBOL_GPL(stack_depot_init); -/* Uses preallocated memory to initialize a new stack depot pool. */ -static void depot_init_pool(void **prealloc) +/* Keeps the preallocated memory to be used for the next stack depot pool. */ +static void depot_keep_next_pool(void **prealloc) { /* - * If the next pool is already initialized or the maximum number of + * If the next pool is already saved or the maximum number of * pools is reached, do not use the preallocated memory. * Access next_pool_required non-atomically, as there are no concurrent * write accesses to this variable. @@ -231,44 +231,34 @@ static void depot_init_pool(void **prealloc) if (!next_pool_required) return; - /* Check if the current pool is not yet allocated. */ - if (stack_pools[pool_index] == NULL) { - /* Use the preallocated memory for the current pool. */ - stack_pools[pool_index] = *prealloc; + /* + * Use the preallocated memory for the next pool + * as long as we do not exceed the maximum number of pools. + */ + if (pool_index + 1 < DEPOT_MAX_POOLS) { + stack_pools[pool_index + 1] = *prealloc; *prealloc = NULL; - } else { - /* - * Otherwise, use the preallocated memory for the next pool - * as long as we do not exceed the maximum number of pools. - */ - if (pool_index + 1 < DEPOT_MAX_POOLS) { - stack_pools[pool_index + 1] = *prealloc; - *prealloc = NULL; - } - /* - * At this point, either the next pool is initialized or the - * maximum number of pools is reached. In either case, take - * note that initializing another pool is not required. - * smp_store_release() pairs with smp_load_acquire() in - * stack_depot_save(). - */ - smp_store_release(&next_pool_required, 0); } + + /* + * At this point, either the next pool is kept or the maximum + * number of pools is reached. In either case, take note that + * keeping another pool is not required. + * smp_store_release() pairs with smp_load_acquire() in + * stack_depot_save(). + */ + smp_store_release(&next_pool_required, 0); } -/* Allocates a new stack in a stack depot pool. */ -static struct stack_record * -depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) +/* Updates refences to the current and the next stack depot pools. */ +static bool depot_update_pools(size_t required_size, void **prealloc) { - struct stack_record *stack; - size_t required_size = DEPOT_STACK_RECORD_SIZE; - /* Check if there is not enough space in the current pool. */ if (unlikely(pool_offset + required_size > DEPOT_POOL_SIZE)) { /* Bail out if we reached the pool limit. */ if (unlikely(pool_index + 1 >= DEPOT_MAX_POOLS)) { WARN_ONCE(1, "Stack depot reached limit capacity"); - return NULL; + return false; } /* @@ -278,9 +268,10 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) */ WRITE_ONCE(pool_index, pool_index + 1); pool_offset = 0; + /* * If the maximum number of pools is not reached, take note - * that the next pool needs to initialized. + * that the next pool needs to be initialized. * smp_store_release() pairs with smp_load_acquire() in * stack_depot_save(). */ @@ -288,9 +279,30 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) smp_store_release(&next_pool_required, 1); } - /* Assign the preallocated memory to a pool if required. */ + /* Check if the current pool is not yet allocated. */ + if (*prealloc && stack_pools[pool_index] == NULL) { + /* Use the preallocated memory for the current pool. */ + stack_pools[pool_index] = *prealloc; + *prealloc = NULL; + return true; + } + + /* Otherwise, try using the preallocated memory for the next pool. */ if (*prealloc) - depot_init_pool(prealloc); + depot_keep_next_pool(prealloc); + return true; +} + +/* Allocates a new stack in a stack depot pool. */ +static struct stack_record * +depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) +{ + struct stack_record *stack; + size_t required_size = DEPOT_STACK_RECORD_SIZE; + + /* Update current and next pools if required and possible. */ + if (!depot_update_pools(required_size, prealloc)) + return NULL; /* Check if we have a pool to save the stack trace. */ if (stack_pools[pool_index] == NULL) @@ -324,7 +336,7 @@ static struct stack_record *depot_fetch_stack(depot_stack_handle_t handle) union handle_parts parts = { .handle = handle }; /* * READ_ONCE() pairs with potential concurrent write in - * depot_alloc_stack(). + * depot_update_pools(). */ int pool_index_cached = READ_ONCE(pool_index); void *pool; @@ -424,7 +436,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, * the memory now - we won't be able to do that under the lock. * * smp_load_acquire() pairs with smp_store_release() in - * depot_alloc_stack() and depot_init_pool(). + * depot_update_pools() and depot_keep_next_pool(). */ if (unlikely(can_alloc && smp_load_acquire(&next_pool_required))) { /* @@ -461,7 +473,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, * Stack depot already contains this stack trace, but let's * keep the preallocated memory for the future. */ - depot_init_pool(&prealloc); + depot_keep_next_pool(&prealloc); } raw_spin_unlock_irqrestore(&pool_lock, flags);