Received: by 2002:a05:7412:31a9:b0:e2:908c:2ebd with SMTP id et41csp3386912rdb; Wed, 13 Sep 2023 10:22:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHn3dp2nN6e1nhl/au3IcImDLudLpqniQM0czDGFg9bEMdguQKIRSX71N+Xmb8rXksW2Juo X-Received: by 2002:a17:90a:9c06:b0:274:4fb:3610 with SMTP id h6-20020a17090a9c0600b0027404fb3610mr2865760pjp.47.1694625775624; Wed, 13 Sep 2023 10:22:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694625775; cv=none; d=google.com; s=arc-20160816; b=phmvEf32hTDzQMJEm8CZ/VwYjXSZrJMiUCoWdoqzG8kXN3CfEw/yjfitSjjUT9F9+D iFYhJdfwZgy/AVOqSQrfk/Ww5nu4gAxmLo2EWz36ltnzkQuvpD+zvLu2r8MjCwkk7Zrr YEc55jbNsuoaCRerLXEZY89K3tk/R5WRwKzuRz85FHST6kXSH9LklAVPR+yUOza+wDPE 4Pf39HaFTfHgUbVsXfeVt/v0rhvwgPSZkV/VpP9tG3g5ZCVsenf4Rg9oMJHt5V/YnNYu 0hX1mCm+vCSVph88tgBzJVhK2R36yYdDfGi2lZEbAuotmWMLfR9v2YCt99NCjF9yDfQM 56Kg== 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=vzaEtrurA8wCF5OWnZYpyydoKMwiRCf4Oe1IvdW9gSjG1gqFf2iT4BV/1xoKTed7cN DvTSWolKIwg36UenqYKE7+JEYgHtQ0GYbvOIGcg+YdB19xqCN3582tqU7rOXaKflP0dB 9F13SyVB9A6EfovcOp//j4QbpTyoUOgS0Oy8KILOUZ37VUJFc53zulORMlW+ACRbgp7L SDRSne/mMr3Q4qSMYwGiuJ/jL1p8EB7+fsupTFiCPgQp8hdmXfZkblIG3woDbDVES4ZH Gm3T0cmtW87J6t2CxIR1zTvDmU6uiNrADCrcyzvYCWKIGY1Pp+0VPukQHTaxCYLOx9/5 R0UQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=ctPU1P8P; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id dw17-20020a17090b095100b0026343b62cdasi1906160pjb.154.2023.09.13.10.22.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 10:22:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=ctPU1P8P; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 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 EDC1C8022A98; Wed, 13 Sep 2023 10:15:08 -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 S231243AbjIMRPH (ORCPT + 99 others); Wed, 13 Sep 2023 13:15:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39108 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230057AbjIMRO4 (ORCPT ); Wed, 13 Sep 2023 13:14:56 -0400 Received: from out-220.mta0.migadu.com (out-220.mta0.migadu.com [91.218.175.220]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 568399B for ; Wed, 13 Sep 2023 10:14:52 -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=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 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT 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]); Wed, 13 Sep 2023 10:15:09 -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