From patchwork Wed Sep 13 17:14:41 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: 13383629 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 9DAF3EE01F4 for ; Wed, 13 Sep 2023 17:17:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B5F6F6B0275; Wed, 13 Sep 2023 13:17:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B09956B0276; Wed, 13 Sep 2023 13:17:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 95E126B0277; Wed, 13 Sep 2023 13:17:06 -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 7CFEE6B0275 for ; Wed, 13 Sep 2023 13:17:06 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 586251CA64E for ; Wed, 13 Sep 2023 17:17:06 +0000 (UTC) X-FDA: 81232229652.26.E10B19B Received: from out-228.mta1.migadu.com (out-228.mta1.migadu.com [95.215.58.228]) by imf04.hostedemail.com (Postfix) with ESMTP id 8D6944001D for ; Wed, 13 Sep 2023 17:17:03 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=F2Ii6+s+; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.228 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694625423; 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=AuXPIx9M1sa3aovsp0XdlaM599Z45TGxV3EusWHlaB4=; b=QHGRfnfPeo2LGWbhSH6ZgFSZd7kX76//2/bZISJ6pOlr5guY0prkIPsQMmlRYH2sigO93d H75IK3jzODSDEw2RpN/2LVxmU7uDQ3wVRoGoEApIVBJ6oP4hUDampL7HmJjTSPvIHzmCu/ saN5rtBfFfZwa+08wDuBpOWD5zLCQ/8= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=F2Ii6+s+; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.228 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694625423; a=rsa-sha256; cv=none; b=Rj/kclVY9s7KKu5JNw3PuUD1jf+uMpaK02Wo06GZxfvEYimSa98+MjzgDqeI392Ghy/a88 mCsiOHeRdyzjR1IvftPA57VmtpLqiE9da21/xtcorNrUyMt8kH7FIK2YIt3JHSaSAnA8Qu vdJBfkRmGlk0ZGJF7x+n7A9ZfrEkET8= 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=1694625422; 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=AuXPIx9M1sa3aovsp0XdlaM599Z45TGxV3EusWHlaB4=; b=F2Ii6+s+cdiC4H7z0xYGquHm32HzsvJdDEssh13Gmfxh6dgxf0ImSZW90i2I0Ul8XHXFZN EabRGGiqHKx8/fjEiRgDu0SjJmNsgtQaiOLP1FFmQ02FkMsAmcJU65Zs5fSzoSf3rveOpt b+/xe3k7MacpwOlBvRZDIYPeX/d3pF0= 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 16/19] lib/stackdepot: allow users to evict stack traces Date: Wed, 13 Sep 2023 19:14:41 +0200 Message-Id: <1a3ea1e6b82f95eea644875756e12daba6c4cc7f.1694625260.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 8D6944001D X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: zfyfywkippueqwdgfarhynr4btkzsdqg X-HE-Tag: 1694625423-46454 X-HE-Meta: U2FsdGVkX19RjSBG5Cooa782mxyO8tB4I+Gh7mx9Z7zlfuEZu5b2UbRrYK8T2v0pWUF3GNiIsGPQ1C4feovdiIGTYT6AAj7dbPzem0a9tWUQjHq5mg5PEUO+AB+SGQqM9oCtw4nDB5mRVkNiiLfiMUkhoaryKTGsRHSu1gKKk9wwY2bUKofnCGoKRCgf/jM2gIgSjajy96mYmt7O5eRdGYWsoP+kfqsg4Fvq0LgBvEh74PKY3UuAiRZCj2oLeZwIP7MCHrwhfvyOt8rKEtsVyoiA7CTeu2FCvjVh+MwhsBkwYzdgEq96fOmOHjvvNmQ68J35ximgBRBT2TiJ8cpaXrzQIqEvxk1oB2+hwtTYtloi50/Cx5KG/xtYFGnLQ2Sh8rmWLziGiH7su9dVhCl6eQCjBgX1cC0kA1D6NZvZMnpLZ0pyyC6ZDMM2BIA3MkjgO1KmDOqvrynlqbUjuJUgVkAwJAQNMOopjMqN4USP7ocz9TuzzTxB+/kNPiW+o3M9uUdZKmovZZUxxLfhJ33LTkzIcDs/icNo7cKVLHE3MXDxEtVVxcyVGNvi4JyRnx7wPD0fYMNuS1igXcRQlueq2tOoLNYiQ2aulGtA3XIH+6Mi+QjWXTzy0o+oJWBdQT9gzBS+oOVVMtW1Xjb5ckSeZBccf6SLK3fXRApmG1tGUYcCJrkCR8T4OXt0kuadxy0aRQLYiIV4FvFXmqxSzOVeEFCgPnJZZT4/2PIrIja78wxTFp8Skk7dFb0Pa96anrVF7zhcF1tgCfMQADlzIzkTVXiNX1HpNHK/CT4lBm8CfUwQLEN3w8MMi0gLWzLYI6/KOOZwH/XT8vbiSLYAxELhqIv8Wb8gNrv1IGaEBcxhd5sE13d2PSx1V7lSj5eah8Jvi/8CqEerr8HOzW4jp9oNwQvSoA0TpJEiC/MOv+zCd/26fE+7aiRafKMSgLvGatutOAoRTz5pBjn9PAyJJCF yaF8fuhB 8qlGe4DLiLtKfLbHoyv2nvDGv8v+GjeS1B/HJdfsRL7qgeRHONwBeujgcjgrNi8BWcUbWdz4biNpCDSp7jX9xtt9QQO1piN0IVlNN 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: From: Andrey Konovalov Add stack_depot_put, a function that decrements the reference counter on a stack record and removes it from the stack depot once the counter reaches 0. Internally, when removing a stack record, the function unlinks it from the hash table bucket and returns to the freelist. With this change, the users of stack depot can call stack_depot_put when keeping a stack trace in the stack depot is not needed anymore. This allows avoiding polluting the stack depot with irrelevant stack traces and thus have more space to store the relevant ones before the stack depot reaches its capacity. Signed-off-by: Andrey Konovalov --- Changes v1->v2: - Comments fixes as suggested by Marco. - Add lockdep_assert annotation. - Adapt to using list_head's. - Rename stack_depot_evict to stack_depot_put. --- include/linux/stackdepot.h | 14 ++++++++++++++ lib/stackdepot.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/include/linux/stackdepot.h b/include/linux/stackdepot.h index 611716702d73..a6796f178913 100644 --- a/include/linux/stackdepot.h +++ b/include/linux/stackdepot.h @@ -97,6 +97,8 @@ static inline int stack_depot_early_init(void) { return 0; } * * 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. + * Users of this flag must also call stack_depot_put() when keeping the stack + * trace is no longer required to avoid overflowing the refcount. * * If the provided stack trace comes from the interrupt context, only the part * up to the interrupt entry is saved. @@ -162,6 +164,18 @@ void stack_depot_print(depot_stack_handle_t stack); int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size, int spaces); +/** + * stack_depot_put - Drop a reference to a stack trace from stack depot + * + * @handle: Stack depot handle returned from stack_depot_save() + * + * The stack trace is evicted from stack depot once all references to it have + * been dropped (once the number of stack_depot_evict() calls matches the + * number of stack_depot_save_flags() calls with STACK_DEPOT_FLAG_GET set for + * this stack trace). + */ +void stack_depot_put(depot_stack_handle_t handle); + /** * stack_depot_set_extra_bits - Set extra bits in a stack depot handle * diff --git a/lib/stackdepot.c b/lib/stackdepot.c index e2c622054265..56f2abc03717 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -398,6 +398,14 @@ static struct stack_record *depot_fetch_stack(depot_stack_handle_t handle) return stack; } +/* Links stack into the freelist. */ +static void depot_free_stack(struct stack_record *stack) +{ + lockdep_assert_held_write(&pool_rwlock); + + list_add(&stack->list, &free_stacks); +} + /* Calculates the hash for a stack. */ static inline u32 hash_stack(unsigned long *entries, unsigned int size) { @@ -580,6 +588,33 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, } EXPORT_SYMBOL_GPL(stack_depot_fetch); +void stack_depot_put(depot_stack_handle_t handle) +{ + struct stack_record *stack; + unsigned long flags; + + if (!handle || stack_depot_disabled) + return; + + write_lock_irqsave(&pool_rwlock, flags); + + stack = depot_fetch_stack(handle); + if (WARN_ON(!stack)) + goto out; + + if (refcount_dec_and_test(&stack->count)) { + /* Unlink stack from the hash table. */ + list_del(&stack->list); + + /* Free stack. */ + depot_free_stack(stack); + } + +out: + write_unlock_irqrestore(&pool_rwlock, flags); +} +EXPORT_SYMBOL_GPL(stack_depot_put); + void stack_depot_print(depot_stack_handle_t stack) { unsigned long *entries;