Received: by 2002:a05:7412:b130:b0:e2:908c:2ebd with SMTP id az48csp2403224rdb; Mon, 20 Nov 2023 09:48:29 -0800 (PST) X-Google-Smtp-Source: AGHT+IGsTZ8eLJnUFC3H73uIBPOHYmHVfnBE3IOSZTJioaCEwYy+Y9dV0BrnSfHful5IFBaTMw4i X-Received: by 2002:a17:90a:c682:b0:280:9074:eb3d with SMTP id n2-20020a17090ac68200b002809074eb3dmr5966321pjt.22.1700502509097; Mon, 20 Nov 2023 09:48:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700502509; cv=none; d=google.com; s=arc-20160816; b=RDLbvy8qFgyvSPfEmb1SSeeBVA899aIInVdMh8g87+ti1P1LDJGMy9pCBJInjCvXP3 KGXYGYyr1iH8OJeWBwgGKvGMnZ8X/talH2l07iq9VJDvKr6E4rJulnudAOYnmXY4R+FO QmzAmrAuwMBQcWQtGXoeDEMCU5fjfhlfHkcJ6+1LehxMwpQ9e3oMPMF++cW8RtnPGcvq AfNiJ/CoR6DfoIpIpHUIK2V5KaQ2MGt7vDQ/sr7Ca/WRPtu5IZAWQipylMccpJVmtbFy 1ngSIMIQTKcpYTBv8DsHP/wJKNuvD1hUFvaVd1rxou6RcwCArNWhTc9+PAT35ubhgJRs xlmw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=4+Y9MGkz9f3yZSUENZCgBAvLs1CfO+kuipQPdiyYS+c=; fh=qWoxmPN1zRqnLSoCWGpfFsDtzJsx/SGdqx98lbP+Uho=; b=0NarAsASvTFgpgKFA9inVtxLJoCT7O8RLFo5BuA7Nniv33kmb6/09fC0fsuwuJKlwG AsFRi2f+0aZPCs5omgbO0W8iXKbcTftjYDqwsDUqfScWBwz+4fnBJ0IUyciiVHbqFEVO fGFxjd6ZsQQ42DrBHzo8i2MxXsr6+iNemSELe/4j8hUC8HXNg+VZ6ZRVJZ69Lk0El7Jt VciaxIo78Tg4zeqSzrTiofd9Qc5Gz5piHAEZmtzUBo2Uirx/A+UUI5cBequ8xlQ5Qj0r 3mrr3XHN7RFz5omEbXPi8NvoN+hGMSbTKoMNptCCdO+4jn7rW+ER/fNN7OGS+N5yECld osdg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=Ql85St5S; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id a23-20020a17090ad81700b002850dc3a3c2si5875199pjv.155.2023.11.20.09.48.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Nov 2023 09:48:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=Ql85St5S; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id 71CD080A850E; Mon, 20 Nov 2023 09:48:23 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234113AbjKTRrs (ORCPT + 99 others); Mon, 20 Nov 2023 12:47:48 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232999AbjKTRrf (ORCPT ); Mon, 20 Nov 2023 12:47:35 -0500 Received: from out-170.mta1.migadu.com (out-170.mta1.migadu.com [IPv6:2001:41d0:203:375::aa]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7F70A9E for ; Mon, 20 Nov 2023 09:47:31 -0800 (PST) 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=1700502449; 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=4+Y9MGkz9f3yZSUENZCgBAvLs1CfO+kuipQPdiyYS+c=; b=Ql85St5SjYl9dK6UKQo/wZBfC05xwZ8kz2f2txy485DwN0EC3gx7bgEAGlRsR3amTQZM6i Nep6MT9W4kXFWBOfaYnIaeXGDBapToM5EgH6SXSUwG03GDKgl7MtqpodPgiSEzEwBXoD0L 1m7pwV0+V5c6VKCswrtvLTavWAO/Q/M= 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 05/22] lib/stackdepot: add depot_fetch_stack helper Date: Mon, 20 Nov 2023 18:47:03 +0100 Message-Id: <170d8c202f29dc8e3d5491ee074d1e9e029a46db.1700502145.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Mon, 20 Nov 2023 09:48:23 -0800 (PST) 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 46a422d31c1f..e41713983cac 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -310,6 +310,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. @@ -319,6 +320,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) { @@ -462,14 +489,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; @@ -482,15 +501,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; -- 2.25.1