Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp1334921rda; Mon, 23 Oct 2023 09:23:39 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHSyzepfyoRnxjJ99/rL9x/p89pmAn709GoqkEyaMogOP51Yg1QhV1doAl+ZuBUoSOzsxvQ X-Received: by 2002:a17:90b:e0b:b0:27d:37d3:6e9e with SMTP id ge11-20020a17090b0e0b00b0027d37d36e9emr9792814pjb.17.1698078219219; Mon, 23 Oct 2023 09:23:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698078219; cv=none; d=google.com; s=arc-20160816; b=eVBPI/mdR2bt/wdO8ZfX9zi58qbWIBjc8G951+zKNUDGKmNa7uJvM/lutSpx2GZZfa ah1ELTBY8p5m/1XzzDl3uVurk1z7mtdwH/edvE/b4mJNJbJ/Pc3bkmYxFBHVc6ftyYAj OJpPbsX6/bFUTqcYZ45eMKEIC+v5dJTcUCblhy714Jl850waABMrQNGp6D0gPzmdkKE4 6MZf7KyUo0lz+1xeYgGehfPt9XOXt94FEEWbGCF1nMzFJ6n58nzy88pqr1dNhPT9kQpW XDYWmeKg9hXJ6CpjN4A/H2yentE58b6pzXDrbktL6CnHNkfUQjuZ9oBCjdtnxLiwahvl tybQ== 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=9Tw+dU2o5sfOuuXbX4FJVP/5BAKc6p0/nQ7cBmUzv5Q=; fh=nfQSbTp1dWHt2Ier1Up8UhVNdXOqoJJLNvTrpT3jJEk=; b=pTJzKtBJAnE0w63cM8XPbko1atLxTV39GFICpNws6L7V6wpq8nFVWuvwQnZy8piCx3 RZXn+9hOwjLQCo9kcWlyapRooV5wB0N8NRheOMxT4KBWQ4WOEBTOMj3mg2jWBiainY2i YRiMnQCK4/yg0MTH9zYCdJPOyU9Lei3KY4n+ocO6ZcXoy9tuYqGxpjC5+SRzFpZPrQ1z RD9Jhy9r8WF8zTAIq/IF5S9xNfdoq+5xQlk3a88MtP7YgzB6X86XtL6sCEq/GO8lHPAX AOs5QMWtb9pdySDFVRYMVEC/7oItKBvfvpEOwdyWH+5NOKnqfColnk7BgMUGPI6nkppm UGLA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=MDW7AbX4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id w60-20020a17090a6bc200b0025c1d114af0si6788284pjj.93.2023.10.23.09.23.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 09:23:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=MDW7AbX4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id 8BF5580A1669; Mon, 23 Oct 2023 09:23:34 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233385AbjJWQXT (ORCPT + 99 others); Mon, 23 Oct 2023 12:23:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38378 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232212AbjJWQXG (ORCPT ); Mon, 23 Oct 2023 12:23:06 -0400 Received: from out-191.mta0.migadu.com (out-191.mta0.migadu.com [91.218.175.191]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AD7F3FF for ; Mon, 23 Oct 2023 09:23:01 -0700 (PDT) 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 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-0.8 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on howler.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 (howler.vger.email [0.0.0.0]); Mon, 23 Oct 2023 09:23:35 -0700 (PDT) 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; -- 2.25.1