From patchwork Mon Oct 23 16:22:35 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: 13433184 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 DA721C25B41 for ; Mon, 23 Oct 2023 16:23:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E83756B00F9; Mon, 23 Oct 2023 12:23:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CDED86B00FC; Mon, 23 Oct 2023 12:23:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AFBA36B00FA; Mon, 23 Oct 2023 12:23:03 -0400 (EDT) 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 998276B00F8 for ; Mon, 23 Oct 2023 12:23:03 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 48E154033C for ; Mon, 23 Oct 2023 16:23:03 +0000 (UTC) X-FDA: 81377245446.24.C8F8794 Received: from out-196.mta0.migadu.com (out-196.mta0.migadu.com [91.218.175.196]) by imf04.hostedemail.com (Postfix) with ESMTP id 787364000C for ; Mon, 23 Oct 2023 16:23:01 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=MDW7AbX4; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.196 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=1698078181; 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=HoOK9Uvxs/7qubJ8D7/3BC32gLtkuyvkZG/hhWEOC+x9Vvz8GsyvU/Buhlma6LPgqXymqU 6rUlmOWWs2fp1p0X/8bOpdzM4C3zvgYwmFQS6+Gso1Erw0EY9LfmRzpRgY7Dwn1y/7M5hC Ga6mSiZLt3Kd2aUxdOM9WihNEZdPdiU= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=MDW7AbX4; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.196 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078181; a=rsa-sha256; cv=none; b=50Jyl9stHH7mX7D7lGKbnrS8k9XsyuE11K5DENs5J7DJFXjEDlMzYBewkESoasgiiRcmlC 6o1924C9+nS3ep9AKaRjHkyqt0dUFOfMyw/g0lytZfX2i5h7Up1gh0IWR9da0psnHaYQ5c VtI6pEcKCSycl9ZTUHAyTcgJhaArsb4= 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=1698078180; 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=MDW7AbX4eL0/TS7YLtRKEDg9+Xdw06lVwYB5nqof1k6w+UTAbp/tBlDVRi4NGfguchUae+ /lNPyD5G/IVsNQ4ZhW+fz7iuCkHqlbzWf5vQUi0pyT0Wn1XU8dxqiYv/rpGzHXYqU6FP/D 216IpW+w8WvbWqzOU6usNeJ95Qnlubo= 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 04/19] lib/stackdepot: add depot_fetch_stack helper Date: Mon, 23 Oct 2023 18:22:35 +0200 Message-Id: <48b71b2ff972088aacb3466d4de5afd46b6aa7e5.1698077459.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 787364000C X-Stat-Signature: ea9ujrmtx9r4c9hgm46umpr7e14shiqx X-HE-Tag: 1698078181-552600 X-HE-Meta: U2FsdGVkX1+k2/h8f9QetWYOpJUJvdKRLRUofgToDiRvN3ELafy+pHhvPm0RepT4DyAvBsWKZvtgtsr2wdJCQoIGqbd+RDDb8/rmTphjiQHc9DptXo1CtljaDz9huDd53UhtcFSVA89jVROBbvJ1BWohsZwn/JzkmH6MqYSKAXq4n+6kH/jrVIs1HfJlph2Khn6LQFlxecTnOhLRpM4Fk1gTYW6OmVo9FtKLUcBamkgtICdTHCyIaL1YT4Wv/qV8pzZ6ikSFh/y0oThP+KEVw4XqQWhZJ/1hnSzAbDGjoAxpAL0vZGFn6a5TdKDFpYaSS7kcvc/KYpnKxljGMViPA1i+uShpkh4rURpXHVHyKVmyT1lZbNuWzUcvEhzrkJ9PpIOom6UVgcMgIlYkGLEFrzgrE/MzKF3l+tCVVsFkcBlZGlE4R6yDYUeDibXMPTIi8BkbRWWIrMfUwQtrjyRUSvHJCmcqTQevycO48i/h/dalTXb5bdgSmpZtXWzl1B3T7ch3ql4k5iKuQ9ZrOx9vuO7cqsjQP0wq7FADiU6vTnoUCK6TnYexP9TSFqCjFyYnVPoKbnkVubfkwG0p3RuoII5TzY1i1cB5zLL6xJH4u05MxhbKBXYOYrLJMRG72AAGDXdPKLuXJ+tgBFIidSExFkcXbQX6ILvWPCMjv0EXbmMjilhUMZARgoY4n7E8Vltay+9jdW28XESFYC87yRYLRl435o5nV0TMVg9B7ug9BeUL+tScMfCA0bOv6R+ScBFo03AeP3i/afZ4sWFY6euYC5zukDW0T3VJstddrR9l8GPcDzXC7lcNxi3wbndt2d+8dpcqlxbp/nC29OVDogYP9qybgo0yk1pwqgbAQTYplrVyMlK/b8Mr8csYqHE7hR7TYM3owv/3aNJUIB+JESdentbxa5VQp+7ZfKG8tQ5hDcX5sscIEOgN4umQFgiC49vrUJckLx8Dxom5steCJee pzMAEwTz 0raacDi/f2mlA3YXBhnW78hGgls+KInz2PN752h5D2VEzBE02VItq9jeihEI2ewCya9UfzGkc8ve7xMy8rzQLHyvRop5sEsnxNnrAKrqtszUsnZ60GrJAeKhsCzds+rkUt/ICjbHZct3ZM8P6SSyO5PO2BA== 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 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;