From patchwork Mon Nov 20 17:47:15 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: 13461831 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 C1188C5AE5C for ; Mon, 20 Nov 2023 17:49:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8BDAA6B0370; Mon, 20 Nov 2023 12:49:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 846326B0374; Mon, 20 Nov 2023 12:49:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 673046B0377; Mon, 20 Nov 2023 12:49:44 -0500 (EST) 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 4E93F6B0370 for ; Mon, 20 Nov 2023 12:49:44 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 1B488B501E for ; Mon, 20 Nov 2023 17:49:44 +0000 (UTC) X-FDA: 81479070288.28.96DE346 Received: from out-174.mta0.migadu.com (out-174.mta0.migadu.com [91.218.175.174]) by imf21.hostedemail.com (Postfix) with ESMTP id 2D3791C0003 for ; Mon, 20 Nov 2023 17:49:41 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=DLuotPQZ; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf21.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.174 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=1700502582; 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=taQhm6+sqq7C99vuNmST7zKoEFBrlbi2ArfSWbY49ok=; b=h7nhDBgXQXaQWkkyWXP3gsDgBlCl0+JBTh1r1/NoM7pm2IXrHMNOd26VGIiqh1XheCHNiT igUAYuJtCfaOX7wYpOiqjKnb5sP3bGIBHLPX2XmKCRuMfaikS0RnLRwDv6jwhV0V5hP/iW wELZn/uod1XJv87X6Wb7MzF1c/THvK8= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=DLuotPQZ; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf21.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.174 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502582; a=rsa-sha256; cv=none; b=uZeKzud/hIeJRwagy4APXnD+G5E2Xozl9psxEVExf0unsDHNti4a4XtBtuSUKnoSdBzpGC dkgheQwIHH1+VHh79JhjHb+4RJ1vBsuCyVxIaOrLBnsoIUj4ksEpUVhQwGSYcOVJlO+wrH dtZnR5dahLrEnJUdJYoMF0m1JenoKwU= 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=1700502580; 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=taQhm6+sqq7C99vuNmST7zKoEFBrlbi2ArfSWbY49ok=; b=DLuotPQZ2ya2ze/Uk9qdks6Velp5g/ufNOpAnZ3F0CTG4bBuTmqBJZX+8HrA4Dqfb+h/XH cXG4e43rc7TxqEGmM94O/sylDTL5wKtysxQVD2NH08lAvJQHMJZz91DKB754x+wRKfj68K mQIu1A/BqCWkTls4JakGGQmnxTCxoCg= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 17/22] lib/stackdepot: allow users to evict stack traces Date: Mon, 20 Nov 2023 18:47:15 +0100 Message-Id: <1d1ad5692ee43d4fc2b3fd9d221331d30b36123f.1700502145.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 2D3791C0003 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 56j9jkke6x946sf1cc7eexgw6gbzb6yj X-HE-Tag: 1700502581-372242 X-HE-Meta: U2FsdGVkX19ejo45ZklWFySp0+XyAZDeFgMrbSEsrOFY2+MMGliMO+lFZpB1VCyK9EWOrZI0SaFZkWiwqoqmkTFlFBhRTFOKKCCPoa1as4VSH/yjUPDrSGBWRZ07l0s0Zds70Z+/RgwrYwW/wY9YnFLqs2+aJVtWaZL4ky8hEQFt+ZvWh80BtbGBMLu+GgBApeDm8NsaGeZaxbPXr5Ai8nveBo+GJJBLL4DMjyCJ9xXWPwCNdfPqCH+QVqHUC9OMKuV+fUAVYzGktGAXL1hgOndmMwpN6DEyOzq6fkkIuHXM6RUk2FHogzOmuP2YyoS1zIOrDQG4Nlg42y/Ed6yJy7Gl8ToMzYq3npIdwxa3mLUo1ASwZejL9/sVVgtUv1sb0RDPMeB7x1dJ2qB1oJ3a28EKbv+EgtT+7eTMgKEU92jG5Y7S0fkhlCR40bxPNVunnA/oHKS0zTj0OSmdypoinSTtttAyB+POa8gCG6L62bGN6eWKbLtghpsADAqS+zKjx9vFrq3hPBaDNp1wILP8tMyPctwZoDRWC6sXQYKOL2esx/zJx3O8CL10+jYMaP7ADKCE++6WGJUpNstmVSZ11ohDL7N0uqaHA/zfxvyXxpyjzAaiaY6Nmw1tt5VJ2RWjr3uYjGA22UwDG5PNey2iksUULVCJXiNdwZQ0pwqksQmZpNeskfyfz6OuN96pzZky3CHc1q5DPPxcJj4rETGprwl1KK5HMjzuyWlaBgNqVQhPydGODlBwPDwd+n8fvxIIDxjHu4BUtxYVXb6tn0I8XEEpeWVwk1/MOPnmJTnV3V2g2RgeTNVE30XJUvJOj4AlI+5CmFxoaVX8k9jBuoCAa9rJGNN+07DWugKO83ndgDSX9fWsvm2TegKGH9M48I/gtk3Yv45C3eVA2aWhb9nroe3wTP0mH7ZjlyblT9OmzxHnU1qhtBDJtUY5E7g8xcrDWL5e2sx2prh1COy2Syo r2ltwQUR fUGBY/T/6KlHI9Njf8BrcnQdxhfW8tbSgnt0rxlI9vHjhB2aeIp27MvZprObTb4sh1xZ4 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 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 | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) 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 911dee11bf39..c1b31160f4b4 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -394,7 +394,7 @@ static struct stack_record *depot_fetch_stack(depot_stack_handle_t handle) size_t offset = parts.offset << DEPOT_STACK_ALIGN; struct stack_record *stack; - lockdep_assert_held_read(&pool_rwlock); + lockdep_assert_held(&pool_rwlock); if (parts.pool_index > pools_num) { WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n", @@ -410,6 +410,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) { @@ -592,6 +600,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;