Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp1336134rda; Mon, 23 Oct 2023 09:25:55 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE6phxAT3m7PY68rpt030s3Kvio9MIUhKicflQsJeku+iL+MfPspod6vV+LQTbf5wfmxptt X-Received: by 2002:a17:90a:3902:b0:27c:f4a9:7e20 with SMTP id y2-20020a17090a390200b0027cf4a97e20mr17030801pjb.15.1698078354888; Mon, 23 Oct 2023 09:25:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698078354; cv=none; d=google.com; s=arc-20160816; b=xhBWIZSDW2wPS3Ien/I1v/f4Y8EB7H3647gh+kDilCf4PE+7rRTemNSlyoPkq3eXig cE9YnGgAZOL2QEvv41cZBSi2FbDm8HkTdIiHnE5XbCCrNZbyEh9kRjIr0dRp3n0wAOSY 5Pm1F426DeJnJxV7UwZJoE9epJGR7HW/07sc3ih4LBLDKXe7ZM/aS78ene/zN3NeCmzK cken4QTSWBBllaM3hDf6Il5tuWv7Xh7j1dxw3nejA4Jj8EGHYrdaHtN1zzSLIZ9Q1Zth bLQgi67NcYMu/RlY5p8toqOa0/Xn0PlR3B+4k09ITrBEWAzfZ35+G0RYhUvbHB5gtCbM 1gnA== 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=1heVLz37BMf9+qEekt7VobiQTsOf70yuv2uAcQVIL/s=; fh=nfQSbTp1dWHt2Ier1Up8UhVNdXOqoJJLNvTrpT3jJEk=; b=XixMF2UonNWL2QOwEMV+2FAZRaT63rAQNljobX+vnJGrOsyd4YTD6cE8DsMgYV+e6I MDBetIEPIHIjg7mMwtGyYmgiyQubtFHgVFtvmbXpR4EfYKEr6QuQzjX0SmqLQjLnKlxC TblHbtB7fMKvtjkr7ftJJE3SxMbL791wADnv1Lt47WpHcxY0hi0wcCDPXQfIXhwO4ucu AeUUvh+wyqXt4SvJ3cUCKi8+lqoosGvQMN1f95/kUuWPrGstlz/pX1SXqvM85LHI984k XxfBzsECsq8zEsJVzTWk7B5gVI9fo8Wt5GCV7H5SSMSvT7u0vUa8NDN3DjsrlZwjoT6F 7SQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b="Kx/VCgxq"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id n8-20020a17090a73c800b0027cef9ad081si9283820pjk.164.2023.10.23.09.25.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 23 Oct 2023 09:25:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b="Kx/VCgxq"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 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 fry.vger.email (Postfix) with ESMTP id 83BD08078608; Mon, 23 Oct 2023 09:25:47 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233629AbjJWQZU (ORCPT + 99 others); Mon, 23 Oct 2023 12:25:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39822 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233529AbjJWQZO (ORCPT ); Mon, 23 Oct 2023 12:25:14 -0400 Received: from out-190.mta0.migadu.com (out-190.mta0.migadu.com [91.218.175.190]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BE3FB10E for ; Mon, 23 Oct 2023 09:25:11 -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=1698078310; 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=1heVLz37BMf9+qEekt7VobiQTsOf70yuv2uAcQVIL/s=; b=Kx/VCgxqK8t1e20YOOctGoZgDJ6b2HmF5/SJtYpc40Yinosi40PcEw97gdh3Q2wSqoKaMv gCLt8HjJWSiEgQi1w+n4G7RMvsrZcD0tl+aQ+Iqp6FbWpwLJ3t5TLLfAK5QFLHK17pR18Y OUxp2UPDNk9ec8bWB7l49xpKBCrLcHA= 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 15/19] lib/stackdepot: add refcount for records Date: Mon, 23 Oct 2023 18:22:46 +0200 Message-Id: <21c7e1646cf9ae61123851c5f62bfd02f21f6bf8.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 fry.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 (fry.vger.email [0.0.0.0]); Mon, 23 Oct 2023 09:25:47 -0700 (PDT) From: Andrey Konovalov Add a reference counter for how many times a stack records has been added to stack depot. Add a new STACK_DEPOT_FLAG_GET flag to stack_depot_save_flags that instructs the stack depot to increment the refcount. Do not yet decrement the refcount; this is implemented in one of the following patches. Do not yet enable any users to use the flag to avoid overflowing the refcount. This is preparatory patch for implementing the eviction of stack records from the stack depot. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- Changes v1->v2: - Add forgotten refcount_inc() under write lock. - Add STACK_DEPOT_FLAG_GET flag for stack_depot_save_flags. --- include/linux/stackdepot.h | 13 ++++++++++--- lib/stackdepot.c | 12 ++++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/include/linux/stackdepot.h b/include/linux/stackdepot.h index 0b262e14144e..611716702d73 100644 --- a/include/linux/stackdepot.h +++ b/include/linux/stackdepot.h @@ -39,8 +39,9 @@ typedef u32 depot_flags_t; * to its declaration for more details. */ #define STACK_DEPOT_FLAG_CAN_ALLOC ((depot_flags_t)0x0001) +#define STACK_DEPOT_FLAG_GET ((depot_flags_t)0x0002) -#define STACK_DEPOT_FLAGS_NUM 1 +#define STACK_DEPOT_FLAGS_NUM 2 #define STACK_DEPOT_FLAGS_MASK ((depot_flags_t)((1 << STACK_DEPOT_FLAGS_NUM) - 1)) /* @@ -94,6 +95,9 @@ static inline int stack_depot_early_init(void) { return 0; } * flags of @alloc_flags). Otherwise, stack depot avoids any allocations and * fails if no space is left to store the stack trace. * + * If STACK_DEPOT_FLAG_GET is set in @depot_flags, stack depot will increment + * the refcount on the saved stack trace if it already exists in stack depot. + * * If the provided stack trace comes from the interrupt context, only the part * up to the interrupt entry is saved. * @@ -116,8 +120,11 @@ depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, * @nr_entries: Number of frames in the stack * @alloc_flags: Allocation GFP flags * - * Context: Contexts where allocations via alloc_pages() are allowed. - * See stack_depot_save_flags() for more details. + * Does not increment the refcount on the saved stack trace; see + * stack_depot_save_flags() for more details. + * + * Context: Contexts where allocations via alloc_pages() are allowed; + * see stack_depot_save_flags() for more details. * * Return: Handle of the stack trace stored in depot, 0 on failure */ diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 902d69d3ee30..278ed646e418 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -60,6 +61,7 @@ struct stack_record { u32 hash; /* Hash in hash table */ u32 size; /* Number of stored frames */ union handle_parts handle; + refcount_t count; unsigned long entries[CONFIG_STACKDEPOT_MAX_FRAMES]; /* Frames */ }; @@ -367,6 +369,7 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) stack->hash = hash; stack->size = size; /* stack->handle is already filled in by depot_init_pool(). */ + refcount_set(&stack->count, 1); memcpy(stack->entries, entries, flex_array_size(stack, entries, size)); /* @@ -483,6 +486,8 @@ depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, /* Fast path: look the stack trace up without full locking. */ found = find_stack(bucket, entries, nr_entries, hash); if (found) { + if (depot_flags & STACK_DEPOT_FLAG_GET) + refcount_inc(&found->count); read_unlock_irqrestore(&pool_rwlock, flags); goto exit; } @@ -522,12 +527,15 @@ depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, list_add(&new->list, bucket); found = new; } - } else if (prealloc) { + } else { + if (depot_flags & STACK_DEPOT_FLAG_GET) + refcount_inc(&found->count); /* * Stack depot already contains this stack trace, but let's * keep the preallocated memory for future. */ - depot_keep_new_pool(&prealloc); + if (prealloc) + depot_keep_new_pool(&prealloc); } write_unlock_irqrestore(&pool_rwlock, flags); -- 2.25.1