From patchwork Mon Oct 23 16:22:46 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: 13433194 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 F2880C25B43 for ; Mon, 23 Oct 2023 16:25:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C5EE76B010E; Mon, 23 Oct 2023 12:25:13 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C364A6B0110; Mon, 23 Oct 2023 12:25:13 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9ED366B010F; Mon, 23 Oct 2023 12:25:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 858656B010D for ; Mon, 23 Oct 2023 12:25:13 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 63778B5C29 for ; Mon, 23 Oct 2023 16:25:13 +0000 (UTC) X-FDA: 81377250906.19.44009D1 Received: from out-195.mta0.migadu.com (out-195.mta0.migadu.com [91.218.175.195]) by imf18.hostedemail.com (Postfix) with ESMTP id 6741A1C002A for ; Mon, 23 Oct 2023 16:25:11 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="Kx/VCgxq"; spf=pass (imf18.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.195 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1698078311; 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=1heVLz37BMf9+qEekt7VobiQTsOf70yuv2uAcQVIL/s=; b=MOkbFPkbALjHiPuNqzU9JPgy+LrnLJp0O1AD/Bbs9+Jbc4bhaEniEqbCCxhoN6NcZ2kmES M0XpPJtz7zhkQV+Vo0DaDf7+Ktj+K1ixHRpDN2Oz8BdvD17BdhypqY8yujOSedqwJDrIQH T2cIytR3lEQ9cnsOMyvdwx6z4sFtRgg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078311; a=rsa-sha256; cv=none; b=eHxDc7pbXLL3PXxwC7Y+KbeRLjwq2LTsV4kJiJk+T0DknVT96HUCrEhG8hclEgq/wa9gsJ UXIXH+4S3LGMiDanItO6rGjNKSYbxek0bJVfFwE7x8Mzjo/kaJeYIXoA7aKWJwxalaXKnT N+SEWO8+a2EH6JL8gMW9lcBASub06PM= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="Kx/VCgxq"; spf=pass (imf18.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.195 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev 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 X-Migadu-Flow: FLOW_OUT X-Stat-Signature: zfnu9h8s8bh1336i7f8m3e8mzo374sis X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 6741A1C002A X-Rspam-User: X-HE-Tag: 1698078311-402067 X-HE-Meta: U2FsdGVkX18lTtRhgsss54mMWJfvPuPdheCLKlpt9f041JphIhqjgVoT8wv7Rr7NC3QI++wXIJR++HMJpwxfHoNLITdWtmrfoaMnW6poioDfag2YrEQT0M8kfu6QvA9qBOwc8oQVCyTC6IEAfkC+pwwXjZAtAUL2M5jt92jSaCvkkPxfWapr1UCYbKbNFW+7kxe4YQA3TNKzLTzehgqkXlc79mhLwec8TPHcw/qxIpefp1TJk1TRMb5Hg6dXzEPAFJ5VNRuoAY654FVHPCdodSlwkLDjd9spoOvtfIExDyxfGfGuYviBH/eWc7vYjuroQCRgq+L5TIeSYrj8E3JILgyJPLC9FRD1lWeahXDgit+OlpSqM2E2y1ZSmG1d2j4nhvEJ2rQbTP81a4fevBjyJkn3dYh8YAS81sldUOaD9aR0aDfIHy5LfHnFNATFR1sxW6S8nyBfeu+SYLcy2VSGhy8U/ezsuf6x3n1I2Lo5SGS3ir5tKicRophfE27Ha3Hhy61XL7IZDiON4j99hT2MnE6YwyQ6shFF7yXMDN23rnwcVqt83US0GJxX7cOdGcujAZVZbH6izI61yVOWUWwXcArr2OQe02Oki5gMGUUi6mgMiAQ/pxuQEyqNBVVBkiSkxA93I4xXt+HZov56qLlcxDLFw0jRPoszb1j8CjpcE17fKM99s4ERVhepV4UOrxmBTWJj3VvlcTAVPEOSMI9kQ/zLafOhoERbyb1zI4mLCT7d5aNeifJc8CTbl5ZqIdNhcowRAqowX6Osz8Y1QSs1ORCAIFNX4sA5poo5zakbzMfUStsIN+OG7/Qyd3G2lXNZn+GzMSfzAk89hI18mkUCJ0LOgbDV6HiIIkyQGj/czYGeQcmUMcv8lahepTSoK80OcVpYBLMEcWl1QHR/RErn+azcXzqyqJAEAa/I5Y3zLGncqssmEE8c1wva2SrdVIEGqHipSFNu2Q6UF7bNEa6 e+6sbWDV 8VVR81R+Pjpt6QKn4uxDHwaMPaqSSq38KPGBYEq060S2731ngEC3sOqlwNiuFbpOsG7HVFJ/5wQyFQqVdIPm9OvLrlwTQqoxzKPFr 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 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);