From patchwork Wed Sep 13 17:14:29 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: 13383617 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 B39BEEE01F5 for ; Wed, 13 Sep 2023 17:15:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 625846B0255; Wed, 13 Sep 2023 13:14:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 587C26B0256; Wed, 13 Sep 2023 13:14:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 38DCC6B0257; Wed, 13 Sep 2023 13:14:54 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 1AF6B6B0255 for ; Wed, 13 Sep 2023 13:14:54 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id ECCF980E87 for ; Wed, 13 Sep 2023 17:14:53 +0000 (UTC) X-FDA: 81232224066.30.906EADE Received: from out-218.mta0.migadu.com (out-218.mta0.migadu.com [91.218.175.218]) by imf03.hostedemail.com (Postfix) with ESMTP id 2B32720008 for ; Wed, 13 Sep 2023 17:14:51 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ctPU1P8P; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf03.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.218 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694625292; 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=9Tw+dU2o5sfOuuXbX4FJVP/5BAKc6p0/nQ7cBmUzv5Q=; b=58HfvPPo4vigRypI+3Cubzsf+vk4Sn5BO94rciUGR1o8Slyy1P6SCLp03AyZuV79RoIEDk M454xJaIa+8WKbPE6WtcoKd2T8+Yl2fh90MCQtXvtIEDDo/5U8467OB8tNU7ICL/VZMiSH zVHSITKlrvWhXA2YL72zvHXoWW7myMg= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ctPU1P8P; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf03.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.218 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694625292; a=rsa-sha256; cv=none; b=kjD9aTm83jwmnRtfKXirQicBmtBJ8NlNiEl8F6CpEiKJaq82BXEGx38sP7k3y0wQPhWjaU lZGNrI2hF7eNeSeR7b5GZfHEctuEZ6ifFrAaQaqjFNB1oJu5liRoDLEuv4f8vFL/+4l8xj Cvj8KUnbOJ4UkZvqrzS82xWWFwwrhq4= 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=1694625290; 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=9Tw+dU2o5sfOuuXbX4FJVP/5BAKc6p0/nQ7cBmUzv5Q=; b=ctPU1P8PjzANEZxxufrmMlhavpf6MSmIQazNfrOE3GkL3F/j/nvFZZSEB8ovVp30c7s6MY CBKIkfoanapClJxqvm57Tjw1Y2V8mO/SFzga+leuMilEeLfLnIn8oak/3JOrpDVDpm0SEr N49MYNSPajkSI5g7JXyddu5oLOfkpEI= 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 v2 04/19] lib/stackdepot: add depot_fetch_stack helper Date: Wed, 13 Sep 2023 19:14:29 +0200 Message-Id: <74e0a28a38b05f27f3a4b54d5fa93213672fcd30.1694625260.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 2B32720008 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: x4o3guwubsneunfco44fkb4pxscu64aq X-HE-Tag: 1694625291-8870 X-HE-Meta: U2FsdGVkX18mi78+SmX/fISowkJ3pInqwUGcPPzp6tOx0Pt59dAe9OxioguvOF4yvyrcTx0F18ulsFVyQrQo1H9OO16aS3SzTWdKIVQZM/Le0ee+BScTJYVaNHM+aL5I/sNO1vPL4/0x+B0IRjLF7+RMn0RNdxz33h2nylWAlH67ofF6aZWHctitq1SB59ZoB1WXX+fSoeTj4cjJvvY7RP3R9UQp9Qn2Lce/9928XPnFms2H/vHfLXxmD4lcYM/UYhdv3jNK6mH4jkPN741tCkewzUdWbE7oha1d0i65MNJNmbOqv7zgNTUTj9y5fVoS/W/RzZ7a7w8Le8ksXpGdHZaC9iBJI4oTujB4szNsk/GmxOUYpr/zhD4mGutfdpb0Qq7WiTqZWSVVBg+TpqAx7a6SfQDeeXeTw5uL/1cMHdd6QRSkXEE+t4V9BCzZhVAnS/x8QmlDlfb4onPjZvCJMqou8CqMhIpcsCXBbeu3vWWaE8iCFG8NmMeK3zjYTdOrSIzhxKrZBjaTfW0vlCYS/i6EoE3WDZYAfGWls/qnTvahkeTKnNlqWFwEK1Mn8HBdeWKcm1xkpqToqj1VNsmFZ/otKW4pXArzS7qZ9XWRJUGZnCIgQsrx7Iqd7e3901yWflvDWyEj/fKurvgoq7dfJqpEwL5KqyF7/wO23LJCxw/pPRRUmaiQ3BCrIIkXmrJrgQ/LV5MNgNtZsY9dqUXj3cKLKaWTVLzf0BS3Z6b9jRxg3deUc0ATTpBGYdA3109PWN8xEv0qEmx9DCKhtvPUfTVFHXjCjK5Qx8BIJ6FzGafuffVN5QMxMwxlw+JYjsX6I70dY21bCFW9RGFPMfCz/l4rGb4NYAAoVtp+fWOMSr9GV71V51Kw1psi8irBVgZ7UJrJjiiD3PO3bUrqpuXX+7HjfIfFJQIx+YTqtYRQjpV9tAuf/pivG3mLVJzpy6na9ptLzA7OOhLOKDwjDTY lC9oZPf3 BUbZdSqxECepIT//Xi0mYIxGTOlhSSNpnS6ETmuAGeVVcEf4+DOJwBycxlz/Y7kXYgyFniFrBjTn6gbUOmgtccexUuzJODxyvzOIGgo9Uxni/s1d8ReSDvWKMnjliVT6xjQF2dfgXXyWjLJzdSXTDo9b+KA== 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: From: Andrey Konovalov Add a helper depot_fetch_stack function that fetches the pointer to a stack record. With this change, all static depot_* functions now operate on stack pools and the exported stack_depot_* functions operate on the hash table. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- Changes v1->v2: - Minor comment fix as suggested by Alexander. --- lib/stackdepot.c | 45 ++++++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 482eac40791e..9a004f15f59d 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -304,6 +304,7 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) stack->handle.extra = 0; memcpy(stack->entries, entries, flex_array_size(stack, entries, size)); pool_offset += required_size; + /* * Let KMSAN know the stored stack record is initialized. This shall * prevent false positive reports if instrumented code accesses it. @@ -313,6 +314,32 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) return stack; } +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(). + */ + int pool_index_cached = READ_ONCE(pool_index); + void *pool; + size_t offset = parts.offset << DEPOT_STACK_ALIGN; + struct stack_record *stack; + + if (parts.pool_index > pool_index_cached) { + WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n", + parts.pool_index, pool_index_cached, handle); + return NULL; + } + + pool = stack_pools[parts.pool_index]; + if (!pool) + return NULL; + + stack = pool + offset; + return stack; +} + /* Calculates the hash for a stack. */ static inline u32 hash_stack(unsigned long *entries, unsigned int size) { @@ -456,14 +483,6 @@ EXPORT_SYMBOL_GPL(stack_depot_save); unsigned int stack_depot_fetch(depot_stack_handle_t handle, unsigned long **entries) { - union handle_parts parts = { .handle = handle }; - /* - * READ_ONCE pairs with potential concurrent write in - * depot_alloc_stack. - */ - int pool_index_cached = READ_ONCE(pool_index); - void *pool; - size_t offset = parts.offset << DEPOT_STACK_ALIGN; struct stack_record *stack; *entries = NULL; @@ -476,15 +495,7 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, if (!handle || stack_depot_disabled) return 0; - if (parts.pool_index > pool_index_cached) { - WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n", - parts.pool_index, pool_index_cached, handle); - return 0; - } - pool = stack_pools[parts.pool_index]; - if (!pool) - return 0; - stack = pool + offset; + stack = depot_fetch_stack(handle); *entries = stack->entries; return stack->size;