From patchwork Mon Oct 23 16:22:32 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: 13433181 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 4C59FC004C0 for ; Mon, 23 Oct 2023 16:23:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1D4C36B00F5; Mon, 23 Oct 2023 12:23:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 15E1B6B00F6; Mon, 23 Oct 2023 12:23:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F3FF96B00F7; Mon, 23 Oct 2023 12:23:01 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id E443C6B00F5 for ; Mon, 23 Oct 2023 12:23:01 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 8CC60120967 for ; Mon, 23 Oct 2023 16:23:01 +0000 (UTC) X-FDA: 81377245362.12.A3BAC5D Received: from out-201.mta0.migadu.com (out-201.mta0.migadu.com [91.218.175.201]) by imf29.hostedemail.com (Postfix) with ESMTP id 9C6CC12002E for ; Mon, 23 Oct 2023 16:22:59 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=CkRusZIQ; spf=pass (imf29.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.201 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=1698078180; 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=dA6zDrMISQ9ixV+61d0CbJSFq6N+6qPUyMdB5Zjx2Sw=; b=FWT7B0Ux8jaQKnSFTbeLJIpkBb3RGYk97cabsCcEzTUYWROJrvyEmrfNnIlKJ2Ro4pbXUs yFzbC1veU40FW5ZP8ufa3d2M4JuZD0fazfcyPvAo3+UyZY88/wbJwCfgOvqiUEmQSR2Wam AvVA7LXrcHqKtSgM+jvCKcWOSWwQ0CQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078180; a=rsa-sha256; cv=none; b=5nibMhaIpRYr6ASO2btkyUwGipLIwhTNJn8PsxfvZ3A2exUueIkc2QRxwRMte4pZLhKzW4 1mMZ86R+Mz1cTRLz14BbGcwNfXx5hBR40vp+qU0GuHwxephnChCjTsoH0PC7aJlHj9VLfA wTqpNWZtwmSRWrYaa1zEcoFgU1KEivA= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=CkRusZIQ; spf=pass (imf29.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.201 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=1698078178; 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=dA6zDrMISQ9ixV+61d0CbJSFq6N+6qPUyMdB5Zjx2Sw=; b=CkRusZIQKT1eG4Gl5cCrRUWt5DUG+IA1Am7cKqEhxJvtxvEOtNHOGfa8oOTjOvRtsLmZuh DIpreTOPrJBHIpWysqvlzeB2EOS4cbEOWO+vFGBjLCZ+m4G6B8FIZg7ByX7aEIEwmgDxIB IhsqPDQSxHpXeKy5blW2EyaMIQyiiKs= 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 01/19] lib/stackdepot: check disabled flag when fetching Date: Mon, 23 Oct 2023 18:22:32 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 9C6CC12002E X-Rspam-User: X-Stat-Signature: jzjzgws3w19qtqc3g1ig8nsnoa9q9dku X-Rspamd-Server: rspam03 X-HE-Tag: 1698078179-476159 X-HE-Meta: U2FsdGVkX19bEy91wrCx1LKJgZLFlckU2/l4FaKLYqE9CNskd/+IQ38aT0e0JZ7WmICVSMNkskGpza/INq96DuNmC8kf+Fhr6F0hsZENbXOwVhgC3VPsAWIVDK3rbbg07Egny8AR7082aJNgRWLTgUJlIrVzaI1/IjjGRbiLRrXKpmHBv5WFkzpH3tC+a4I93c2ycofdn0iakHsG22qqnIYTdVO9B790JCOIAzvaW8pY1ky6eZBIX7G6Z9StdRE5rx5eUp68LpddEJ01AIxPS6B1Cd4mQdcZod0msb9wZGl72B3IETGIw0o/LPNJq195wxzWlRKT//KISXrb9Llh8/3ndoj5FkTCJFHQXJBx1CJdyr553AC7hhKRpltIQ+449Pw/NdlmqpJ5y3ej/AQUjht8yBXQB8juX2DmCPPdCI0tmhv7PQkSoXlixQEAwze3zyHiBh+2aJHXas0xkGFv5ljifkEo4TSY7KvowrPgtkOi9aE+eJs9y031LXlr3k+NheFtTsKGrsj3hBT5TULHpjr0ajVs8Qo69g61ylUD+mfSbdD3Pi+9B9jxSqB2NFMGTUyFcHj/r+JSu3ukHQPac+7vL+xdEDG/26M0HP9e6vwSNTFy30ePUGy0/2SUyUXxYBQHpGVxSaqh4g5RsYwS5m6uspe+YUYQqAp/RWhNci2R44AedNXxQ0+CArAToJr+CBimB4JNjkxIdMV645cITn4UeI+6qBbizm9mE3xzZciKYVJ49PpLFDKaautA6E9hAav8kp3hCfdkjIagja7oUjcYStf9kQTDf0mo6OS5MD69iFS3q+MLHQaJkglFbfWufax6x7kQlvfBZk6kFBOp8Xvi/qrkUzcmveb331tqeftPKc0k9y8+o9ctkh5QSo4m2nAy9nIjUXIXb9HgxXE8e5P3aofTi+Tmq6rsxPw3w2x7ahsl8xJftTnnFilyMydFUPb1TDwFDDUJ+7uMtL+ xufNRHnJ DO5U4wV6pinHAmih/bNEkKYyAmMGp8ufANFJLSesMwlob3ZX3DgWe5WVQ75jV3agywOzno8uHwzPJXoEUzvau6G361xUORaIeBEA2 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 Do not try fetching a stack trace from the stack depot if the stack_depot_disabled flag is enabled. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- lib/stackdepot.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 2f5aa851834e..3a945c7206f3 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -477,7 +477,7 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, */ kmsan_unpoison_memory(entries, sizeof(*entries)); - if (!handle) + if (!handle || stack_depot_disabled) return 0; if (parts.pool_index > pool_index_cached) { From patchwork Mon Oct 23 16:22:33 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: 13433182 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 50B47C001E0 for ; Mon, 23 Oct 2023 16:23:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9C4C56B00F6; Mon, 23 Oct 2023 12:23:02 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 926C56B00F7; Mon, 23 Oct 2023 12:23:02 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6B4666B00F8; Mon, 23 Oct 2023 12:23:02 -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 5DF536B00F6 for ; Mon, 23 Oct 2023 12:23:02 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 16C1580369 for ; Mon, 23 Oct 2023 16:23:02 +0000 (UTC) X-FDA: 81377245404.10.E9BAD3D Received: from out-198.mta0.migadu.com (out-198.mta0.migadu.com [91.218.175.198]) by imf21.hostedemail.com (Postfix) with ESMTP id 33C341C001C for ; Mon, 23 Oct 2023 16:22:59 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=BwPbMNBq; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf21.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.198 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078180; a=rsa-sha256; cv=none; b=O57KPqxdSmhNK7WYamfhMT7+Mcj+vRy4JhiwJitVt53/eTc4ZeDq7LpPMqcaSpxSMDwS3w 0Fet+dV/KQH/ZvEgZg559/XKRpfXTz5Tve48TpXErMjLdUFBRo8+BcooDy8b6mRQyaj2QS JFdnYLvxd4Cxr2k8xnWKe9gxn7JG5Kk= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=BwPbMNBq; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf21.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.198 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=1698078180; 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=hlykGlfL2an/VgWBLR95t+wE6CpOLlFaVT0cHbSVLN0=; b=6Ee2X4hrG7yRKQgkpyuruJHhSkWD0xROiPzJZkJqjQ7FipkMacAwqWRyNi9etGwjpRyms9 z/vlqGo13dAtdkJIjJdJeJWDnJyu3n2awo9H1hAMPkwvZ3k2PqcsVW1LzILlDLkVqujUSn ICqkj62kOTiB8d2X/2qNyZXwlt/I8go= 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=1698078178; 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=hlykGlfL2an/VgWBLR95t+wE6CpOLlFaVT0cHbSVLN0=; b=BwPbMNBqQr8eCi/2bzTTF8duZP4VRRfQUHs2ug+CAfTzdfxW5c7tGWmaNI5EFjq6IxzJZq SfjwshdOrp6OB9NwpUh9zr0qnIhgsa5b547AMebg2LpMrGL6e3ElCdDIR06kr2JMooKxRy iI329x7c9WbbQOKgKwFWyqcU/WVnUL4= 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 02/19] lib/stackdepot: simplify __stack_depot_save Date: Mon, 23 Oct 2023 18:22:33 +0200 Message-Id: <6ff0d1e89e50ba74618eed30fd3170dc78decea3.1698077459.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 33C341C001C X-Stat-Signature: cxhdim89rxmezgnoyssuceqk87rob7ir X-HE-Tag: 1698078179-407099 X-HE-Meta: U2FsdGVkX1/pNABn2p0KehIWRJ6vJBWalCW1+qHDtibPFTCCqQnYR2LnQ5EtKVy+ueYPd2gefJskX5P2mGeH1Jte5298nnoGYu5lK/3ZwAEBkgPnRGnSyEb9/4slJbH6eHjKz+XFyRSaDyPjHvYkGtINE/ejg5iQdH+qI7+lomEPvjfVSta/DDKAbmbAzc/I+di4nR7h1v8nCkG39qqefT/Z9xivF6mgmJD/i34j1H+nu4z9vjwUu9qIG/UZrluBD4/1S+kBzozzV2jMyNovFbTwSIjb15YKr0FMkSpcKPUVR8b0/Q1jZzc+lFtX4NHRzId0BY7hdso1n5nZbi1wm0jcYBsZGnaC8IB33+dp+UTfKJ03Bi7nujMPoPzY78sJx8/44mo2XiEmrqaz9Y8rA2I3SkpCXBZdZNdQdEkWMXOSjzYGDlkGY1h+HbozjjPVdN80WuLFQQmaVUksBWfTdCR8f4fhJ7nN/RiMVFosKWe+U7lWBwG8NeIU7A5esd0aFFivzdggnqkDgYvEbzRPHcQL8Zi6YcOsjwGjCyMrBOYV2jbFkWjyAjQYnZopz0u0HakXiEOcP0Knm1mlWAxJzsL1onwZZASua5GJNzwI0JzFKlUUZyXCL3ReABwn+qDLvMi6mf8dw8cP03qafUSeEiJq0I/x+HWgJ0CAG5ty2rI0x2wkPzPWHlTd6fZncxJiVdfdabjjp+VjLu9rr8VdxNuen400yaaNtAivD1oO0eJhdwLgQbW2GS+umReoLxAiomrNlQlDB0iaYW3OhyIhG+FSrlLz7DwAB1gIIgMWGykYjnmbz698kLXK1VDv+nlxwpUQ8FrTNf/zbPkKaAFZwrwA+chnx0u959uxyDnic7Lnl3pwFc/PPW4nam3mEJC9LvpgLe0ucjfdqZhuc2eWViMjmCHHr6YE2LzGsZDH8vf92IOhDyPTIqRg8CZb+0LV0nAiDQm9PGew0rUUp8E nA7wq0Sr FOkQRBOHQEDN1aGd40Vqun9WCaK6RyT4yyuNmnidRwWW99mQ+MHzvyvzGFrseLaR8ra6yzk/Te9ffV4Uj+6eY852nun0mktKxGmc1xCEPQsKu0XqjWEuqxu1QzErFx/SdkLobxsngeon5A6Cc11fKYP78iw== 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 The retval local variable in __stack_depot_save has the union type handle_parts, but the function never uses anything but the union's handle field. Define retval simply as depot_stack_handle_t to simplify the code. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- lib/stackdepot.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 3a945c7206f3..0772125efe8a 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -360,7 +360,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, gfp_t alloc_flags, bool can_alloc) { struct stack_record *found = NULL, **bucket; - union handle_parts retval = { .handle = 0 }; + depot_stack_handle_t handle = 0; struct page *page = NULL; void *prealloc = NULL; unsigned long flags; @@ -377,7 +377,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, nr_entries = filter_irq_stacks(entries, nr_entries); if (unlikely(nr_entries == 0) || stack_depot_disabled) - goto fast_exit; + return 0; hash = hash_stack(entries, nr_entries); bucket = &stack_table[hash & stack_hash_mask]; @@ -443,9 +443,8 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, free_pages((unsigned long)prealloc, DEPOT_POOL_ORDER); } if (found) - retval.handle = found->handle.handle; -fast_exit: - return retval.handle; + handle = found->handle.handle; + return handle; } EXPORT_SYMBOL_GPL(__stack_depot_save); From patchwork Mon Oct 23 16:22:34 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: 13433183 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 C138FC25B43 for ; Mon, 23 Oct 2023 16:23:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2249C6B00F7; Mon, 23 Oct 2023 12:23:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E9D56B00F8; Mon, 23 Oct 2023 12:23:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E7D5A6B00F9; Mon, 23 Oct 2023 12:23:02 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id D14786B00F7 for ; Mon, 23 Oct 2023 12:23:02 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id AB2EFC0136 for ; Mon, 23 Oct 2023 16:23:02 +0000 (UTC) X-FDA: 81377245404.01.A599478 Received: from out-209.mta0.migadu.com (out-209.mta0.migadu.com [91.218.175.209]) by imf21.hostedemail.com (Postfix) with ESMTP id E1CE01C0015 for ; Mon, 23 Oct 2023 16:23:00 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="S/cthfh4"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf21.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.209 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=1698078181; 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=eQriSK20/8KwwHOVdQKhz9C/zLOim8sff0QLVit0F7g=; b=Igl2DJ/EwRxlCd5vIucJ9j+wX7b6KTy1MpbaIbvcngHSK0EpRKCtRuvAhqU4O4ZV6l5X5y KYxH93+Lu42bxOop9g8bUmenfHsuw+Rsh3XjKMOVYrfDIDWspLUfgU5rrx5IYOOoSq6/It bVNWnSDalhweNF9DzG5LN+zsyVDA7lE= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="S/cthfh4"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf21.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.209 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078181; a=rsa-sha256; cv=none; b=Lpc34N6Rm3753hY34bnD1mgM9smQa8pw02ItsGvSwOZlUyb0r5wciwxoGtI56MGGG3GNPD ERUZmlEvwMhzfWdTNHm2+OTJ/WNhGrwW+WaTJVIzLigAtjFm4UuR9aOHfY5vONUm+ATzP4 7OgWU1cy222jM0dZecWhgJN3/ixKi2U= 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=1698078179; 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=eQriSK20/8KwwHOVdQKhz9C/zLOim8sff0QLVit0F7g=; b=S/cthfh4emQrjcVoGLkWqQT0OvtdGJAoIvLTqkDDKVckvqP8bUpXYF9LHT/z69ET7396wu VHNZOBPwORg5a49jPPMsPr93gE7IJt9XyYCBaDWrIFujMNX6hp01vXkIeQlmRKoXZZfyEN tUKjyZoUBaxEoZLynpfz1I7/Mg/7kMg= 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 03/19] lib/stackdepot: drop valid bit from handles Date: Mon, 23 Oct 2023 18:22:34 +0200 Message-Id: <5e251a589cb3142607ec5af8fcb904d424702a14.1698077459.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: E1CE01C0015 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 7o1qsbqw57ukratba64croix3sw9hkus X-HE-Tag: 1698078180-62612 X-HE-Meta: U2FsdGVkX18UydRpX+qkHyQWUELD0U0i/Md/x7gUnx00+MMbzzGhx5yeGFZNLJ7VZG71dWAI+TtjXx0R6v+bCr/juA/GfO8MEZ8cRo7uFqbsqewkEifBGkFaJIuMeXEvY61anWUpOOlor4WsPrJnrtBd+c9bmMDKuonaNM+nJjxyx+P1w0OO64JRvQ0yrNrTi5KrJq/0M0p2isfhlsBUY1+WvxWtW/NWjRvFGsaR6e7iCPHSYqQ9gE9o0OzVyg0isCbnaADJawtyp1BTP98O29KLAJ+MQiDXFjd/G8H132ODjMpX6yMNjcj2WxvedXB+m297XqhYom3xFPKFtEEKY7IA10VFQC1C5xAcFp7s9CcQKcrUL0QYTKteiztC73o697iPpMJmH9h3Hkes1xlsmkj1bbPE/PanwOqkt6oLE1Jqb7p9Poh9mqfQ7a4PWUml1isoNGFsDymc6t8QJMJW76onerbxaQ4wqAETZWTcDjQ9j4SNHBpLuCSpLOfHpuTOhntGl0pLgjmFSkWYP5xu4Yly1YsSxDejQxxcA+YmYVg3ps2Jy+MVXEKSgXqiH8Y+vJ+a65Z6IZjmt+2w5a+f8xwqYjrh3QbY7SJcQM2neMQs1CI3+RpTE1HzkDRrhxESPhhT9xNz6iyEnb/BX5GfsaV5AkW9glheqHDE80yoi1X/vFRsszzwdOfqxqIOgvzVk6eF6+plar6a1Efmj9fTVUeEDdmCe7vDEamEd38SyMECGnxJ9+ETQu1kVHJ6IszrltTc0189G9qV2ZOj3RiTeq7JATxcFVNE3M/POT/WBscph1n5vlUZocSZetqcel/KTULf2dSo6i4Iw61cFnX5VBxKfJfTnS/urojFBzY9mdoBeJftK6TFom8WhbRhymLlEGWMT+RG4XrMMlrFOvahJb1N1F2u4XLCpNiqZhtE5F2bYIF+tAyvaOPqOnrr2RCnZQErIT6Pm31rfKewdWn zu9/oBtO /qZEHziEoidL1ZnyvP1BmQQWT78QlUSax5a5LMq8541R3Vqdn4EUODIzZ1yGydVh9CaSYJi/WqRKwGgVvd5ZIBA9SLrh2qMa789tM 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 Stack depot doesn't use the valid bit in handles in any way, so drop it. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- lib/stackdepot.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 0772125efe8a..482eac40791e 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -32,13 +32,12 @@ #define DEPOT_HANDLE_BITS (sizeof(depot_stack_handle_t) * 8) -#define DEPOT_VALID_BITS 1 #define DEPOT_POOL_ORDER 2 /* Pool size order, 4 pages */ #define DEPOT_POOL_SIZE (1LL << (PAGE_SHIFT + DEPOT_POOL_ORDER)) #define DEPOT_STACK_ALIGN 4 #define DEPOT_OFFSET_BITS (DEPOT_POOL_ORDER + PAGE_SHIFT - DEPOT_STACK_ALIGN) -#define DEPOT_POOL_INDEX_BITS (DEPOT_HANDLE_BITS - DEPOT_VALID_BITS - \ - DEPOT_OFFSET_BITS - STACK_DEPOT_EXTRA_BITS) +#define DEPOT_POOL_INDEX_BITS (DEPOT_HANDLE_BITS - DEPOT_OFFSET_BITS - \ + STACK_DEPOT_EXTRA_BITS) #define DEPOT_POOLS_CAP 8192 #define DEPOT_MAX_POOLS \ (((1LL << (DEPOT_POOL_INDEX_BITS)) < DEPOT_POOLS_CAP) ? \ @@ -50,7 +49,6 @@ union handle_parts { struct { u32 pool_index : DEPOT_POOL_INDEX_BITS; u32 offset : DEPOT_OFFSET_BITS; - u32 valid : DEPOT_VALID_BITS; u32 extra : STACK_DEPOT_EXTRA_BITS; }; }; @@ -303,7 +301,6 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) stack->size = size; stack->handle.pool_index = pool_index; stack->handle.offset = pool_offset >> DEPOT_STACK_ALIGN; - stack->handle.valid = 1; stack->handle.extra = 0; memcpy(stack->entries, entries, flex_array_size(stack, entries, size)); pool_offset += required_size; From patchwork Mon Oct 23 16:22:35 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: 13433184 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 DA721C25B41 for ; Mon, 23 Oct 2023 16:23:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E83756B00F9; Mon, 23 Oct 2023 12:23:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CDED86B00FC; Mon, 23 Oct 2023 12:23:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AFBA36B00FA; Mon, 23 Oct 2023 12:23:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 998276B00F8 for ; Mon, 23 Oct 2023 12:23:03 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 48E154033C for ; Mon, 23 Oct 2023 16:23:03 +0000 (UTC) X-FDA: 81377245446.24.C8F8794 Received: from out-196.mta0.migadu.com (out-196.mta0.migadu.com [91.218.175.196]) by imf04.hostedemail.com (Postfix) with ESMTP id 787364000C for ; Mon, 23 Oct 2023 16:23:01 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=MDW7AbX4; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.196 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=1698078181; 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=9Tw+dU2o5sfOuuXbX4FJVP/5BAKc6p0/nQ7cBmUzv5Q=; b=HoOK9Uvxs/7qubJ8D7/3BC32gLtkuyvkZG/hhWEOC+x9Vvz8GsyvU/Buhlma6LPgqXymqU 6rUlmOWWs2fp1p0X/8bOpdzM4C3zvgYwmFQS6+Gso1Erw0EY9LfmRzpRgY7Dwn1y/7M5hC Ga6mSiZLt3Kd2aUxdOM9WihNEZdPdiU= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=MDW7AbX4; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.196 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078181; a=rsa-sha256; cv=none; b=50Jyl9stHH7mX7D7lGKbnrS8k9XsyuE11K5DENs5J7DJFXjEDlMzYBewkESoasgiiRcmlC 6o1924C9+nS3ep9AKaRjHkyqt0dUFOfMyw/g0lytZfX2i5h7Up1gh0IWR9da0psnHaYQ5c VtI6pEcKCSycl9ZTUHAyTcgJhaArsb4= 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=1698078180; 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=MDW7AbX4eL0/TS7YLtRKEDg9+Xdw06lVwYB5nqof1k6w+UTAbp/tBlDVRi4NGfguchUae+ /lNPyD5G/IVsNQ4ZhW+fz7iuCkHqlbzWf5vQUi0pyT0Wn1XU8dxqiYv/rpGzHXYqU6FP/D 216IpW+w8WvbWqzOU6usNeJ95Qnlubo= 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 04/19] lib/stackdepot: add depot_fetch_stack helper Date: Mon, 23 Oct 2023 18:22:35 +0200 Message-Id: <48b71b2ff972088aacb3466d4de5afd46b6aa7e5.1698077459.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 787364000C X-Stat-Signature: ea9ujrmtx9r4c9hgm46umpr7e14shiqx X-HE-Tag: 1698078181-552600 X-HE-Meta: U2FsdGVkX1+k2/h8f9QetWYOpJUJvdKRLRUofgToDiRvN3ELafy+pHhvPm0RepT4DyAvBsWKZvtgtsr2wdJCQoIGqbd+RDDb8/rmTphjiQHc9DptXo1CtljaDz9huDd53UhtcFSVA89jVROBbvJ1BWohsZwn/JzkmH6MqYSKAXq4n+6kH/jrVIs1HfJlph2Khn6LQFlxecTnOhLRpM4Fk1gTYW6OmVo9FtKLUcBamkgtICdTHCyIaL1YT4Wv/qV8pzZ6ikSFh/y0oThP+KEVw4XqQWhZJ/1hnSzAbDGjoAxpAL0vZGFn6a5TdKDFpYaSS7kcvc/KYpnKxljGMViPA1i+uShpkh4rURpXHVHyKVmyT1lZbNuWzUcvEhzrkJ9PpIOom6UVgcMgIlYkGLEFrzgrE/MzKF3l+tCVVsFkcBlZGlE4R6yDYUeDibXMPTIi8BkbRWWIrMfUwQtrjyRUSvHJCmcqTQevycO48i/h/dalTXb5bdgSmpZtXWzl1B3T7ch3ql4k5iKuQ9ZrOx9vuO7cqsjQP0wq7FADiU6vTnoUCK6TnYexP9TSFqCjFyYnVPoKbnkVubfkwG0p3RuoII5TzY1i1cB5zLL6xJH4u05MxhbKBXYOYrLJMRG72AAGDXdPKLuXJ+tgBFIidSExFkcXbQX6ILvWPCMjv0EXbmMjilhUMZARgoY4n7E8Vltay+9jdW28XESFYC87yRYLRl435o5nV0TMVg9B7ug9BeUL+tScMfCA0bOv6R+ScBFo03AeP3i/afZ4sWFY6euYC5zukDW0T3VJstddrR9l8GPcDzXC7lcNxi3wbndt2d+8dpcqlxbp/nC29OVDogYP9qybgo0yk1pwqgbAQTYplrVyMlK/b8Mr8csYqHE7hR7TYM3owv/3aNJUIB+JESdentbxa5VQp+7ZfKG8tQ5hDcX5sscIEOgN4umQFgiC49vrUJckLx8Dxom5steCJee pzMAEwTz 0raacDi/f2mlA3YXBhnW78hGgls+KInz2PN752h5D2VEzBE02VItq9jeihEI2ewCya9UfzGkc8ve7xMy8rzQLHyvRop5sEsnxNnrAKrqtszUsnZ60GrJAeKhsCzds+rkUt/ICjbHZct3ZM8P6SSyO5PO2BA== 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 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; From patchwork Mon Oct 23 16:22:36 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: 13433185 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 90C34C001E0 for ; Mon, 23 Oct 2023 16:23:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 24F876B00FA; Mon, 23 Oct 2023 12:23:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 13FB06B00FD; Mon, 23 Oct 2023 12:23:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E09926B00FB; Mon, 23 Oct 2023 12:23:03 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id C080C6B00F9 for ; Mon, 23 Oct 2023 12:23:03 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 98FACC0878 for ; Mon, 23 Oct 2023 16:23:03 +0000 (UTC) X-FDA: 81377245446.20.CBACAC0 Received: from out-194.mta0.migadu.com (out-194.mta0.migadu.com [91.218.175.194]) by imf10.hostedemail.com (Postfix) with ESMTP id EB120C0004 for ; Mon, 23 Oct 2023 16:23:01 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=rkq81ZIH; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf10.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.194 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=1698078182; 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=bMe0IukHdtXUzp2BGVWle8stWPjWZ8/o4+6QYAoowVk=; b=V+k3DKDgXqAZNWNUPRLkWoBo8VKuWk50zLJHPS7NwaqpH25aC6yYiNaYv/YcgI9qY6pkXE OQdGAX3VgSy0hH+EMqeSmWMJXLJQYebn/MY/k95bbFui7cCdbfvhhjCXr995ZGINQ+bs8L YA/Owf9Pzg/fLjweSoTxbp16A7CRN/I= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=rkq81ZIH; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf10.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.194 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078182; a=rsa-sha256; cv=none; b=fU5Nm2YCLC4+9HQuQyz9i9150709ylYtvc4D561G09wwVEiR822icZJ62kcWNQ5YAPqTgr lEkmXwXO5a895huBYwKfKATu4clcYsqwdMDWnd5FMOmyPcu6T3QcnjmnJk39RgnMLz5a03 DhmjtUQz8yYeahiswtGm1J0s0be5NYs= 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=1698078180; 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=bMe0IukHdtXUzp2BGVWle8stWPjWZ8/o4+6QYAoowVk=; b=rkq81ZIHIptQkPMqAJh4zbGsz6Tf/Km8HtCA+J1L5FmEVUWRXZFPtR3mYiLwbXzLU1ve+m ipP9E4g9fFYH2AhD85YBltaGTWSFkAJKRvJkJTSAujCAdfb7pv+ffb4OXZf2EtpweYEmBM QWsHiUJVDHj2Hr+JhC9QlyMPSRnLRJ0= 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 05/19] lib/stackdepot: use fixed-sized slots for stack records Date: Mon, 23 Oct 2023 18:22:36 +0200 Message-Id: <4340f57fa82fde81e00f64f98d69ee4a5d5ed9a8.1698077459.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: EB120C0004 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 6et3b7rcxnk3uq33zt77iyac89mb65sc X-HE-Tag: 1698078181-841708 X-HE-Meta: U2FsdGVkX18NUuwTnK4YcA0CHX+Mfq09kfebONvyBWlk46KcPmLNsSTtPH9/y7riKGRnpERDnpvLWYzCyigDqesbnWj/5N5J7ethy3U9sAOR8Mzue2oY/yRaxPlfrtAwm38LZ9UTsvaGS/yg7rD07+/hyS3GQGe96ULkTQ5Rp5+sRlEQFg8Tp09BaDtmbsrR86YD8VK9rHNMGH5wZUeQ+zUGrdme0T7hdvc61cq9MnKLp4ihNYiXbuGakh336n523JyW5fInejDdIzGHv5eS0ukZ6/VMFmirR4ogzC/u4bS3vmLBX8NkexKT5PMtMCl84LSAB/HDk6i7uCv85W8B42aK+LV+z5KxdeoXCopDlsht2MI6sVs8Ba+/XNUmiCYga9Tx+FO/Dzq9n8y12Iq1hYulopvbHROXxWZRqwlQHFhbpqBdqxjNH0k/xz23TIpCSf8k8vp2TCxJPgodi5VS2T1S9uTXoxhKokl5VF+ySBLS7oSovbxoDD3zehQ7b5yKLlJz5lNohaSDGo04OiFW51BzaCosfJYn6/qOOs9vuxaiprtyF98OVdXbbMEdyPM8gy2EIgMcuvYCPa1eYSQT72b0uSwJge2XuP4EhT8M7flsYgYqfeb49ZUquBBiqAFgb1ErvaeVwSp6bG9B5vOeU9jB2H3m/Bjmb8A3MvQPTWkj/i7FTeK43k8QJfTI2T7WC2pbHhAWc0o0mOsxD3DF00wwlAlE5EUcL21hzTi9cjKuLfU60IsGrRFUAbxlKzQTsKE5iQuNv89CNveVX997/gUKvG+qNRCHUN2OR2ABE70kccAODhzrt0euTKwI+kX0kIK+uSGVeoULdCCyBJO4v8Mw+9FXOLs/orNmf43Cm82bKLn4w+eXnrhGTInUuWiF/A6B2c5q1cTqeBJ9qSburZx9UFVggaK4lz8HcHLXBZBpQpYx6+4QVwqk8Ud3OdtrwpeZYpoKQJAflkffzJi qIt9casI RqU2FB2c/EzN8cPbEikUe9sIoJxjsWoCDEq3S+0qB8w3ZQYL3bRyatn/xdIZn7OmRfeIltRtFDmCYx7hnUYpTDiLbRXvmdeTJqLZR+tT8rh3LQ34= 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 Instead of storing stack records in stack depot pools one right after another, use fixed-sized slots. Add a new Kconfig option STACKDEPOT_MAX_FRAMES that allows to select the size of the slot in frames. Use 64 as the default value, which is the maximum stack trace size both KASAN and KMSAN use right now. Also add descriptions for other stack depot Kconfig options. This is preparatory patch for implementing the eviction of stack records from the stack depot. Signed-off-by: Andrey Konovalov --- Changes v2->v3: - Keep previously existing Kconfig options not configurable by users. Changes v1->v2: - Add and use STACKDEPOT_MAX_FRAMES Kconfig option. --- lib/Kconfig | 10 ++++++++++ lib/stackdepot.c | 13 +++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/Kconfig b/lib/Kconfig index c686f4adc124..5f3fa3659fa9 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -710,10 +710,20 @@ config ARCH_STACKWALK config STACKDEPOT bool select STACKTRACE + help + Stack depot: stack trace storage that avoids duplication config STACKDEPOT_ALWAYS_INIT bool select STACKDEPOT + help + Always initialize stack depot during early boot + +config STACKDEPOT_MAX_FRAMES + int "Maximum number of frames in trace saved in stack depot" + range 1 256 + default 64 + depends on STACKDEPOT config REF_TRACKER bool diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 9a004f15f59d..128ece21afe9 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -58,9 +58,12 @@ struct stack_record { u32 hash; /* Hash in the hash table */ u32 size; /* Number of stored frames */ union handle_parts handle; - unsigned long entries[]; /* Variable-sized array of frames */ + unsigned long entries[CONFIG_STACKDEPOT_MAX_FRAMES]; /* Frames */ }; +#define DEPOT_STACK_RECORD_SIZE \ + ALIGN(sizeof(struct stack_record), 1 << DEPOT_STACK_ALIGN) + static bool stack_depot_disabled; static bool __stack_depot_early_init_requested __initdata = IS_ENABLED(CONFIG_STACKDEPOT_ALWAYS_INIT); static bool __stack_depot_early_init_passed __initdata; @@ -258,9 +261,7 @@ static struct stack_record * depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) { struct stack_record *stack; - size_t required_size = struct_size(stack, entries, size); - - required_size = ALIGN(required_size, 1 << DEPOT_STACK_ALIGN); + size_t required_size = DEPOT_STACK_RECORD_SIZE; /* Check if there is not enough space in the current pool. */ if (unlikely(pool_offset + required_size > DEPOT_POOL_SIZE)) { @@ -295,6 +296,10 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) if (stack_pools[pool_index] == NULL) return NULL; + /* Limit number of saved frames to CONFIG_STACKDEPOT_MAX_FRAMES. */ + if (size > CONFIG_STACKDEPOT_MAX_FRAMES) + size = CONFIG_STACKDEPOT_MAX_FRAMES; + /* Save the stack trace. */ stack = stack_pools[pool_index] + pool_offset; stack->hash = hash; From patchwork Mon Oct 23 16:22:37 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: 13433188 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 BDD12C001E0 for ; Mon, 23 Oct 2023 16:24:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AD8386B0102; Mon, 23 Oct 2023 12:24:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A604E6B0101; Mon, 23 Oct 2023 12:24:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7A0A06B0104; Mon, 23 Oct 2023 12:24:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 62B4F6B0101 for ; Mon, 23 Oct 2023 12:24:08 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 5DA4E160B3A for ; Mon, 23 Oct 2023 16:24:07 +0000 (UTC) X-FDA: 81377248134.05.83DAE97 Received: from out-207.mta1.migadu.com (out-207.mta1.migadu.com [95.215.58.207]) by imf13.hostedemail.com (Postfix) with ESMTP id 7E7FC20028 for ; Mon, 23 Oct 2023 16:24:05 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=tTmhSqri; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf13.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.207 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=1698078245; 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=6w0l/5/OLmmiPtUBy12XvNW481vH2W+yw4PJnFK30wA=; b=2cWOBSHCn+1jHtkFjxxc7DuKg8ZchoEr++jhuJ53wJRFZJruy8bqwPgJ1ZbyYeMqq9oKVq /HGJ3KQrEuyIDCrl09lUgqvyOEwLR743vYiak27FAsTZV9WpV1osms1koeKpuy/xBtcTkN /cv30JP6AyAvnz4LCGpCZ8g0n3pFaO0= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=tTmhSqri; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf13.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.207 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078245; a=rsa-sha256; cv=none; b=U9aLEPcdfHtHINXNluVa9spOiTbIVTazt6NJYGuTQv7rTeQwg/mqbPYCeFO/i9qY3nRJ+Y ol84ooT3IUTCAWUrYFR5kiJguGLgaYeuhQdPBcjw9tVOnJno03RGUaFYC9gqp0KMGYHakb DrqtHJ5z+Hr821QMJdcvV6JATLcI4QQ= 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=1698078243; 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=6w0l/5/OLmmiPtUBy12XvNW481vH2W+yw4PJnFK30wA=; b=tTmhSqriTq1xAUwIMKx9hsGiOaqTSZIq/wKJLXRg6daSa2A/wpp5RWjZ1i+KFXZWKiTvL7 FT80+nqHJsuFFL2EcMj7absCsQ1tHdiG6bPU8EWrNzLrOT2cqyLQvL1bQUd/tEodCi1gSB pSo03HvvDVMKsmPPsyN4xZhauDuuzOs= 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 06/19] lib/stackdepot: fix and clean-up atomic annotations Date: Mon, 23 Oct 2023 18:22:37 +0200 Message-Id: <8f649d7e5919c56bcc5d2d356c9584fdcb87800e.1698077459.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 7E7FC20028 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: dr5ipf11gu6n1jg9ic1oex63fg5or64z X-HE-Tag: 1698078245-198402 X-HE-Meta: U2FsdGVkX19RLp/9/jnX2SOt1dqF6lswEX+p04f0QGt/+s1cdOgQA/kFahaVO7DqJbcL+28Q5N6YnSb4y1qigJ9Kl3nFuYLn0CmptpUOvvZ/4IPt44emFSjzqeTWHYWUXQm6ptrdXsOo0hPx0BvKrMdc6XwVzlsiFRQcmgmU9OGlhxN1yJnJ7VGVZu09e5nXD2849tXRLNo6RdNuu1kXtMtHIOrLoluYKrxSk7q1tndlT48g5UurTJzZX4iEGZp4fB5MesGb7cRss430/jRY9Hmam3cB6udVxpwZnyPVzMXv//QJVq8k2aCOwXNoPtHeBkEgfwdrzPfjengOzNeX7jz6ZqSK8SzcOHYB1D7bJsu7v9E2Zy2Yc1F2GaiCxQqIpMNBql08MzA7UqCv7iYabYAgtiRW2q4XYzEsjAmlyZBslr1lIhy0dGXpkPO7+ucQAfk9VkwMskS+Brmw126wmferyUiBXZMUM4vULQA1d10RwQB4Bn8PbM+n3V5thC6E8x1ABpYYjZHuoht2pjDz//xbhHdug5fzK4++9EdQiKvlfQUarQOrD2ux01vjS5/KolchwxbfKMjnlz6pk/TRA2F1Q4denewoCX1W3dj7swJPYPaZgF2UBXVl1Trgv/aY07LFOutna5E45ZcWuIToqZ4EPbfNljOT4S2y2Xz4Vx69ldNQr0bBXMN843NQ96IIb0trTpkRe9djFPXrZQQx+Xg2mCI0rooFszl0nZccgve9+2CbOvKO95dV6IKgmT11zoM8G+sIRgaUS7pnlZzZmihXlNMiWViaICtQffr/jcM4o7rZiRkUsWgsm72eMGABL3OdK17JFXrmMzOPeVdUhj5m58iX4tDlTXO9BY8b2icSptp05iGEh0eHxVbL8cnPLKcZbegdOfm7Vcw7huriuRL01qZOE6f0VZ4/K7wEAish+SRr3WajK4Vu0lBnfXyk7z45mfDnEpEssNoAujM JeBaOY0X tEQ/8MgvysyZMDzwf9VfqOebeU0MdvepVmlsP1z+q23nurVS/HjhUEPBqXaIsjmD5eQ2FmzIRCZkyZfSvl9nxpPyFPm1sA08DaCwE 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 Drop smp_load_acquire from next_pool_required in depot_init_pool, as both depot_init_pool and the all smp_store_release's to this variable are executed under the stack depot lock. Also simplify and clean up comments accompanying the use of atomic accesses in the stack depot code. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- This patch is not strictly required, as the atomic accesses are fully removed in one of the latter patches. However, I decided to keep the patch just in case we end up needing these atomics in the following iterations of this series. Changes v2->v3: - Keep parentheses when referring to functions in comments. - Add comment that explains why depot_init_pool reads next_pool_required non-atomically. Changes v1->v2: - Minor comment fix as suggested by Marco. - Drop READ_ONCE marking for next_pool_required. --- lib/stackdepot.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 128ece21afe9..60aea549429a 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -225,10 +225,10 @@ static void depot_init_pool(void **prealloc) /* * If the next pool is already initialized or the maximum number of * pools is reached, do not use the preallocated memory. - * smp_load_acquire() here pairs with smp_store_release() below and - * in depot_alloc_stack(). + * Access next_pool_required non-atomically, as there are no concurrent + * write accesses to this variable. */ - if (!smp_load_acquire(&next_pool_required)) + if (!next_pool_required) return; /* Check if the current pool is not yet allocated. */ @@ -249,8 +249,8 @@ static void depot_init_pool(void **prealloc) * At this point, either the next pool is initialized or the * maximum number of pools is reached. In either case, take * note that initializing another pool is not required. - * This smp_store_release pairs with smp_load_acquire() above - * and in stack_depot_save(). + * smp_store_release() pairs with smp_load_acquire() in + * stack_depot_save(). */ smp_store_release(&next_pool_required, 0); } @@ -273,7 +273,7 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) /* * Move on to the next pool. - * WRITE_ONCE pairs with potential concurrent read in + * WRITE_ONCE() pairs with potential concurrent read in * stack_depot_fetch(). */ WRITE_ONCE(pool_index, pool_index + 1); @@ -281,8 +281,8 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) /* * If the maximum number of pools is not reached, take note * that the next pool needs to initialized. - * smp_store_release() here pairs with smp_load_acquire() in - * stack_depot_save() and depot_init_pool(). + * smp_store_release() pairs with smp_load_acquire() in + * stack_depot_save(). */ if (pool_index + 1 < DEPOT_MAX_POOLS) smp_store_release(&next_pool_required, 1); @@ -323,7 +323,7 @@ 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 + * READ_ONCE() pairs with potential concurrent write in * depot_alloc_stack(). */ int pool_index_cached = READ_ONCE(pool_index); @@ -413,8 +413,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, /* * Fast path: look the stack trace up without locking. - * The smp_load_acquire() here pairs with smp_store_release() to - * |bucket| below. + * smp_load_acquire() pairs with smp_store_release() to |bucket| below. */ found = find_stack(smp_load_acquire(bucket), entries, nr_entries, hash); if (found) @@ -424,8 +423,8 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, * Check if another stack pool needs to be initialized. If so, allocate * the memory now - we won't be able to do that under the lock. * - * The smp_load_acquire() here pairs with smp_store_release() to - * |next_pool_inited| in depot_alloc_stack() and depot_init_pool(). + * smp_load_acquire() pairs with smp_store_release() in + * depot_alloc_stack() and depot_init_pool(). */ if (unlikely(can_alloc && smp_load_acquire(&next_pool_required))) { /* @@ -451,8 +450,8 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, if (new) { new->next = *bucket; /* - * This smp_store_release() pairs with - * smp_load_acquire() from |bucket| above. + * smp_store_release() pairs with smp_load_acquire() + * from |bucket| above. */ smp_store_release(bucket, new); found = new; From patchwork Mon Oct 23 16:22:38 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: 13433186 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 5E595C001E0 for ; Mon, 23 Oct 2023 16:24:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C84756B00EF; Mon, 23 Oct 2023 12:24:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C339E6B0100; Mon, 23 Oct 2023 12:24:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AFC8D6B0101; Mon, 23 Oct 2023 12:24:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 9C80B6B00EF for ; Mon, 23 Oct 2023 12:24:07 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 708E04036A for ; Mon, 23 Oct 2023 16:24:07 +0000 (UTC) X-FDA: 81377248134.08.758F99B Received: from out-192.mta1.migadu.com (out-192.mta1.migadu.com [95.215.58.192]) by imf22.hostedemail.com (Postfix) with ESMTP id 96CD8C002B for ; Mon, 23 Oct 2023 16:24:05 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=t+UnmZVx; spf=pass (imf22.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.192 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=1698078245; 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=NZ9DxgJteIUWBZVu3uTAKUKrmlqiVCjIJZsHKffVHuY=; b=MVhH4UEeQ1RzKyPpRdCrk1Mu8QWtU4shAUyIpYCxChV/3PvnVxmd+HWVkhAG0P/EdibRSG FQ1+dQ/3XlPJ/es2kByfzfCvUe0owH6IimTuWTpYvnffNpeKqKUDe0EUXudEKvxGTdPlUH xeBKlSO0RiuV3RxbW4Pa78ZZzNosOIg= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078245; a=rsa-sha256; cv=none; b=2m13I93pd2RL2XRrNQKBeIMB16hFLCyjQcTVa6zlH4GqqnkZgmCCiS86DZXdyzU398V3gT O8NKYJkiZcfI40UlDAAgtQ0yIswKjrHaABCK6I04bOEL67/AFziz1DXgjC/WAhDcm8m8Z7 z8BqfPATccQFqbGa2y9elAtprOMTP04= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=t+UnmZVx; spf=pass (imf22.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.192 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=1698078243; 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=NZ9DxgJteIUWBZVu3uTAKUKrmlqiVCjIJZsHKffVHuY=; b=t+UnmZVxxGN5KzWEQTBmXitTlS6kZTAGzoq0dlMyEevn1HxA0s/x7HvVUtWknOMuAkjIeo BUBxiie7hZs3gjhcBL/qrihnoFcL2dZNbSEVqgM/Rho1AztZFu7TnVgjNbIz/DOVY1yTw3 E63W+Bv8C9VotbqWpTxfMOF8IeJvWIk= 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 07/19] lib/stackdepot: rework helpers for depot_alloc_stack Date: Mon, 23 Oct 2023 18:22:38 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 96CD8C002B X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: oqidznydr88gtezi1fx4bc69yxujdtdj X-HE-Tag: 1698078245-235481 X-HE-Meta: U2FsdGVkX1/MhTf52kSlMFj8SOySJXHvbxak9Zr71brALQKSBcYYFtPPd/j+os2Hp/RAWD+0u2blNbHNerOoZLY16GCSs1z/eWjgtByt5gLipyDzG2+x8Q6tkawxbjIe24O98rRnc3gJrzEzzKbZk0TXJQWNG4CEaz+tVLSiAEqBxhMBgx8ozwPApErTythD9fbZFiQ90YZvlgxVawrJQBnkrzU/B1plRJIOooUy9iRQB7ZbqenFI9X8dGQlU7DkXpSuL3iNPMITyp/9S8If8HN2gBLn56gSX7Miyd2kbaWy6copY4EYd/nwTv9fNOIy9CwSBcB4ae6X7U6mdaLCUKhvVdFr+spdeUsu7xrmT33CC23ltjCenCpZV8topxUtbc8j+pHRl6JcGcMM/M7k6tiO9BbK8v+fiEHalYk7YQ50tZaYXnZIJopJOkAIvGIePXEyLCMFJ6C5BDcy4L8ik8qFZDfqAGWLz6HstfBecaEO25BhgR57+oIL8+DuYvu9BTdd2mNp3Kv1r9v4WsLRNL59M21AwAdf6hDbjgp9Qp8hBMPNE62AEVmZjQJH9Msu2s6ioMxpj3aOrvhSo/UCDa6sRvBKPB/KIIbiVKhadFVSWBl+CnJHwRUmMAexps2mN7DHTWPlnKWVb6LNY9Ae417CHz57S9APBmSVwsdM8IQMKoUi55gqOKHzHLL5qeun2D9dcfRbFgqVkY3qHiDTfZEPZpAIoEOoJIy9ShXk1dfaZPbiVStYqqXRs37XNsGFBWrCocd/rgNd+f5qVUUUUE6MsOix4dZVen15Tu1HYKl1IANSyw95Y4DuYIyixj+L/1F7ZNKq2Mlnppwiq2b4J258Pg2gTMTHR2Sk9IfMfZGfQMXZTcGvxKTiDYQHkBh9gFOnkDoJ4F1yS8PcbzkcJDbwE+kQ3JBwdQGdeefOWGjowaZcLWJf/0loG3cqMdHGy1XG7LF+Xx5gzasW0Y4 t7rDWdi7 L/jG8Fpp/4PxmqeC2zvd9TqwPhNWZa1m1J6+V/g1m+MbRqpD/4WcLe9D6q6LG3O9zwNH4fVp0bD3Kzez8iqsMig9Z4BEvAJaUlhF+vnPxiGCOdtOxyEaUEoi5RQ5G1yeEzzbI/pJF/p1AOaTBHZnwya9PrQ== 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 Split code in depot_alloc_stack and depot_init_pool into 3 functions: 1. depot_keep_next_pool that keeps preallocated memory for the next pool if required. 2. depot_update_pools that moves on to the next pool if there's no space left in the current pool, uses preallocated memory for the new current pool if required, and calls depot_keep_next_pool otherwise. 3. depot_alloc_stack that calls depot_update_pools and then allocates a stack record as before. This makes it somewhat easier to follow the logic of depot_alloc_stack and also serves as a preparation for implementing the eviction of stack records from the stack depot. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- Changes v2->v3: - Add parentheses when referring to function calls in comments. --- lib/stackdepot.c | 86 +++++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 37 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 60aea549429a..3f921aaae44a 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -219,11 +219,11 @@ int stack_depot_init(void) } EXPORT_SYMBOL_GPL(stack_depot_init); -/* Uses preallocated memory to initialize a new stack depot pool. */ -static void depot_init_pool(void **prealloc) +/* Keeps the preallocated memory to be used for the next stack depot pool. */ +static void depot_keep_next_pool(void **prealloc) { /* - * If the next pool is already initialized or the maximum number of + * If the next pool is already saved or the maximum number of * pools is reached, do not use the preallocated memory. * Access next_pool_required non-atomically, as there are no concurrent * write accesses to this variable. @@ -231,44 +231,34 @@ static void depot_init_pool(void **prealloc) if (!next_pool_required) return; - /* Check if the current pool is not yet allocated. */ - if (stack_pools[pool_index] == NULL) { - /* Use the preallocated memory for the current pool. */ - stack_pools[pool_index] = *prealloc; + /* + * Use the preallocated memory for the next pool + * as long as we do not exceed the maximum number of pools. + */ + if (pool_index + 1 < DEPOT_MAX_POOLS) { + stack_pools[pool_index + 1] = *prealloc; *prealloc = NULL; - } else { - /* - * Otherwise, use the preallocated memory for the next pool - * as long as we do not exceed the maximum number of pools. - */ - if (pool_index + 1 < DEPOT_MAX_POOLS) { - stack_pools[pool_index + 1] = *prealloc; - *prealloc = NULL; - } - /* - * At this point, either the next pool is initialized or the - * maximum number of pools is reached. In either case, take - * note that initializing another pool is not required. - * smp_store_release() pairs with smp_load_acquire() in - * stack_depot_save(). - */ - smp_store_release(&next_pool_required, 0); } + + /* + * At this point, either the next pool is kept or the maximum + * number of pools is reached. In either case, take note that + * keeping another pool is not required. + * smp_store_release() pairs with smp_load_acquire() in + * stack_depot_save(). + */ + smp_store_release(&next_pool_required, 0); } -/* Allocates a new stack in a stack depot pool. */ -static struct stack_record * -depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) +/* Updates refences to the current and the next stack depot pools. */ +static bool depot_update_pools(size_t required_size, void **prealloc) { - struct stack_record *stack; - size_t required_size = DEPOT_STACK_RECORD_SIZE; - /* Check if there is not enough space in the current pool. */ if (unlikely(pool_offset + required_size > DEPOT_POOL_SIZE)) { /* Bail out if we reached the pool limit. */ if (unlikely(pool_index + 1 >= DEPOT_MAX_POOLS)) { WARN_ONCE(1, "Stack depot reached limit capacity"); - return NULL; + return false; } /* @@ -278,9 +268,10 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) */ WRITE_ONCE(pool_index, pool_index + 1); pool_offset = 0; + /* * If the maximum number of pools is not reached, take note - * that the next pool needs to initialized. + * that the next pool needs to be initialized. * smp_store_release() pairs with smp_load_acquire() in * stack_depot_save(). */ @@ -288,9 +279,30 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) smp_store_release(&next_pool_required, 1); } - /* Assign the preallocated memory to a pool if required. */ + /* Check if the current pool is not yet allocated. */ + if (*prealloc && stack_pools[pool_index] == NULL) { + /* Use the preallocated memory for the current pool. */ + stack_pools[pool_index] = *prealloc; + *prealloc = NULL; + return true; + } + + /* Otherwise, try using the preallocated memory for the next pool. */ if (*prealloc) - depot_init_pool(prealloc); + depot_keep_next_pool(prealloc); + return true; +} + +/* Allocates a new stack in a stack depot pool. */ +static struct stack_record * +depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) +{ + struct stack_record *stack; + size_t required_size = DEPOT_STACK_RECORD_SIZE; + + /* Update current and next pools if required and possible. */ + if (!depot_update_pools(required_size, prealloc)) + return NULL; /* Check if we have a pool to save the stack trace. */ if (stack_pools[pool_index] == NULL) @@ -324,7 +336,7 @@ 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(). + * depot_update_pools(). */ int pool_index_cached = READ_ONCE(pool_index); void *pool; @@ -424,7 +436,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, * the memory now - we won't be able to do that under the lock. * * smp_load_acquire() pairs with smp_store_release() in - * depot_alloc_stack() and depot_init_pool(). + * depot_update_pools() and depot_keep_next_pool(). */ if (unlikely(can_alloc && smp_load_acquire(&next_pool_required))) { /* @@ -461,7 +473,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, * Stack depot already contains this stack trace, but let's * keep the preallocated memory for the future. */ - depot_init_pool(&prealloc); + depot_keep_next_pool(&prealloc); } raw_spin_unlock_irqrestore(&pool_lock, flags); From patchwork Mon Oct 23 16:22:39 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: 13433187 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 7255AC004C0 for ; Mon, 23 Oct 2023 16:24:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1D60F6B0100; Mon, 23 Oct 2023 12:24:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 185B26B0101; Mon, 23 Oct 2023 12:24:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 025D26B0102; Mon, 23 Oct 2023 12:24:07 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id E39FB6B0101 for ; Mon, 23 Oct 2023 12:24:07 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id A43911A02F3 for ; Mon, 23 Oct 2023 16:24:07 +0000 (UTC) X-FDA: 81377248134.07.95C6385 Received: from out-194.mta1.migadu.com (out-194.mta1.migadu.com [95.215.58.194]) by imf30.hostedemail.com (Postfix) with ESMTP id C55178001F for ; Mon, 23 Oct 2023 16:24:05 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="Ln8jn/bt"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf30.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.194 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=1698078245; 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=QFz7c41J1QbS9PQ1h2ItmRgQJE/l+/U2CDCp2U3Lga0=; b=zgHvmcbqZ4+Br8Rp6FKDwO9mF5yp6aMdCKbGTixvoDsA0JuwxrmK70T4qgLcW/C1Ham7Qz Zf7WHsyKlFuF1b88RTnGvF2dYxPJePdZtiBTGnRMf+8eXgKeorzs9tiOkkNGu/nn7iPf/+ vfY3nXW4CQNw0a6lX5T1LNZVUxcgNQs= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="Ln8jn/bt"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf30.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.194 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078245; a=rsa-sha256; cv=none; b=0fL2CDsq8kINY73EinePsJIrZFl1oEKLSJkbKe8ZoLnEtKPKa+bDPd8qDTGLFnJDnHkOu5 4/nQjo0FxE7BtbRMtC/90UB/Rh7isw0UOIxqvJEBo2gInVKTQIZBqlADpWBxX0wJgdmCO4 n3Tzn1MPbkjAXnZ3+7CfarvWEOYdu8o= 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=1698078244; 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=QFz7c41J1QbS9PQ1h2ItmRgQJE/l+/U2CDCp2U3Lga0=; b=Ln8jn/btUwxdS3NYz+kfGYLWvsWTvJ5u3YyVev72quQFxpQ6rAJIQeN6sfP0fJG2IQ2K/7 Ru/NFMADjWGJJEoVmdgwuQzr+Cafy+Kpdr9JMH8ZcWTuixF85WuxaVOR1wWdIJI6caGvld MaqFZ97JESE7qZ5aZRIgTe9xXo/s0x4= 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 08/19] lib/stackdepot: rename next_pool_required to new_pool_required Date: Mon, 23 Oct 2023 18:22:39 +0200 Message-Id: <5d8faf61763307b4d71a9b5272b88fb5c5ae7521.1698077459.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: C55178001F X-Stat-Signature: 7qgasbopm7bi3w7j1cxphxg3cd84gw47 X-HE-Tag: 1698078245-668209 X-HE-Meta: U2FsdGVkX194O48vnd/LQ6kTbbdpfeQ++veBMesitY2mdkakH02BiPD5VY0Xc9QYYZNiMlM7t+WRXz3kk9sxGVWJbVroLnnVtEuZM0qJ3JYbd3E7Zfr7KB5ETldaKWBEwQWADLXj5TOngd9INwyduNAGV+nFn+7WVmXT368OuLwCXhoR0T7fVkoe3SsPQmN00nmBEH0+k2SX1msyTt9SF+QItibV95CsBxx7JuRNs5vZgHgm9LCf5oJsTwBKV+a+d2YoiD7UAm20symWdkcMrKdZb/1FfFoHFLURh6xuM+zb2dd06chKRta8yRoolzkySqJVerhqOKhu2G41QNlB6EMiT/0rgobvmtnjJ/IpEnmwQyLb7ioKAnYU7ZbNvKgzk6viq/vkmX8Y1mHxNT73teNaX4c+1rsZjGGXBIRB5la05q0l+puqqvyjqEBSPFoWvqUsQgrlG45uW4dPxPXrXN+FfYAi9tY4/i0dNKkxtHJjRV216LjBI4M04749wJm7pqzgQ81KpPPfr+zIHxXWeji+WDUv9CVeZZti+6ZtrCvXbo8daiSTLFlmkL8Lfji/A8yU+LBWAJvbHueYWv0V6lBSZ7sgMO8KKmcRuzAa6Gq2FFrj3UHbQx9Ue8tVzqhUXpvGX6QgJHSfnFPCpo0xnpBzIreh7nBenVkNAaNQGqGjBUl92RYc3vV/85k4iccbwci3XL90+8hlSzsF1TZTObIVjEysVQpOr+d24EjhYE+ly7bhktDo0B3XE0PVxtfOWr0Csb9zNfhk/rZjipqn3tqGixGBKxZqwRm2FzTtLAMygaxLg/r3W+JJlxdaGxEqhVuy+/CWxc3Wwgtae2Z3/R09EgOH8A0EwC/H1qehOQQQOr5e5JG3PSscNk32NuzoTPQ+SFIZkjBEJraNDYolAMQdNYEqygQpmDLk9CsJNqQx+VF6fvmhrhLDzN/r+9xqNJvCOvbEtmcLWX1Icnj 1oSjkP4W 14imBvq5mI2razycagowmV+TByh1Aa9ihyV9fFuaLeoEWKjVXwNzx/j5soWoazw77m/5+tGkg8QHOF8Tu+g4LZroY+5WXlR081LxFRRhX+gqyqBSpNsYBcuUtcjFB2y7I9bqPPj+k4PHwe7w= 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 Rename next_pool_required to new_pool_required. This a purely code readability change: the following patch will change stack depot to store the pointer to the new pool in a separate variable, and "new" seems like a more logical name. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- lib/stackdepot.c | 49 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 3f921aaae44a..7579e20114b1 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -93,12 +93,11 @@ static size_t pool_offset; static DEFINE_RAW_SPINLOCK(pool_lock); /* * Stack depot tries to keep an extra pool allocated even before it runs out - * of space in the currently used pool. - * This flag marks that this next extra pool needs to be allocated and - * initialized. It has the value 0 when either the next pool is not yet - * initialized or the limit on the number of pools is reached. + * of space in the currently used pool. This flag marks whether this extra pool + * needs to be allocated. It has the value 0 when either an extra pool is not + * yet allocated or if the limit on the number of pools is reached. */ -static int next_pool_required = 1; +static int new_pool_required = 1; static int __init disable_stack_depot(char *str) { @@ -219,20 +218,20 @@ int stack_depot_init(void) } EXPORT_SYMBOL_GPL(stack_depot_init); -/* Keeps the preallocated memory to be used for the next stack depot pool. */ -static void depot_keep_next_pool(void **prealloc) +/* Keeps the preallocated memory to be used for a new stack depot pool. */ +static void depot_keep_new_pool(void **prealloc) { /* - * If the next pool is already saved or the maximum number of + * If a new pool is already saved or the maximum number of * pools is reached, do not use the preallocated memory. - * Access next_pool_required non-atomically, as there are no concurrent + * Access new_pool_required non-atomically, as there are no concurrent * write accesses to this variable. */ - if (!next_pool_required) + if (!new_pool_required) return; /* - * Use the preallocated memory for the next pool + * Use the preallocated memory for the new pool * as long as we do not exceed the maximum number of pools. */ if (pool_index + 1 < DEPOT_MAX_POOLS) { @@ -241,13 +240,13 @@ static void depot_keep_next_pool(void **prealloc) } /* - * At this point, either the next pool is kept or the maximum + * At this point, either a new pool is kept or the maximum * number of pools is reached. In either case, take note that * keeping another pool is not required. * smp_store_release() pairs with smp_load_acquire() in * stack_depot_save(). */ - smp_store_release(&next_pool_required, 0); + smp_store_release(&new_pool_required, 0); } /* Updates refences to the current and the next stack depot pools. */ @@ -262,7 +261,7 @@ static bool depot_update_pools(size_t required_size, void **prealloc) } /* - * Move on to the next pool. + * Move on to the new pool. * WRITE_ONCE() pairs with potential concurrent read in * stack_depot_fetch(). */ @@ -271,12 +270,12 @@ static bool depot_update_pools(size_t required_size, void **prealloc) /* * If the maximum number of pools is not reached, take note - * that the next pool needs to be initialized. + * that yet another new pool needs to be allocated. * smp_store_release() pairs with smp_load_acquire() in * stack_depot_save(). */ if (pool_index + 1 < DEPOT_MAX_POOLS) - smp_store_release(&next_pool_required, 1); + smp_store_release(&new_pool_required, 1); } /* Check if the current pool is not yet allocated. */ @@ -287,9 +286,9 @@ static bool depot_update_pools(size_t required_size, void **prealloc) return true; } - /* Otherwise, try using the preallocated memory for the next pool. */ + /* Otherwise, try using the preallocated memory for a new pool. */ if (*prealloc) - depot_keep_next_pool(prealloc); + depot_keep_new_pool(prealloc); return true; } @@ -300,7 +299,7 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) struct stack_record *stack; size_t required_size = DEPOT_STACK_RECORD_SIZE; - /* Update current and next pools if required and possible. */ + /* Update current and new pools if required and possible. */ if (!depot_update_pools(required_size, prealloc)) return NULL; @@ -432,13 +431,13 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, goto exit; /* - * Check if another stack pool needs to be initialized. If so, allocate - * the memory now - we won't be able to do that under the lock. + * Check if another stack pool needs to be allocated. If so, allocate + * the memory now: we won't be able to do that under the lock. * * smp_load_acquire() pairs with smp_store_release() in - * depot_update_pools() and depot_keep_next_pool(). + * depot_update_pools() and depot_keep_new_pool(). */ - if (unlikely(can_alloc && smp_load_acquire(&next_pool_required))) { + if (unlikely(can_alloc && smp_load_acquire(&new_pool_required))) { /* * Zero out zone modifiers, as we don't have specific zone * requirements. Keep the flags related to allocation in atomic @@ -471,9 +470,9 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, } else if (prealloc) { /* * Stack depot already contains this stack trace, but let's - * keep the preallocated memory for the future. + * keep the preallocated memory for future. */ - depot_keep_next_pool(&prealloc); + depot_keep_new_pool(&prealloc); } raw_spin_unlock_irqrestore(&pool_lock, flags); From patchwork Mon Oct 23 16:22:40 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: 13433189 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 46486C004C0 for ; Mon, 23 Oct 2023 16:24:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D431C6B0101; Mon, 23 Oct 2023 12:24:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id BE7A26B0104; Mon, 23 Oct 2023 12:24:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9C2A16B0105; Mon, 23 Oct 2023 12:24:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 6B68B6B0102 for ; Mon, 23 Oct 2023 12:24:08 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 4FA38C012E for ; Mon, 23 Oct 2023 16:24:08 +0000 (UTC) X-FDA: 81377248176.23.B6DA5E0 Received: from out-202.mta1.migadu.com (out-202.mta1.migadu.com [95.215.58.202]) by imf09.hostedemail.com (Postfix) with ESMTP id 7A0B114003D for ; Mon, 23 Oct 2023 16:24:06 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=NfBcevWE; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf09.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.202 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078246; a=rsa-sha256; cv=none; b=g7LCTymF67/uGO4ttr9NNlIXsavnRoq2dH7qr9HAnoVVttoLYw+cLlEyoo/lAbxU6OYsyw +VXeUIUxUjagQH/fGbgZLrD/AyCy+7WFf5/midexa6sZpgAXlYLIrK8eXKboINcjwi8NDr ffArikJSGNgWXAL2bU9H2Zce8i2Ir7o= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=NfBcevWE; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf09.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.202 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=1698078246; 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=ApzhSzVlCGqOU4aCEVUKgk2/C6zStSgrVwNwdDBjs8U=; b=pmNRvaBAQ06AkZ/wCOJemNIjm+sdsUKkUjlAMsB1npDozfpZMReqT7mYhgwdT9NKRT958V PVa+P/eQ9mGlB1tnj2ttI0pdJpm6MA+iKUn4F+EEfZcFr9B98t5B2cipm+xddtMV2euKat CGyZouvf3DtJfstjKDzc3St+LiIogk0= 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=1698078245; 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=ApzhSzVlCGqOU4aCEVUKgk2/C6zStSgrVwNwdDBjs8U=; b=NfBcevWE9BlTOxp5WKU+AFsbwgvY+znjPFhBjCe3OZapZ0zGGWutAZrUfoCCoyqQvxbvaI R7EdE6nsCaw0PPJ/YMOZJqf2yEfy6oqmcAkClvCx2QV09MJBtPRaCYiYEtW9gHNizb9Lbe 030j3vL55nL81zukRlM4oSMetrRtY+M= 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 09/19] lib/stackdepot: store next pool pointer in new_pool Date: Mon, 23 Oct 2023 18:22:40 +0200 Message-Id: <852c5fed993f6b1e21beca9faa85e0fc2d9b84e6.1698077459.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 7A0B114003D X-Stat-Signature: iibf5c66671x8mf83rqyd68ks9o5tz6q X-HE-Tag: 1698078246-152713 X-HE-Meta: U2FsdGVkX19ggVqqXRTi87Z+EOi/XMWYg1LWdAF18uVJEKYOqSo0sAw5y4VnB8HijjQuX7rkP0YQ1TlT/EwDHmUucxb2dzxODCO5BRlpwa6i5ZVxpZSdfA8sdZamLuwrMsnd4YEoWwnbCtlXTO1tL/QhkfvVy6PO9HRs/HbHP2pxdCfA86R2NFVpGonLyHVKTDKj2fgypcvi3rNPs3WBgtUi78qfmKxm5SOQr7/JKKjPoDElyRlM1ltxFtLHrIbtQxuR/kY+T0BVCASywb2dgkVQCWTh+ljaN1Ih6Co3l+v+GMgiH+N7fgtr8YfLZSVQsWBqU60Ez89wV6GT6q4dgWUpJG0IT1/scowJnjR1vAHEF5EsNYskRcvnDz0FI1EkUKjAM5wTJ6fVZvy/ylU95XwqDx+dbkGf31KtRAGV/Gle0w+mwHLj+xsMUtHzi3Hr0rQt6ogJE5cTi3ZP2L72ktRdZKGEwM44wptJs++FLlq0hrgkY93/Da3/HDg1d0aNyu+cy3ceEMbSVAjdcp29jMIgnA4uvvzz03dHoba0NXhL8G5IgCQEw/fjSP+i8vhhjfrQ4PJ3hdaZ250gm2EEe5aF9PRONR/cuPJjv7yF2Lg4VwuL8i3RQmL8u//vVetDiUfV8bskWh253p5wApPWcw/x2a5t929c89UEm4vmDnaXPPG2aajS9TGrGGeleKtntx/27HOsX2bgIxgFS4+gNAIfekuH24FifswsQ7ik14zj2wXviqVFusykbBsPZG3FuJ2hgYIADV3gIzA0eaXelGmrU5BGJu/ys7DQubc5xWFPJm+vGGHlNV4J6Ygw9qets+2lmewoXOdlUMCKUxJHUN93lMXgI2+f1Flwd5HVh+008F2dEwWkH9EbrAZOosKGooOoQwCsId/t9tp7ndDJN7c81qlOBAptgdpE34095knXYNT0eEIuxSn8gi8HFceH3oZzevZ7cK9Aan1M4jF 6f2GniC2 weAVx7dsn/9GaSO7/2qfIWQiferq9enpRztdmG49Sf+PYhkOs/bbtIA+xyfFB1jC8iC0yz2y+kww3Aeb0wliH8TV3e9tW6h47c7Zj2wJ+WmktT9EoS3Q+84BjI93iGEH5/bVMiaml3l1Yc1W6mpIdOLogtw== 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 Instead of using the last pointer in stack_pools for storing the pointer to a new pool (which does not yet store any stack records), use a new new_pool variable. This a purely code readability change: it seems more logical to store the pointer to a pool with a special meaning in a dedicated variable. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- lib/stackdepot.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 7579e20114b1..5315952f26ec 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -85,6 +85,8 @@ static unsigned int stack_hash_mask; /* Array of memory regions that store stack traces. */ static void *stack_pools[DEPOT_MAX_POOLS]; +/* Newly allocated pool that is not yet added to stack_pools. */ +static void *new_pool; /* Currently used pool in stack_pools. */ static int pool_index; /* Offset to the unused space in the currently used pool. */ @@ -235,7 +237,7 @@ static void depot_keep_new_pool(void **prealloc) * as long as we do not exceed the maximum number of pools. */ if (pool_index + 1 < DEPOT_MAX_POOLS) { - stack_pools[pool_index + 1] = *prealloc; + new_pool = *prealloc; *prealloc = NULL; } @@ -266,6 +268,8 @@ static bool depot_update_pools(size_t required_size, void **prealloc) * stack_depot_fetch(). */ WRITE_ONCE(pool_index, pool_index + 1); + stack_pools[pool_index] = new_pool; + new_pool = NULL; pool_offset = 0; /* From patchwork Mon Oct 23 16:22: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: 13433190 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 D1161C004C0 for ; Mon, 23 Oct 2023 16:24:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3362A6B0103; Mon, 23 Oct 2023 12:24:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2E57F6B0104; Mon, 23 Oct 2023 12:24:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 15EE36B0105; Mon, 23 Oct 2023 12:24:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id EC57D6B0103 for ; Mon, 23 Oct 2023 12:24:08 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id C34B7C0A10 for ; Mon, 23 Oct 2023 16:24:08 +0000 (UTC) X-FDA: 81377248176.19.1174104 Received: from out-198.mta1.migadu.com (out-198.mta1.migadu.com [95.215.58.198]) by imf20.hostedemail.com (Postfix) with ESMTP id EC3B71C001B for ; Mon, 23 Oct 2023 16:24:06 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=iTY6JYsU; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf20.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.198 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=1698078247; 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=hVpUL5x06hsk3ZkSjFGdljEHgMA5oAygtOmJMO9tzo8=; b=kRYJl6DS4RFr/z5S7jbNHEZY6YHyx39MsBBuUY3eN4tcZzCK8ERlQfzrl0qNVy1LnK+RIw yDYkwWTwOf4qJmZ8EsUFJHE3bCjiGOsXAqYwmZPS03LwI4Ad2OhDXYsPbijnwtb1kY0HZb cZpE6pWrj38nYjeMYRRWRd88AdLaKeY= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=iTY6JYsU; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf20.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.198 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078247; a=rsa-sha256; cv=none; b=XU+WiC3vr76jYllnBf+PVYparCW/j03JXMglcvdw2EOWcq2DzESNZ8VgaXM3sf8OeCJcSP b/VjikNF1HwIgOXtzccd3lk/SBe+M+5zH7chEstLl90vu7m6kpDxcCqlQhAw5sn32j4NAR gef5o4gzofuQ38TcRrsDxWfRsJkhco0= 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=1698078245; 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=hVpUL5x06hsk3ZkSjFGdljEHgMA5oAygtOmJMO9tzo8=; b=iTY6JYsUY35XrAxNWjoB6AUfx0WkIdXZLDRXvdjLJBdlA2H9B3Sv8PN27k3Rj2k9vwFZ/Q kYu4cNJ28hwFR0icW3cybkceDhoOMLD3TZkJREMB1OdD+PpAjqP9FOskxFAS7mpSmwYFq0 R3sOIGWwVN6+425LalBAwW/O0YRDfck= 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 10/19] lib/stackdepot: store free stack records in a freelist Date: Mon, 23 Oct 2023 18:22:41 +0200 Message-Id: <0e798c0431646643ce077117933e8a79899a2403.1698077459.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: EC3B71C001B X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: i96bbeqnsteys1y1wu8hzqce58x1c5wb X-HE-Tag: 1698078246-216665 X-HE-Meta: U2FsdGVkX1+/b4RJLOzBOHTOVd0X7gs6ErQkaJq+iB7ARUFPDppGSWA3FK3GpP9zpcpVahMB3Y9TewlBYPEWb7u9vo8sx/kgB9JwP2+VQNNqNsOKSL6bd+uMUK9aXi2sylg1/+/D5nbVzL3D3OrNQO2C1/qpOx13PRNJ8qbNXQ01kAoywprgOWrQRsgK5HiyrsLkPio+6+LKCNsUC8TR9s/KYSz5/Xsxh7Z3KWqXFvy0ssQPWWTMu76YBZe8h4gKtCqiBfYfYxAcUzEmK6fir97sSEYGanUXGmnUPbdxfHmsRNzIcB4FhV0p9ec9lQpUkV7o71UxQb+NiHXAsgVc2ut30G2+4y9WAund19pV/r6Fk46T8lDpv75vrNuYrxxeCWCw3z9ZNgA6YZVuLVWVNV35U+mcOqBsqPcJ0Rsj+P6DGx1kF8Pd0qZBn0y1S5d5dLTfRHZAjz8hKgGYgcqV7edk/Y4uA1zzAjT/3RVr2wosPLiSXxGp/7EHtuS5+61E82+ypvUvo30uZ3gZXxxVgNu10MSpUqQpTU7vc7HRW8pRnWXhNx3BZfvoXj5rcj+WOceA5YMCX4/8U/EeVFa2sfQ4PNKj/himnyzxKvFnaxjS/q0snRnjjP9lSv1g8uWGSxNgPT+DGJ3L5BHQcdpPUuGjyDxEBe1PeLikIRosXN9TL6FxFPYrCyqP422XUd2Jb7fyGK1rWh4o2laNoSvM9dFbJDT8N8MfA2Up7D7TNjbXQf0sNAfz64xwLA8B2p0fuP9SGGKFFn5EZEtvgTSFKFdfqemJpMO1sFjxKmvQvPJLFqB6an2YD3QdhmVX0Tqax8xEzEuXSq2VukPNNSYiGg8eGxcwl8EF8LhWxoeimHa8JieBL/VqX2m5CSnXxmZWleS3X7J6tLT1n3R6i0PguzLE/xfL1wlB6tjqjX5FguUtfeHdpPbVH1dAxqi3PILpQ2W5J20hyR8IUANU7LU PO8MGEiK igRqONaImHEzaGO38969EQe4GPIMUKpEuhfcnUY9+h+9R5aLTBXIeoqptTaNn2VPOF6XhWUFDEwv6Wzk9Tgke/BUZjICYCRc9+KTHAzkLnuAl9aU= 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 Instead of using the global pool_offset variable to find a free slot when storing a new stack record, mainlain a freelist of free slots within the allocated stack pools. A global next_stack variable is used as the head of the freelist, and the next field in the stack_record struct is reused as freelist link (when the record is not in the freelist, this field is used as a link in the hash table). 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 v2->v3: - Add parentheses when referring to function calls in comments. Changes v1->v2: - Fix out-of-bounds when initializing a pool. --- lib/stackdepot.c | 131 +++++++++++++++++++++++++++++------------------ 1 file changed, 82 insertions(+), 49 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 5315952f26ec..1067c072a0f8 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -54,8 +54,8 @@ union handle_parts { }; struct stack_record { - struct stack_record *next; /* Link in the hash table */ - u32 hash; /* Hash in the hash table */ + struct stack_record *next; /* Link in hash table or freelist */ + u32 hash; /* Hash in hash table */ u32 size; /* Number of stored frames */ union handle_parts handle; unsigned long entries[CONFIG_STACKDEPOT_MAX_FRAMES]; /* Frames */ @@ -87,10 +87,10 @@ static unsigned int stack_hash_mask; static void *stack_pools[DEPOT_MAX_POOLS]; /* Newly allocated pool that is not yet added to stack_pools. */ static void *new_pool; -/* Currently used pool in stack_pools. */ -static int pool_index; -/* Offset to the unused space in the currently used pool. */ -static size_t pool_offset; +/* Number of pools in stack_pools. */ +static int pools_num; +/* Next stack in the freelist of stack records within stack_pools. */ +static struct stack_record *next_stack; /* Lock that protects the variables above. */ static DEFINE_RAW_SPINLOCK(pool_lock); /* @@ -220,6 +220,42 @@ int stack_depot_init(void) } EXPORT_SYMBOL_GPL(stack_depot_init); +/* Initializes a stack depol pool. */ +static void depot_init_pool(void *pool) +{ + const int records_in_pool = DEPOT_POOL_SIZE / DEPOT_STACK_RECORD_SIZE; + int i, offset; + + /* Initialize handles and link stack records to each other. */ + for (i = 0, offset = 0; + offset <= DEPOT_POOL_SIZE - DEPOT_STACK_RECORD_SIZE; + i++, offset += DEPOT_STACK_RECORD_SIZE) { + struct stack_record *stack = pool + offset; + + stack->handle.pool_index = pools_num; + stack->handle.offset = offset >> DEPOT_STACK_ALIGN; + stack->handle.extra = 0; + + if (i < records_in_pool - 1) + stack->next = (void *)stack + DEPOT_STACK_RECORD_SIZE; + else + stack->next = NULL; + } + + /* Link stack records into the freelist. */ + WARN_ON(next_stack); + next_stack = pool; + + /* Save reference to the pool to be used by depot_fetch_stack(). */ + stack_pools[pools_num] = pool; + + /* + * WRITE_ONCE() pairs with potential concurrent read in + * depot_fetch_stack(). + */ + WRITE_ONCE(pools_num, pools_num + 1); +} + /* Keeps the preallocated memory to be used for a new stack depot pool. */ static void depot_keep_new_pool(void **prealloc) { @@ -236,7 +272,7 @@ static void depot_keep_new_pool(void **prealloc) * Use the preallocated memory for the new pool * as long as we do not exceed the maximum number of pools. */ - if (pool_index + 1 < DEPOT_MAX_POOLS) { + if (pools_num < DEPOT_MAX_POOLS) { new_pool = *prealloc; *prealloc = NULL; } @@ -252,45 +288,42 @@ static void depot_keep_new_pool(void **prealloc) } /* Updates refences to the current and the next stack depot pools. */ -static bool depot_update_pools(size_t required_size, void **prealloc) +static bool depot_update_pools(void **prealloc) { - /* Check if there is not enough space in the current pool. */ - if (unlikely(pool_offset + required_size > DEPOT_POOL_SIZE)) { - /* Bail out if we reached the pool limit. */ - if (unlikely(pool_index + 1 >= DEPOT_MAX_POOLS)) { - WARN_ONCE(1, "Stack depot reached limit capacity"); - return false; - } + /* Check if we still have objects in the freelist. */ + if (next_stack) + goto out_keep_prealloc; - /* - * Move on to the new pool. - * WRITE_ONCE() pairs with potential concurrent read in - * stack_depot_fetch(). - */ - WRITE_ONCE(pool_index, pool_index + 1); - stack_pools[pool_index] = new_pool; + /* Check if we have a new pool saved and use it. */ + if (new_pool) { + depot_init_pool(new_pool); new_pool = NULL; - pool_offset = 0; - /* - * If the maximum number of pools is not reached, take note - * that yet another new pool needs to be allocated. - * smp_store_release() pairs with smp_load_acquire() in - * stack_depot_save(). - */ - if (pool_index + 1 < DEPOT_MAX_POOLS) + /* Take note that we might need a new new_pool. */ + if (pools_num < DEPOT_MAX_POOLS) smp_store_release(&new_pool_required, 1); + + /* Try keeping the preallocated memory for new_pool. */ + goto out_keep_prealloc; + } + + /* Bail out if we reached the pool limit. */ + if (unlikely(pools_num >= DEPOT_MAX_POOLS)) { + WARN_ONCE(1, "Stack depot reached limit capacity"); + return false; } - /* Check if the current pool is not yet allocated. */ - if (*prealloc && stack_pools[pool_index] == NULL) { - /* Use the preallocated memory for the current pool. */ - stack_pools[pool_index] = *prealloc; + /* Check if we have preallocated memory and use it. */ + if (*prealloc) { + depot_init_pool(*prealloc); *prealloc = NULL; return true; } - /* Otherwise, try using the preallocated memory for a new pool. */ + return false; + +out_keep_prealloc: + /* Keep the preallocated memory for a new pool if required. */ if (*prealloc) depot_keep_new_pool(prealloc); return true; @@ -301,35 +334,35 @@ static struct stack_record * depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) { struct stack_record *stack; - size_t required_size = DEPOT_STACK_RECORD_SIZE; /* Update current and new pools if required and possible. */ - if (!depot_update_pools(required_size, prealloc)) + if (!depot_update_pools(prealloc)) return NULL; - /* Check if we have a pool to save the stack trace. */ - if (stack_pools[pool_index] == NULL) + /* Check if we have a stack record to save the stack trace. */ + stack = next_stack; + if (!stack) return NULL; + /* Advance the freelist. */ + next_stack = stack->next; + /* Limit number of saved frames to CONFIG_STACKDEPOT_MAX_FRAMES. */ if (size > CONFIG_STACKDEPOT_MAX_FRAMES) size = CONFIG_STACKDEPOT_MAX_FRAMES; /* Save the stack trace. */ - stack = stack_pools[pool_index] + pool_offset; + stack->next = NULL; stack->hash = hash; stack->size = size; - stack->handle.pool_index = pool_index; - stack->handle.offset = pool_offset >> DEPOT_STACK_ALIGN; - stack->handle.extra = 0; + /* stack->handle is already filled in by depot_init_pool(). */ 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. */ - kmsan_unpoison_memory(stack, required_size); + kmsan_unpoison_memory(stack, DEPOT_STACK_RECORD_SIZE); return stack; } @@ -339,16 +372,16 @@ 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_update_pools(). + * depot_init_pool(). */ - int pool_index_cached = READ_ONCE(pool_index); + int pools_num_cached = READ_ONCE(pools_num); void *pool; size_t offset = parts.offset << DEPOT_STACK_ALIGN; struct stack_record *stack; - if (parts.pool_index > pool_index_cached) { + if (parts.pool_index > pools_num_cached) { WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n", - parts.pool_index, pool_index_cached, handle); + parts.pool_index, pools_num_cached, handle); return NULL; } From patchwork Mon Oct 23 16:22:42 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: 13433191 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 BF68FC001E0 for ; Mon, 23 Oct 2023 16:24:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E0F756B0104; Mon, 23 Oct 2023 12:24:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CF8096B0105; Mon, 23 Oct 2023 12:24:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B22336B0106; Mon, 23 Oct 2023 12:24:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 97DBB6B0104 for ; Mon, 23 Oct 2023 12:24:09 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 73992B5C10 for ; Mon, 23 Oct 2023 16:24:09 +0000 (UTC) X-FDA: 81377248218.12.B712B88 Received: from out-191.mta1.migadu.com (out-191.mta1.migadu.com [95.215.58.191]) by imf21.hostedemail.com (Postfix) with ESMTP id 7AE9E1C001C for ; Mon, 23 Oct 2023 16:24:07 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=d8BAggel; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf21.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.191 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=1698078247; 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=p8Hgl8kIxqRiPg7P/caKlN6EZe6EuOrMJBszDGSf5JA=; b=gqlrqWSL2vWA4pwBnBljds3NI/c0BRNz+wmsIEoHNvPMstRpLm1G3nzqqXvB7dH3Ilwk/P 2K9wCP2bWkUnsJlfNlzCVTQGwuUN/6b7YroQXMwrw6ED3Pbv9nwMhu0mUeqHd2+F71bDSG h+XUJdavTRAXjLCYcreAjXMMpo6Ivdg= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=d8BAggel; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf21.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.191 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078247; a=rsa-sha256; cv=none; b=rj4FiEy+J9IHJXTAwJm/KdvAK9dmDklnRA71cjxc8VZ00VEB9jH0i1jrlG/EPbiOpJpnMN hmfMl44pePyxucyzFInJbexaCihjlO6MFpI3OYfxnDlsgfIhDbb9lW+2i1wV/zT7kGXX6p +U5dh3G5S/b+zircpOMgdBMM8UU9eC8= 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=1698078246; 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=p8Hgl8kIxqRiPg7P/caKlN6EZe6EuOrMJBszDGSf5JA=; b=d8BAggels5u67sU3a1DsLYB3mzDLPzqPZYgCDFI/IUfM7R5EyUYByQkE3kU+k9PyxTsmM6 GNUYqd5pUyWDQggTtDoeOwneQ3CQxbmr4n6O3nWwZpqYNBDV2DNnDg12CvGqgMggwxUqrA 379T75KzzDunwidWZHcC0GAmFd/27C8= 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 11/19] lib/stackdepot: use read/write lock Date: Mon, 23 Oct 2023 18:22:42 +0200 Message-Id: <68b90916541c09fd64de22dd4666b53172f7e618.1698077459.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 7AE9E1C001C X-Stat-Signature: j1da1qsrty15y3qfs5sqh1fguzhxtz5d X-HE-Tag: 1698078247-220751 X-HE-Meta: U2FsdGVkX18RHHADvA6KGWPq13O4QSj1ypMXIn71IECu2/sJLYS9x1duoE0hL7L0RXgn+kZEaqQQcwySo+w20Gw4TqSluooky/yNE8NkH2lQygdX70T1SFA2Lw4dJgExK9JGqxV8/x85QQL7kJ/o8T7RAeJOKccqFk9svcp97+An++gCICsoHY1Fsy06Voqti7iLUp0anhEadEOcnnHddx2lu4oppbzUkPMZgUVioYL4H1s2RaI+GHXu9lxHy40Otm5kALyo3PVf+EDmETsOVUcS/Ygr7p8aM5b62cu13Mu5lfUaEiRDJMyzaDuk7VN4HgEoUTTwMqRUiw9v2vMeiAYe0Dh7P7BZ5+kgFHSBEzbhYkQeNM6sfvVgyjFnd8d/54T/8o4TYy+KQyfteDaxFcnXzQNvJ8iTzUq2NRce7fCj2P5IKU9Q1KaEGI+x+HV1/NAnnDxrAf9KPaByh61vfrXvfws8C14T5tTZZjCHY9sC6dHtNIO3LOu0bm7J6VBX/JyzTj7zq9PtiKTPKgu+fxpAetbQlw+qhQG4SSWgy1M9tug0UF+iJMMIGoBqji8NwW3xz8EHHv+BUNhjCU1rQ+trdvlRTX+92T7wkB8IXAyO3xjuNmcpH+9yPsnCRSmkZ1tjQwi9y9qjfBwQq8wzi9qK6gJHTyvaJDLxK3Y4iTjzjNMZ7s45I9Hyrqg4DtRYSJgoBRpgGoSd3dgbBDNHs1YiKdJYykIOD2FYfwV0q63p7Fu9TMYEijX/fbktEh4xSVlEAP74jaGfDqjNQHaq9G68bPgyF3QPWshP7OIKlKRhzEo/vUUK8oB8KgmTwZswMTOQCl2vh+m/NewVhEeMBtWZp2zs9zcmZiOAW5PbS1GqZZGI9flWw2+Db9eYWOfEKy5svkrkQS/hnfCZj6Zz8YDujL8uVBv8iV9iZI0U2W3b27Pp6zzWH3nZYp6N2doP1s2cJ+MzpwpTZQOHvB9 Kk5IRW/e dzFeEgosuARbrGnL0J3OokgrFzK+cK8R4fJubpG7ZX3e9C+BmA5u9IqsgFpM+L0nkFzoI/ILuKSjC9YjFF8f4rwR+Bkc+L0SRNS8zemUqnKwxEqc= 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 Currently, stack depot uses the following locking scheme: 1. Lock-free accesses when looking up a stack record, which allows to have multiple users to look up records in parallel; 2. Spinlock for protecting the stack depot pools and the hash table when adding a new record. For implementing the eviction of stack traces from stack depot, the lock-free approach is not going to work anymore, as we will need to be able to also remove records from the hash table. Convert the spinlock into a read/write lock, and drop the atomic accesses, as they are no longer required. Looking up stack traces is now protected by the read lock and adding new records - by the write lock. One of the following patches will add a new function for evicting stack records, which will be protected by the write lock as well. With this change, multiple users can still look up records in parallel. This is preparatory patch for implementing the eviction of stack records from the stack depot. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- Changed v2->v3: - Use lockdep_assert_held_read annotation in depot_fetch_stack. Changes v1->v2: - Add lockdep_assert annotations. --- lib/stackdepot.c | 87 +++++++++++++++++++++++++----------------------- 1 file changed, 46 insertions(+), 41 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 1067c072a0f8..b1ceade0acc9 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -91,15 +92,15 @@ static void *new_pool; static int pools_num; /* Next stack in the freelist of stack records within stack_pools. */ static struct stack_record *next_stack; -/* Lock that protects the variables above. */ -static DEFINE_RAW_SPINLOCK(pool_lock); /* * Stack depot tries to keep an extra pool allocated even before it runs out * of space in the currently used pool. This flag marks whether this extra pool * needs to be allocated. It has the value 0 when either an extra pool is not * yet allocated or if the limit on the number of pools is reached. */ -static int new_pool_required = 1; +static bool new_pool_required = true; +/* Lock that protects the variables above. */ +static DEFINE_RWLOCK(pool_rwlock); static int __init disable_stack_depot(char *str) { @@ -226,6 +227,8 @@ static void depot_init_pool(void *pool) const int records_in_pool = DEPOT_POOL_SIZE / DEPOT_STACK_RECORD_SIZE; int i, offset; + lockdep_assert_held_write(&pool_rwlock); + /* Initialize handles and link stack records to each other. */ for (i = 0, offset = 0; offset <= DEPOT_POOL_SIZE - DEPOT_STACK_RECORD_SIZE; @@ -248,22 +251,17 @@ static void depot_init_pool(void *pool) /* Save reference to the pool to be used by depot_fetch_stack(). */ stack_pools[pools_num] = pool; - - /* - * WRITE_ONCE() pairs with potential concurrent read in - * depot_fetch_stack(). - */ - WRITE_ONCE(pools_num, pools_num + 1); + pools_num++; } /* Keeps the preallocated memory to be used for a new stack depot pool. */ static void depot_keep_new_pool(void **prealloc) { + lockdep_assert_held_write(&pool_rwlock); + /* * If a new pool is already saved or the maximum number of * pools is reached, do not use the preallocated memory. - * Access new_pool_required non-atomically, as there are no concurrent - * write accesses to this variable. */ if (!new_pool_required) return; @@ -281,15 +279,15 @@ static void depot_keep_new_pool(void **prealloc) * At this point, either a new pool is kept or the maximum * number of pools is reached. In either case, take note that * keeping another pool is not required. - * smp_store_release() pairs with smp_load_acquire() in - * stack_depot_save(). */ - smp_store_release(&new_pool_required, 0); + new_pool_required = false; } /* Updates refences to the current and the next stack depot pools. */ static bool depot_update_pools(void **prealloc) { + lockdep_assert_held_write(&pool_rwlock); + /* Check if we still have objects in the freelist. */ if (next_stack) goto out_keep_prealloc; @@ -301,7 +299,7 @@ static bool depot_update_pools(void **prealloc) /* Take note that we might need a new new_pool. */ if (pools_num < DEPOT_MAX_POOLS) - smp_store_release(&new_pool_required, 1); + new_pool_required = true; /* Try keeping the preallocated memory for new_pool. */ goto out_keep_prealloc; @@ -335,6 +333,8 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) { struct stack_record *stack; + lockdep_assert_held_write(&pool_rwlock); + /* Update current and new pools if required and possible. */ if (!depot_update_pools(prealloc)) return NULL; @@ -370,18 +370,15 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) 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_init_pool(). - */ - int pools_num_cached = READ_ONCE(pools_num); void *pool; size_t offset = parts.offset << DEPOT_STACK_ALIGN; struct stack_record *stack; - if (parts.pool_index > pools_num_cached) { + lockdep_assert_held_read(&pool_rwlock); + + if (parts.pool_index > pools_num) { WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n", - parts.pool_index, pools_num_cached, handle); + parts.pool_index, pools_num, handle); return NULL; } @@ -423,6 +420,8 @@ static inline struct stack_record *find_stack(struct stack_record *bucket, { struct stack_record *found; + lockdep_assert_held(&pool_rwlock); + for (found = bucket; found; found = found->next) { if (found->hash == hash && found->size == size && @@ -440,6 +439,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, depot_stack_handle_t handle = 0; struct page *page = NULL; void *prealloc = NULL; + bool need_alloc = false; unsigned long flags; u32 hash; @@ -459,22 +459,26 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, hash = hash_stack(entries, nr_entries); bucket = &stack_table[hash & stack_hash_mask]; - /* - * Fast path: look the stack trace up without locking. - * smp_load_acquire() pairs with smp_store_release() to |bucket| below. - */ - found = find_stack(smp_load_acquire(bucket), entries, nr_entries, hash); - if (found) + read_lock_irqsave(&pool_rwlock, flags); + + /* Fast path: look the stack trace up without full locking. */ + found = find_stack(*bucket, entries, nr_entries, hash); + if (found) { + read_unlock_irqrestore(&pool_rwlock, flags); goto exit; + } + + /* Take note if another stack pool needs to be allocated. */ + if (new_pool_required) + need_alloc = true; + + read_unlock_irqrestore(&pool_rwlock, flags); /* - * Check if another stack pool needs to be allocated. If so, allocate - * the memory now: we won't be able to do that under the lock. - * - * smp_load_acquire() pairs with smp_store_release() in - * depot_update_pools() and depot_keep_new_pool(). + * Allocate memory for a new pool if required now: + * we won't be able to do that under the lock. */ - if (unlikely(can_alloc && smp_load_acquire(&new_pool_required))) { + if (unlikely(can_alloc && need_alloc)) { /* * Zero out zone modifiers, as we don't have specific zone * requirements. Keep the flags related to allocation in atomic @@ -488,7 +492,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, prealloc = page_address(page); } - raw_spin_lock_irqsave(&pool_lock, flags); + write_lock_irqsave(&pool_rwlock, flags); found = find_stack(*bucket, entries, nr_entries, hash); if (!found) { @@ -497,11 +501,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, if (new) { new->next = *bucket; - /* - * smp_store_release() pairs with smp_load_acquire() - * from |bucket| above. - */ - smp_store_release(bucket, new); + *bucket = new; found = new; } } else if (prealloc) { @@ -512,7 +512,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, depot_keep_new_pool(&prealloc); } - raw_spin_unlock_irqrestore(&pool_lock, flags); + write_unlock_irqrestore(&pool_rwlock, flags); exit: if (prealloc) { /* Stack depot didn't use this memory, free it. */ @@ -536,6 +536,7 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, unsigned long **entries) { struct stack_record *stack; + unsigned long flags; *entries = NULL; /* @@ -547,8 +548,12 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, if (!handle || stack_depot_disabled) return 0; + read_lock_irqsave(&pool_rwlock, flags); + stack = depot_fetch_stack(handle); + read_unlock_irqrestore(&pool_rwlock, flags); + *entries = stack->entries; return stack->size; } From patchwork Mon Oct 23 16:22:43 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: 13433192 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 0A293C001E0 for ; Mon, 23 Oct 2023 16:25:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9EC566B010B; Mon, 23 Oct 2023 12:25:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 99C4C6B010C; Mon, 23 Oct 2023 12:25:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 864CF6B010D; Mon, 23 Oct 2023 12:25:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 7729C6B010B for ; Mon, 23 Oct 2023 12:25:11 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 571A91A029A for ; Mon, 23 Oct 2023 16:25:11 +0000 (UTC) X-FDA: 81377250822.11.85D2133 Received: from out-191.mta0.migadu.com (out-191.mta0.migadu.com [91.218.175.191]) by imf05.hostedemail.com (Postfix) with ESMTP id 8D26F100017 for ; Mon, 23 Oct 2023 16:25:09 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=iGUJS+rO; spf=pass (imf05.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.191 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=1698078309; 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=kwAPQEKRgU/QjlEBZ2MhqIx/KOtst15cg0pwz3Hyr9g=; b=2uEdsHxmh/FwYT57SefKaEiUP4RbXjKAamd3U1FwtTI2GmCBF0OEbwGTcQ2l0sjuDO20T9 yZdKxWvWMYpkalleCcIpb/KwN3tuOISVlOvXP6SutpQVetoZIbwR9x7ttEEG42L1RAnPLs xvnfpVdqtIeJbkXTP2fWT/q0giURQD4= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078309; a=rsa-sha256; cv=none; b=KfRt4x1uNt84oj/msqMB8HLLp+gWrrmaedFhERudaNSMR9eMZMzi0+LQBguoBDMoqAkPOA xtEJEkAjLHZsi/SXma9zBEXoSKAraDRWQg46kiqBSj/6h7T99XHCLwExdAojhJ1hergxbQ afdTzUyUzWxUgoskac5bn59CI63Ifx8= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=iGUJS+rO; spf=pass (imf05.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.191 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=1698078308; 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=kwAPQEKRgU/QjlEBZ2MhqIx/KOtst15cg0pwz3Hyr9g=; b=iGUJS+rOn2dXj8f4BIY+WwdB8Mdqb1aEQqfE+6SqjcK1mxQ+u4/MVV6bams1FgB2otyMtr VknTKi9tknQ69VcwKIOlmvNy0fBatX3KvqxEFyA/LAWA33e35h42LQLvfNWq4n+5VujTpi 4WErb4Df30D2FUv9R93/ziOe2SgmPy4= 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 12/19] lib/stackdepot: use list_head for stack record links Date: Mon, 23 Oct 2023 18:22:43 +0200 Message-Id: <518e3873243845249c8fb019d744c5f5eac90205.1698077459.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 8D26F100017 X-Rspam-User: X-Stat-Signature: qi1z191p1fanaogeb19pnaya4za6j68s X-Rspamd-Server: rspam03 X-HE-Tag: 1698078309-561313 X-HE-Meta: U2FsdGVkX19Uih8ZrJkC5s0Q/VAv55r4s1U1TdV3rehiS8aCAYaZ8VgoYHrDT6UV81TmPp0ufK1rOVimFNtXgk4aU7A29ZD8ARwDeNbKM92KkLU9bS9T0QtQtXsIyLlMECq5O1e0vdBQYNrT/gF9B31YYDfTccMQ/82RfflKr9xLlsjQ35f3LaYhU4QVAjVAc+Vr/jj8g+46Zlx6c0cBVn9PQqXU6fQqFIA3oC+rAm84tEB5M3CTU7mgXrDDbBV7aUFUz5iM4v0NG+dGLIi+gfBYo950sdtXq9WrCtqpSuPZRVUSZ5zL08X73sTmb+tgTP8465hWGwCds+0D6ozv1LhfdRKJDrcpCrTpyvr2sEbKnsyDr9byrFOt2N00adXSnFHqhpVk+qC96BoaL/l2EVYDD1Dxc6heGhQ5n0Fmh4yPHOaDbP7wwImS+7vzg0B1sou7WRz4tChfs04yXri/rXqumYCMJhqAbBh2nBJQ7lNIn2VLN90zEdAkLwajAVpzsGVtNHzxuROU0ydl91TOIz0wKsdcu+oLPypC2p1KxKLkCjDnbPsV/OoLyE3kWMzsYv/N1mu1GkFlsvq1QAqpHBQI36v77+yAkWV6WLRcE025BGCpsWGuMg50pu7sZmV6uvL8JCmvRY5MIGhEGkcl5aXAUg5dXHmTcJHNh5G8fjUDvZwj1iQM6aYiFkRTC9M4GLbDLK+rBh511Y6ij7VPuD+wtlVmfKfzJYz3mxsTuRoWqGc8IBsR8LWVi4mccX/jr6wNKPQfEA9eLByENXkEgH3XDHvULw0LtWANoHI5JDLU5A9xRA3D7SuOHOlbbyRgppDlfMxYe120UCpkjB1pA415kUSYM6lrUczGpE+2qglLhVGR3NWO2rD0v9NhFpz50LBbr24W39g0tTzXvcmYf46FXGrzo+XlvYLnw3dGbTI2yLC3EFERGkkdtGzwAgTmHXG6AUSpsHVSYq4jhh6 8+2Ennt8 Hn00cCVMVNgM/3k96fkOerwab4S4gI+qJ7IpMSZTgXnV1vGLLinzyPTaLK0U2/7//7BE+vGXtpmXyLlBQOqe/t68Ygc4ECqnt6Qq83ppVZIPJQlxDvQkwbUVSDWTiu3R3FtkYj+FY5EIhk2Fe7V6Eoeso+w== 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 Switch stack_record to use list_head for links in the hash table and in the freelist. This will allow removing entries from the hash table buckets. This is preparatory patch for implementing the eviction of stack records from the stack depot. Signed-off-by: Andrey Konovalov --- Changes v2->v3: - Use the proper number of entries for initializing the stack table when alloc_large_system_hash() auto-calculates the number. Changes v1->v2: - Use list_head instead of open-coding backward links. --- lib/stackdepot.c | 87 ++++++++++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 37 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index b1ceade0acc9..85fd40c63817 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -55,7 +56,7 @@ union handle_parts { }; struct stack_record { - struct stack_record *next; /* Link in hash table or freelist */ + struct list_head list; /* Links in hash table or freelist */ u32 hash; /* Hash in hash table */ u32 size; /* Number of stored frames */ union handle_parts handle; @@ -77,21 +78,21 @@ static bool __stack_depot_early_init_passed __initdata; /* Initial seed for jhash2. */ #define STACK_HASH_SEED 0x9747b28c -/* Hash table of pointers to stored stack traces. */ -static struct stack_record **stack_table; +/* Hash table of stored stack records. */ +static struct list_head *stack_table; /* Fixed order of the number of table buckets. Used when KASAN is enabled. */ static unsigned int stack_bucket_number_order; /* Hash mask for indexing the table. */ static unsigned int stack_hash_mask; -/* Array of memory regions that store stack traces. */ +/* Array of memory regions that store stack records. */ static void *stack_pools[DEPOT_MAX_POOLS]; /* Newly allocated pool that is not yet added to stack_pools. */ static void *new_pool; /* Number of pools in stack_pools. */ static int pools_num; -/* Next stack in the freelist of stack records within stack_pools. */ -static struct stack_record *next_stack; +/* Freelist of stack records within stack_pools. */ +static LIST_HEAD(free_stacks); /* * Stack depot tries to keep an extra pool allocated even before it runs out * of space in the currently used pool. This flag marks whether this extra pool @@ -123,6 +124,15 @@ void __init stack_depot_request_early_init(void) __stack_depot_early_init_requested = true; } +/* Initialize list_head's within the hash table. */ +static void init_stack_table(unsigned long entries) +{ + unsigned long i; + + for (i = 0; i < entries; i++) + INIT_LIST_HEAD(&stack_table[i]); +} + /* Allocates a hash table via memblock. Can only be used during early boot. */ int __init stack_depot_early_init(void) { @@ -146,16 +156,16 @@ int __init stack_depot_early_init(void) /* * If stack_bucket_number_order is not set, leave entries as 0 to rely - * on the automatic calculations performed by alloc_large_system_hash. + * on the automatic calculations performed by alloc_large_system_hash(). */ if (stack_bucket_number_order) entries = 1UL << stack_bucket_number_order; pr_info("allocating hash table via alloc_large_system_hash\n"); stack_table = alloc_large_system_hash("stackdepot", - sizeof(struct stack_record *), + sizeof(struct list_head), entries, STACK_HASH_TABLE_SCALE, - HASH_EARLY | HASH_ZERO, + HASH_EARLY, NULL, &stack_hash_mask, 1UL << STACK_BUCKET_NUMBER_ORDER_MIN, @@ -165,6 +175,14 @@ int __init stack_depot_early_init(void) stack_depot_disabled = true; return -ENOMEM; } + if (!entries) { + /* + * Obtain the number of entries that was calculated by + * alloc_large_system_hash(). + */ + entries = stack_hash_mask + 1; + } + init_stack_table(entries); return 0; } @@ -205,7 +223,7 @@ int stack_depot_init(void) entries = 1UL << STACK_BUCKET_NUMBER_ORDER_MAX; pr_info("allocating hash table of %lu entries via kvcalloc\n", entries); - stack_table = kvcalloc(entries, sizeof(struct stack_record *), GFP_KERNEL); + stack_table = kvcalloc(entries, sizeof(struct list_head), GFP_KERNEL); if (!stack_table) { pr_err("hash table allocation failed, disabling\n"); stack_depot_disabled = true; @@ -213,6 +231,7 @@ int stack_depot_init(void) goto out_unlock; } stack_hash_mask = entries - 1; + init_stack_table(entries); out_unlock: mutex_unlock(&stack_depot_init_mutex); @@ -224,31 +243,24 @@ EXPORT_SYMBOL_GPL(stack_depot_init); /* Initializes a stack depol pool. */ static void depot_init_pool(void *pool) { - const int records_in_pool = DEPOT_POOL_SIZE / DEPOT_STACK_RECORD_SIZE; - int i, offset; + int offset; lockdep_assert_held_write(&pool_rwlock); - /* Initialize handles and link stack records to each other. */ - for (i = 0, offset = 0; - offset <= DEPOT_POOL_SIZE - DEPOT_STACK_RECORD_SIZE; - i++, offset += DEPOT_STACK_RECORD_SIZE) { + WARN_ON(!list_empty(&free_stacks)); + + /* Initialize handles and link stack records into the freelist. */ + for (offset = 0; offset <= DEPOT_POOL_SIZE - DEPOT_STACK_RECORD_SIZE; + offset += DEPOT_STACK_RECORD_SIZE) { struct stack_record *stack = pool + offset; stack->handle.pool_index = pools_num; stack->handle.offset = offset >> DEPOT_STACK_ALIGN; stack->handle.extra = 0; - if (i < records_in_pool - 1) - stack->next = (void *)stack + DEPOT_STACK_RECORD_SIZE; - else - stack->next = NULL; + list_add(&stack->list, &free_stacks); } - /* Link stack records into the freelist. */ - WARN_ON(next_stack); - next_stack = pool; - /* Save reference to the pool to be used by depot_fetch_stack(). */ stack_pools[pools_num] = pool; pools_num++; @@ -289,7 +301,7 @@ static bool depot_update_pools(void **prealloc) lockdep_assert_held_write(&pool_rwlock); /* Check if we still have objects in the freelist. */ - if (next_stack) + if (!list_empty(&free_stacks)) goto out_keep_prealloc; /* Check if we have a new pool saved and use it. */ @@ -340,19 +352,18 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) return NULL; /* Check if we have a stack record to save the stack trace. */ - stack = next_stack; - if (!stack) + if (list_empty(&free_stacks)) return NULL; - /* Advance the freelist. */ - next_stack = stack->next; + /* Get and unlink the first entry from the freelist. */ + stack = list_first_entry(&free_stacks, struct stack_record, list); + list_del(&stack->list); /* Limit number of saved frames to CONFIG_STACKDEPOT_MAX_FRAMES. */ if (size > CONFIG_STACKDEPOT_MAX_FRAMES) size = CONFIG_STACKDEPOT_MAX_FRAMES; /* Save the stack trace. */ - stack->next = NULL; stack->hash = hash; stack->size = size; /* stack->handle is already filled in by depot_init_pool(). */ @@ -414,15 +425,17 @@ int stackdepot_memcmp(const unsigned long *u1, const unsigned long *u2, } /* Finds a stack in a bucket of the hash table. */ -static inline struct stack_record *find_stack(struct stack_record *bucket, +static inline struct stack_record *find_stack(struct list_head *bucket, unsigned long *entries, int size, u32 hash) { + struct list_head *pos; struct stack_record *found; lockdep_assert_held(&pool_rwlock); - for (found = bucket; found; found = found->next) { + list_for_each(pos, bucket) { + found = list_entry(pos, struct stack_record, list); if (found->hash == hash && found->size == size && !stackdepot_memcmp(entries, found->entries, size)) @@ -435,7 +448,8 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, unsigned int nr_entries, gfp_t alloc_flags, bool can_alloc) { - struct stack_record *found = NULL, **bucket; + struct list_head *bucket; + struct stack_record *found = NULL; depot_stack_handle_t handle = 0; struct page *page = NULL; void *prealloc = NULL; @@ -462,7 +476,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, read_lock_irqsave(&pool_rwlock, flags); /* Fast path: look the stack trace up without full locking. */ - found = find_stack(*bucket, entries, nr_entries, hash); + found = find_stack(bucket, entries, nr_entries, hash); if (found) { read_unlock_irqrestore(&pool_rwlock, flags); goto exit; @@ -494,14 +508,13 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, write_lock_irqsave(&pool_rwlock, flags); - found = find_stack(*bucket, entries, nr_entries, hash); + found = find_stack(bucket, entries, nr_entries, hash); if (!found) { struct stack_record *new = depot_alloc_stack(entries, nr_entries, hash, &prealloc); if (new) { - new->next = *bucket; - *bucket = new; + list_add(&new->list, bucket); found = new; } } else if (prealloc) { From patchwork Mon Oct 23 16:22:44 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: 13433193 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 0711EC004C0 for ; Mon, 23 Oct 2023 16:25:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 35CF86B010C; Mon, 23 Oct 2023 12:25:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 30D426B010D; Mon, 23 Oct 2023 12:25:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1D6086B010E; Mon, 23 Oct 2023 12:25:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 0A7CD6B010C for ; Mon, 23 Oct 2023 12:25:12 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id E17681A0354 for ; Mon, 23 Oct 2023 16:25:11 +0000 (UTC) X-FDA: 81377250822.10.99795CB Received: from out-195.mta0.migadu.com (out-195.mta0.migadu.com [91.218.175.195]) by imf30.hostedemail.com (Postfix) with ESMTP id 2142880021 for ; Mon, 23 Oct 2023 16:25:09 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=kPoNUZX0; spf=pass (imf30.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-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078310; a=rsa-sha256; cv=none; b=Efbc0PhCqr5Qdx9ku4/upNj5gjYPUU16pyS1kYa63ORtt14xI+RQdHL/pIBk++i/uOAW4n nhQB+2qdh/fZNbPJWz8HcpVdupuWMJZ9Dd1ttYe7KtoMWcMOuFfq/uSpgCxnUaA4ASlcx6 UMTHn4LqtKB+h8uSAvriUe1KmTvuFaI= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=kPoNUZX0; spf=pass (imf30.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=1698078310; 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=EAtH6mtyDDwPJA3G1u2tEpoRGlDDlV29Wi3V3trZXF4=; b=mdwwzjGhL4kfkrpJkU8W25nP+fQRMkYENiPc9I+URXsfd64mdtEEArUUfEIMfhJXqlMgFZ oymGN4TF0926qvcV74/JCm23XrS6Q2Hq7hM20X8X6SqRImpZqbeTv2AIPXpwL46os79M2m RdcpbBPOgeaEOWA9v7LiJujb31+kA7k= 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=1698078308; 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=EAtH6mtyDDwPJA3G1u2tEpoRGlDDlV29Wi3V3trZXF4=; b=kPoNUZX00gmVx2jTNWUBw1MnjfYZ+Ih+Xex2SmhQ0zyGsX2Ioeza+QBykBxwXLreMwqivE M2aRr8G1yeGyH9/IfvhHARI9SQY8lVb/5X48pbhNEAEo4GD626fn7sdXdC2E92CjCX+Meu xIXcm3bRhi2HV2gkZQpZtoUQzaGzoec= 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 13/19] kmsan: use stack_depot_save instead of __stack_depot_save Date: Mon, 23 Oct 2023 18:22:44 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 2142880021 X-Stat-Signature: bfdestbwh5yz9g8xind7iy7eeasrerdu X-Rspam-User: X-HE-Tag: 1698078309-146067 X-HE-Meta: U2FsdGVkX19GUb115bej9JdZAMRtOz9XvmWvMPT9kYlw/z5haj1GnPgrFD0t45c6SNo+tPANtyXpXaivtG6LZyWJhRqhZBY3usf6tWAl/iOgur7gbxIVxmCME9+HK0zmVifCAGidH0qdwLaelQFxHlE+fzeRiA35PpKlDBOYtmC0ud0nfcOtf/RurdWgPLRo2WfKzhGbeqz461wrkpGOaA6kp39YOJioscrxt7iZxixHYoL/GWPTLswNyTPK/t2ahvbb+5mQXmdQj0R0ueYtLywjd121qgAQrjDIedEiyON/l5EJZuDBC4ZbKVZKwc4R8lCfVmh9sLMNiMYN3Tht9QEeQIxHpwDQuIIOutRAkGd9E7x0fvHmo3AYb1sTDO2RwxJGZdWepG/Nh30lhmc+ldq/xF8B5usbEwN1JkZSeYeTnPlyuAbYQbkEp7tuXl+9L9Skf/BfxW069xYA099+3iyAAzUTGJqnJZ69z3AVOs+rtpSnX23BcrR+0VHPbFLJHZz3KsVUQjZV6/7FSbH87vXDaaRW2/qQFYB0We/5ItwOczExzzqdaYlynzoG3d5h0l3vP0D6NZKHQbPsNog1omOtqwASKnSWckfQY6dquPRWnzQmpLGj836KpL7CG5IzJS0tnBQIFKUZBsJ7y6aPQKJfLm5I3CWlCz59eoWNFkOenQR1TSjB0qhOCMw8r5V2CP/55hx0CofjZedjDk6zlFxZ+g2Gj4o0M5npgwp67lCH//xRrvEEbUKgr3PwCCyIGMIOzS6XVhB6p4Us92fpXcEsUA4ihuh/cd+sC/mfEhKohR9eNUXg640msupImByhjpIXtJXbTnDu4UIWvpk17BKVkZhSi02ClJGhy7X6tMpoyd+NCtX1xR6J7vC7mCogIxIqvWYbxdF7g6h8P7g4mdzT1H14M4yU1ndzqmmTCJGYn5JXrHafl5RJnHBmvpJpwPVYB3RvdhckaQ4oRbx uyiLR3SW XaGFCn4hg1Potm+UfPcnWvC8xO/33Kj6IkxCLgqHA1BekXC61AGbv/94GS+FG6Rkvo6xod//Pa5NXKOPCJnsml4ZrPKvMPkYeMhJDG7iQ/E1WF5o63MzUTcc9n+iXyAduiqpb 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 Make KMSAN use stack_depot_save instead of __stack_depot_save, as it always passes true to __stack_depot_save as the last argument. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- Changes v1->v2: - This is a new patch. --- mm/kmsan/core.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/mm/kmsan/core.c b/mm/kmsan/core.c index 3adb4c1d3b19..5d942f19d12a 100644 --- a/mm/kmsan/core.c +++ b/mm/kmsan/core.c @@ -76,7 +76,7 @@ depot_stack_handle_t kmsan_save_stack_with_flags(gfp_t flags, /* Don't sleep. */ flags &= ~(__GFP_DIRECT_RECLAIM | __GFP_KSWAPD_RECLAIM); - handle = __stack_depot_save(entries, nr_entries, flags, true); + handle = stack_depot_save(entries, nr_entries, flags); return stack_depot_set_extra_bits(handle, extra); } @@ -250,11 +250,10 @@ depot_stack_handle_t kmsan_internal_chain_origin(depot_stack_handle_t id) /* * @entries is a local var in non-instrumented code, so KMSAN does not * know it is initialized. Explicitly unpoison it to avoid false - * positives when __stack_depot_save() passes it to instrumented code. + * positives when stack_depot_save() passes it to instrumented code. */ kmsan_internal_unpoison_memory(entries, sizeof(entries), false); - handle = __stack_depot_save(entries, ARRAY_SIZE(entries), __GFP_HIGH, - true); + handle = stack_depot_save(entries, ARRAY_SIZE(entries), __GFP_HIGH); return stack_depot_set_extra_bits(handle, extra_bits); } From patchwork Mon Oct 23 16:22:45 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: 13433195 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 CB325C25B41 for ; Mon, 23 Oct 2023 16:25:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1D7556B010D; Mon, 23 Oct 2023 12:25:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 13A8F6B010F; Mon, 23 Oct 2023 12:25:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E58A66B0111; Mon, 23 Oct 2023 12:25:13 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id A9EBB6B010D for ; Mon, 23 Oct 2023 12:25:13 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 6AFFEB5C2C for ; Mon, 23 Oct 2023 16:25:13 +0000 (UTC) X-FDA: 81377250906.18.6DA805A Received: from out-204.mta0.migadu.com (out-204.mta0.migadu.com [91.218.175.204]) by imf14.hostedemail.com (Postfix) with ESMTP id C072010003A for ; Mon, 23 Oct 2023 16:25:10 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=cQKAccVp; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf14.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.204 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=1698078310; 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=MAMDAy8Q02glfcWzfXnjTMXtIPTZ6ms/1w5eICwlRhw=; b=PvvAUEwfVd0kNKTFb0QII8oOBST1fLHkvhN8jdhh2fNP5+DBs05S0YT5seQZmlKbK3lwcf GGClJTtD64WO2iFMR2PldY4WQN1iMglDEcGCxhbuNMAFWbdV0vPrsQXi/f95fYQiqpSHIt fqfijNWLr8dnHrCQGaXwD/5oMyQfV5w= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=cQKAccVp; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf14.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.204 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078310; a=rsa-sha256; cv=none; b=4YV54idOLdj//lWCrzbgVcCNia8Iff7PlndDqCjaLaO3wwF2tDEiqZq+Jks13qzNEEXXOu c8oWLMX0sTRipJ+EbNpvOZW/i7aSQ2hrH3sY0Fl2akC5G0U0FQDvtRkHDdDmUUzrRi29tf lf9Gd7iStx6L6VT0A1Bm3RJP0DiYYIY= 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=1698078309; 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=MAMDAy8Q02glfcWzfXnjTMXtIPTZ6ms/1w5eICwlRhw=; b=cQKAccVp4i4WGbePGAJzFG2QCILkp8GkBM1OodnLLpvC71XrWxziHhpn7+OWU7s+/rAmvr cEwzqyw5M2YJ63oA5AjW1urTtz/u0Nune13U3rAkp3m2NXgelJhraa+xC4QncjzGLUzDkQ /FKPykl7hegY40JrJhmtwgrwmInGiS4= 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 14/19] lib/stackdepot, kasan: add flags to __stack_depot_save and rename Date: Mon, 23 Oct 2023 18:22:45 +0200 Message-Id: <391437de83944753819a6c0b1d95bd7aa55ea106.1698077459.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: C072010003A X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 3u3dwxedp6jxui3pb7rtkpusr63sii4k X-HE-Tag: 1698078310-330917 X-HE-Meta: U2FsdGVkX19htqLXfUqietZDojdJmEa7Zyidrmlj5lbZItq6ZoiaLan46tnmJ0EGYc0JKQfnRFjUDvXUPEU43zSvdNrDjGqL7foX6Fr1oHogDdpWL8pgFlrvbE7t9pDyqcjSxAHT+NDmqEf6h38zCR6krzv1dL6g08L4T9qT8hXgllQweIl5XL9CMUDqT810OvjWFsUieCQ+1qqJV1bBGE5dn+FeXbacIhJKBcVSLlfxMn8ON9h8okbOWc1QZkGGVubsaT1+po3KNtqGy4qfrCWZdmeNr+Ydu4Cn4bXs2RGmm+HCMzqdqNzr85zjmiOVsQc3pNccqi2cQsv+eeAsUK3vc99Ml5hgXBfOrUb/JSqHNY7OyH6PHpIA8V6sGxkfUcq0t9jj24OW8Hs5oHTn5S+lEuplPVu1j1T5+R9stWwXZmqSlZNAdDJFDBl0Ork9S9gfNmgeZfSiIv/92NQyrRD39jy9ypXGBOONulNZ91XZ6F/WTjO+ddJ7jH2pGreMzlIM+EIMt8NHLEpvN07TlcV2wzd1MQXcBHR9+qFOSD4kWqeO7KqVE9Qe5+0fzyeQH3FtIyU+FYgHG1qvhrsIcjqduCXM0AL0zpzeCfNTtqZAhYGUUS3x7FvDDteuIbOTFR9DOItaLJ416BEBigG7A3ohrmnzZ21sPJLLUPoew/xWf+5Btl7Qlb5319fFT+7S6Oq6pBaM4tyGftHe9+vqicccBQFsoZkReVcr0GlG8b0KVlBAsXlsptp7FQQakZoyj+XL354/cuewnMh7yCYZBdRAxyZyUrdUlTw1q8tmWt9m39/15BeqEeY0SD6MOvl+cvS8IRaxD6/1WGTdZyNynGPdZl+ffz9/HAdaGUWGHJX5nFgQij0KKaO64mnIBgANCH+0iN6CsOIhhemQ0jfF7LMAhwoEJgLlVk0+ZoyAa58zcrFa9IdhwGE1cY5ant2DpEpKVwxOB9McJ3eFP4u Gg1s5owC puQ9jLe4po81btygM3W1IvHs8/W8OlIVK8o5B1pdwCrJMZhVAktHYBgjgHZ9dnAjjaVtJ7a/aIoEOUkl2WDfL/KiXAxucGAGoEyUtH1rN26eWfjjez2hygOnJYJhBjzX9i011zwNt8ZO5xDg= 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 Change the bool can_alloc argument of __stack_depot_save to a u32 argument that accepts a set of flags. The following patch will add another flag to stack_depot_save_flags besides the existing STACK_DEPOT_FLAG_CAN_ALLOC. Also rename the function to stack_depot_save_flags, as __stack_depot_save is a cryptic name, Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- Changes v2->v3: - WARN_ON invalid flags in stack_depot_save_flags. Changes v1->v2: - This is a new patch. --- include/linux/stackdepot.h | 36 +++++++++++++++++++++++++----------- lib/stackdepot.c | 16 +++++++++++----- mm/kasan/common.c | 7 ++++--- mm/kasan/generic.c | 9 +++++---- mm/kasan/kasan.h | 2 +- mm/kasan/tags.c | 3 ++- 6 files changed, 48 insertions(+), 25 deletions(-) diff --git a/include/linux/stackdepot.h b/include/linux/stackdepot.h index e58306783d8e..0b262e14144e 100644 --- a/include/linux/stackdepot.h +++ b/include/linux/stackdepot.h @@ -32,6 +32,17 @@ typedef u32 depot_stack_handle_t; */ #define STACK_DEPOT_EXTRA_BITS 5 +typedef u32 depot_flags_t; + +/* + * Flags that can be passed to stack_depot_save_flags(); see the comment next + * to its declaration for more details. + */ +#define STACK_DEPOT_FLAG_CAN_ALLOC ((depot_flags_t)0x0001) + +#define STACK_DEPOT_FLAGS_NUM 1 +#define STACK_DEPOT_FLAGS_MASK ((depot_flags_t)((1 << STACK_DEPOT_FLAGS_NUM) - 1)) + /* * Using stack depot requires its initialization, which can be done in 3 ways: * @@ -69,31 +80,34 @@ static inline int stack_depot_early_init(void) { return 0; } #endif /** - * __stack_depot_save - Save a stack trace to stack depot + * stack_depot_save_flags - Save a stack trace to stack depot * * @entries: Pointer to the stack trace * @nr_entries: Number of frames in the stack * @alloc_flags: Allocation GFP flags - * @can_alloc: Allocate stack pools (increased chance of failure if false) + * @depot_flags: Stack depot flags + * + * Saves a stack trace from @entries array of size @nr_entries. * - * Saves a stack trace from @entries array of size @nr_entries. If @can_alloc is - * %true, stack depot can replenish the stack pools in case no space is left - * (allocates using GFP flags of @alloc_flags). If @can_alloc is %false, avoids - * any allocations and fails if no space is left to store the stack trace. + * If STACK_DEPOT_FLAG_CAN_ALLOC is set in @depot_flags, stack depot can + * replenish the stack pools in case no space is left (allocates using GFP + * flags of @alloc_flags). Otherwise, stack depot avoids any allocations and + * fails if no space is left to store the stack trace. * * If the provided stack trace comes from the interrupt context, only the part * up to the interrupt entry is saved. * - * Context: Any context, but setting @can_alloc to %false is required if + * Context: Any context, but setting STACK_DEPOT_FLAG_CAN_ALLOC is required if * alloc_pages() cannot be used from the current context. Currently * this is the case for contexts where neither %GFP_ATOMIC nor * %GFP_NOWAIT can be used (NMI, raw_spin_lock). * * Return: Handle of the stack struct stored in depot, 0 on failure */ -depot_stack_handle_t __stack_depot_save(unsigned long *entries, - unsigned int nr_entries, - gfp_t gfp_flags, bool can_alloc); +depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, + unsigned int nr_entries, + gfp_t gfp_flags, + depot_flags_t depot_flags); /** * stack_depot_save - Save a stack trace to stack depot @@ -103,7 +117,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, * @alloc_flags: Allocation GFP flags * * Context: Contexts where allocations via alloc_pages() are allowed. - * See __stack_depot_save() for more details. + * 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 85fd40c63817..902d69d3ee30 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -444,19 +444,24 @@ static inline struct stack_record *find_stack(struct list_head *bucket, return NULL; } -depot_stack_handle_t __stack_depot_save(unsigned long *entries, - unsigned int nr_entries, - gfp_t alloc_flags, bool can_alloc) +depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, + unsigned int nr_entries, + gfp_t alloc_flags, + depot_flags_t depot_flags) { struct list_head *bucket; struct stack_record *found = NULL; depot_stack_handle_t handle = 0; struct page *page = NULL; void *prealloc = NULL; + bool can_alloc = depot_flags & STACK_DEPOT_FLAG_CAN_ALLOC; bool need_alloc = false; unsigned long flags; u32 hash; + if (WARN_ON(depot_flags & ~STACK_DEPOT_FLAGS_MASK)) + return 0; + /* * If this stack trace is from an interrupt, including anything before * interrupt entry usually leads to unbounded stack depot growth. @@ -535,13 +540,14 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, handle = found->handle.handle; return handle; } -EXPORT_SYMBOL_GPL(__stack_depot_save); +EXPORT_SYMBOL_GPL(stack_depot_save_flags); depot_stack_handle_t stack_depot_save(unsigned long *entries, unsigned int nr_entries, gfp_t alloc_flags) { - return __stack_depot_save(entries, nr_entries, alloc_flags, true); + return stack_depot_save_flags(entries, nr_entries, alloc_flags, + STACK_DEPOT_FLAG_CAN_ALLOC); } EXPORT_SYMBOL_GPL(stack_depot_save); diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 256930da578a..825a0240ec02 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -37,19 +38,19 @@ struct slab *kasan_addr_to_slab(const void *addr) return NULL; } -depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc) +depot_stack_handle_t kasan_save_stack(gfp_t flags, depot_flags_t depot_flags) { unsigned long entries[KASAN_STACK_DEPTH]; unsigned int nr_entries; nr_entries = stack_trace_save(entries, ARRAY_SIZE(entries), 0); - return __stack_depot_save(entries, nr_entries, flags, can_alloc); + return stack_depot_save_flags(entries, nr_entries, flags, depot_flags); } void kasan_set_track(struct kasan_track *track, gfp_t flags) { track->pid = current->pid; - track->stack = kasan_save_stack(flags, true); + track->stack = kasan_save_stack(flags, STACK_DEPOT_FLAG_CAN_ALLOC); } #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index 4d837ab83f08..5d168c9afb32 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -472,7 +473,7 @@ size_t kasan_metadata_size(struct kmem_cache *cache, bool in_object) sizeof(struct kasan_free_meta) : 0); } -static void __kasan_record_aux_stack(void *addr, bool can_alloc) +static void __kasan_record_aux_stack(void *addr, depot_flags_t depot_flags) { struct slab *slab = kasan_addr_to_slab(addr); struct kmem_cache *cache; @@ -489,17 +490,17 @@ static void __kasan_record_aux_stack(void *addr, bool can_alloc) return; alloc_meta->aux_stack[1] = alloc_meta->aux_stack[0]; - alloc_meta->aux_stack[0] = kasan_save_stack(0, can_alloc); + alloc_meta->aux_stack[0] = kasan_save_stack(0, depot_flags); } void kasan_record_aux_stack(void *addr) { - return __kasan_record_aux_stack(addr, true); + return __kasan_record_aux_stack(addr, STACK_DEPOT_FLAG_CAN_ALLOC); } void kasan_record_aux_stack_noalloc(void *addr) { - return __kasan_record_aux_stack(addr, false); + return __kasan_record_aux_stack(addr, 0); } void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) diff --git a/mm/kasan/kasan.h b/mm/kasan/kasan.h index d37831b8511c..3787266d9794 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -368,7 +368,7 @@ static inline void kasan_init_cache_meta(struct kmem_cache *cache, unsigned int static inline void kasan_init_object_meta(struct kmem_cache *cache, const void *object) { } #endif -depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc); +depot_stack_handle_t kasan_save_stack(gfp_t flags, depot_flags_t depot_flags); void kasan_set_track(struct kasan_track *track, gfp_t flags); void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags); void kasan_save_free_info(struct kmem_cache *cache, void *object); diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index 7dcfe341d48e..4fd32121b0fd 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -101,7 +102,7 @@ static void save_stack_info(struct kmem_cache *cache, void *object, struct kasan_stack_ring_entry *entry; void *old_ptr; - stack = kasan_save_stack(gfp_flags, true); + stack = kasan_save_stack(gfp_flags, STACK_DEPOT_FLAG_CAN_ALLOC); /* * Prevent save_stack_info() from modifying stack ring 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); From patchwork Mon Oct 23 16:22:47 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: 13433196 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 07E71C25B43 for ; Mon, 23 Oct 2023 16:25:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 92A256B0110; Mon, 23 Oct 2023 12:25:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8D99B6B0112; Mon, 23 Oct 2023 12:25:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6696E6B0111; Mon, 23 Oct 2023 12:25:14 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 4D89D6B010F for ; Mon, 23 Oct 2023 12:25:14 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id EA593A0A94 for ; Mon, 23 Oct 2023 16:25:13 +0000 (UTC) X-FDA: 81377250906.04.B72D3D3 Received: from out-203.mta0.migadu.com (out-203.mta0.migadu.com [91.218.175.203]) by imf16.hostedemail.com (Postfix) with ESMTP id 013B4180030 for ; Mon, 23 Oct 2023 16:25:11 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="oyH/s4RT"; spf=pass (imf16.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.203 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=1698078312; 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=KyQaV2ck0CZBTZjrz6rGoXlFfvUUwlefDCsA1d1OnDI=; b=bEjGHZewFqIG1GmPbnksuH+0OVJ7jqtgDlSxM9lwLOJlaDQVZGhzvFZNJ/kyJFenY5xoUI zSSYPg+JZ0GAHiyvKFDRXXxUjr9pIsc0tjIVnyd587wOWzUrYbtPUeJvQWhvawmE2fuoK6 VbtLtZYgF4aBzaOq6gLR79BaONT4jgQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078312; a=rsa-sha256; cv=none; b=n+uuxy9uPkDA5lx3A1SeUoLY1K+LJ3zmV6aoIzQXT/HqooHfpuNfgFzKePCiFXD6vc5ji/ 1/K3OmVipxBd6fm3FymF4tAGEpVlHw59Czl01nUU+asyp9uY9o72PFUkHQjxCHjw9sAwJd jTgITmJs/VdCBA1dp0GIDMht9lOU9Jo= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="oyH/s4RT"; spf=pass (imf16.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.203 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=KyQaV2ck0CZBTZjrz6rGoXlFfvUUwlefDCsA1d1OnDI=; b=oyH/s4RTev5TTn2tx92NM3vkXg6oUk6Kpicty1qVqAfYRd+524Z47P2qCKhvUIRV4y3uiL rlsDYYtewCtLkLPB0dG2UOn/EJuLBsAiSUiToU+oaYgXFZHxJ+CthWgKW++yb5No2yIg9Y u1Z8kAH4ZHqywmyABUdVN11IBWKHboo= 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 16/19] lib/stackdepot: allow users to evict stack traces Date: Mon, 23 Oct 2023 18:22:47 +0200 Message-Id: <8ba201688d0e4b8f3ec56b426bb350965a30c8fd.1698077459.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Stat-Signature: zridzokxsxs13crb41kbu4ua5gbxz8hs X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 013B4180030 X-Rspam-User: X-HE-Tag: 1698078311-735068 X-HE-Meta: U2FsdGVkX189F02R6fhdfmqANDKUM5pk5mMgnJIlqh05QB/7Mo8RVea1whHac6fe2FA1Brf2uTsTTbw7ANPKgp0phsKqC/ruhD0+6hKsgqeVte3Dllu1rYE97wSG50o0F7xvomjx4fS63lkBlAL8/p85Dl7+DRcHOEw+okhCkSEi8W8GojrTHE/t12k2RDcnqo8Vd6pw7aVSAc7Kf/lDTfDVLv5/zi4aKYBJV+5Svii1fRPvNepAfoaTH/pISsdSFTRxvYsWT8opW5akNDFLRftzS3dd1h338J/YAo6oOV3schu2T4Imk2vKZPezMk19PnCSeFRI88N5gahHX1E1v147t6UDiOcFTsLW9L26+FCTpZPh68JR/59KQee3wPHITotKSBVFfo+C104VmVRs62UwuAsYEZSsxhWNdH1AI2UrUgyeEKKSvLi/N6oUtzznTiB3qIbSu00UcaRUaWO6kxticEIi3zvWACcmu9wrxguTe23gmMEmYpAGR0004NTZHdH+YKQ2WuZ7/nRnfPhI2AEbDMfKsiSNMnVma02AYBYgG3VHtOBFj8VwiUQ6t+79owtRmQSQiQQW6QV20X8f14es7Z9cPgMU4O0+ont3d3jKlVbpd31IsR7KCwg28enINVeEctYW9UO1NGG5cYelwoTu0FWbXv5G3oN9MWHr9wgle0cNEbBLIpEAr42aeS78nkNFZrF3BrIAMKdd01z6ASCB32ydTjtsQ8gYSaNqVTIePqGEyP9HiqKPXW6WO8UEvjngoXghwxuzuplkwVwZc0JFWCRwKLKDoHIgk7c0dyjg3H1XsLUAboASpHxmNPAY37nNEfH6+q+F/KBH9IbGnpQk1ajz6Dv57KMNndP9YZRVJ8hCgSaxnR9beBP9Wg9e4JyIvCsuGzgCW625Ji5tP/HUYSmUsmKMLyH3mowpE19PBsSypGmvY8n6OmRX+it3dHAfHLq5dOG/2s0njki bGmPhhtL etl/hB+i6lOrx/mn/p3iDs/ltwFsfv2uRRA8LT73kNtBUILGEcxNFxJEoqIScHy/kuAS8deoLdi7sv2cX9xKN1wljfg== 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 | 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 278ed646e418..3a8f045696fd 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -404,6 +404,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) { @@ -586,6 +594,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; From patchwork Mon Oct 23 16:22:48 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: 13433197 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 1F0ACC004C0 for ; Mon, 23 Oct 2023 16:25:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE71D6B0111; Mon, 23 Oct 2023 12:25:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D45F26B0112; Mon, 23 Oct 2023 12:25:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C0D816B0113; Mon, 23 Oct 2023 12:25:14 -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 A283F6B0111 for ; Mon, 23 Oct 2023 12:25:14 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 778831202D7 for ; Mon, 23 Oct 2023 16:25:14 +0000 (UTC) X-FDA: 81377250948.25.11750B4 Received: from out-207.mta0.migadu.com (out-207.mta0.migadu.com [91.218.175.207]) by imf21.hostedemail.com (Postfix) with ESMTP id A1FD01C0021 for ; Mon, 23 Oct 2023 16:25:12 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=CQBo9f5r; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf21.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.207 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=1698078312; 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=h9lSbnFVvjj2GZw0LrViVCVgXWgHHPxOTEIrM79fNvQ=; b=ob2IHi1GLbZzJc6V2fZutVodCSoNuymoeYkZtIJTKmUiV2sMquzVOKK10NLBBcSnVozCOD bRgBPV6wvJcihgxxHsLvLfAXFYPg/4yDDuoHz8xg5LDLjEopr2Ib8uufvHYGnSvuv3hIqk +GZBAmUHp2yZuTfZTK0Ww4xi6G67A0c= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=CQBo9f5r; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf21.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.207 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078312; a=rsa-sha256; cv=none; b=SpbwrEZfRPsKr1hGQq1TIbE9WqYXyZIWJ2m260ZoO17ficBA8eHhiwiHugPMiyf1krq/BE A3RkwuacIt1MFvCvLvR0EQCht7eTLvXrAKmvbI/m++/nFICCzJ0JwEdICMfAOaJjDu3wb2 g/jflwMgAHG8/bX7DPF2H2lz9Q+Vi2Q= 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=1698078311; 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=h9lSbnFVvjj2GZw0LrViVCVgXWgHHPxOTEIrM79fNvQ=; b=CQBo9f5r78HDYIzuMKRdcegVRv3KmK60h3RNuj3rljg/vJLY0On77Rw0SAXFAcOXknm+Yn sWYecQHmhDaiksWaRwRkX2AT+Gd9TdjhzprwlFBsP9luxtS1bbrxbl6j0iwAjY4bMTGHZc IWeq/EXSKTJEUgePln3o5gvif7vZcqM= 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 17/19] kasan: remove atomic accesses to stack ring entries Date: Mon, 23 Oct 2023 18:22:48 +0200 Message-Id: <81b8b7984846e0e7abfd794aad3bafee97c89c29.1698077459.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: A1FD01C0021 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: r5c73exemhw7uk3w43yfur65b1wsatg1 X-HE-Tag: 1698078312-488182 X-HE-Meta: U2FsdGVkX18G7yvAMAk5UtWSc5eR/Si8j+TzGMx9okRiJZBnt+JAKYvKoVb7rIdxHqTSQgs746F++Z8UdCcjsxJ1uaVvBujia5jytHHV0Ajj7Y09kI87SnUT6B1ph22whKy6eArkF0ei6uQHGR1k3ekVQOr1GVH4onlc9WAVcg7/m4YgLVqx9M6Kce/FBMlghZU9uJlZER1nn879o6Un1nLR/5bivdUaPlkJRJov4w4HbM0ZPOBkFIYl11fiYI3DbhbKmh+Ix05ySS5rmt2mnUl/CELsQ9iG4hdq6OuUBGU7gfIsHFIi7RsjcuQcSEOKRivhHIIDAetcwgdQTVse4gJfCOM8jYG0qyXzCMaZLYjnEgXRUk2Fc6z8notUopuOsq7wq3tcOkNQ7mhmtURG3ssuBxqQtP1ZfOKhtTfQd1uce/ujma0ygyf+zapp6dvd5Di1GCvLqaAJ3IBbQVus1pGT3kbil4R7Qg+GOHUJLEnxqihkY3cOLWnvzWpr4DbnKL2a/dx2MeBUC3c8LZKnhLS7rCYbgOgR2w6/frDcY1NXn/0rK2G671W+q1ARr9cjN7BGSCXIINdvvBaQPWSPvpMXAaZp93Z6Q4kbeGi30RWnn54JfxyWhXUxpIfPUoc5MHeQMvneS4k6RhOKOnpIpNWU6CaCIaCjyEpWaqE2Eob8C+2nnPEcYp228yYZadoiaiK40QDO1VjpNMmmWQpLBdmBTarHNMqXpzeN95Un+GSpRLk77VqwnLopC9YoDpwSu1SMhT08LKqXEqIQ7R0iMpCaYYy79QLu0gG8dbkLXTdBE3jULwIMHHjaJEs9HLtfaU5uAGWwqsKRN0TDJFbRgAAiOznuw+sogRCuTEGxkEIfi/Q5314ieOXXv1Q6DuHmuRr91ZJhxDAeLWmrN5WYMLmGBgeN74ZImqpj9YMOd4G5thIgJ17wZ8arteIZ+UflymzbC/PM6Lut8fqwwjt LKUBirvk CbQnfYrdIoqhFxuoiJBnaTrt1BUy9T6ZLfOKM9XQYOXMYI3RDcR2EAL3UyB3vK/meED66PkiaqY6W9AOduNNQdilnI2BknubU8TrS 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 Remove the atomic accesses to entry fields in save_stack_info and kasan_complete_mode_report_info for tag-based KASAN modes. These atomics are not required, as the read/write lock prevents the entries from being read (in kasan_complete_mode_report_info) while being written (in save_stack_info) and the try_cmpxchg prevents the same entry from being rewritten (in save_stack_info) in the unlikely case of wrapping during writing. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- Changes v1->v2: - This is a new patch. --- mm/kasan/report_tags.c | 25 +++++++------------------ mm/kasan/tags.c | 13 +++++-------- 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c index 8b8bfdb3cfdb..78abdcde5da9 100644 --- a/mm/kasan/report_tags.c +++ b/mm/kasan/report_tags.c @@ -31,10 +31,6 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) unsigned long flags; u64 pos; struct kasan_stack_ring_entry *entry; - void *ptr; - u32 pid; - depot_stack_handle_t stack; - bool is_free; bool alloc_found = false, free_found = false; if ((!info->cache || !info->object) && !info->bug_type) { @@ -61,18 +57,11 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) entry = &stack_ring.entries[i % stack_ring.size]; - /* Paired with smp_store_release() in save_stack_info(). */ - ptr = (void *)smp_load_acquire(&entry->ptr); - - if (kasan_reset_tag(ptr) != info->object || - get_tag(ptr) != get_tag(info->access_addr)) + if (kasan_reset_tag(entry->ptr) != info->object || + get_tag(entry->ptr) != get_tag(info->access_addr)) continue; - pid = READ_ONCE(entry->pid); - stack = READ_ONCE(entry->stack); - is_free = READ_ONCE(entry->is_free); - - if (is_free) { + if (entry->is_free) { /* * Second free of the same object. * Give up on trying to find the alloc entry. @@ -80,8 +69,8 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) if (free_found) break; - info->free_track.pid = pid; - info->free_track.stack = stack; + info->free_track.pid = entry->pid; + info->free_track.stack = entry->stack; free_found = true; /* @@ -95,8 +84,8 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) if (alloc_found) break; - info->alloc_track.pid = pid; - info->alloc_track.stack = stack; + info->alloc_track.pid = entry->pid; + info->alloc_track.stack = entry->stack; alloc_found = true; /* diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index 4fd32121b0fd..b6c017e670d8 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -121,15 +121,12 @@ static void save_stack_info(struct kmem_cache *cache, void *object, if (!try_cmpxchg(&entry->ptr, &old_ptr, STACK_RING_BUSY_PTR)) goto next; /* Busy slot. */ - WRITE_ONCE(entry->size, cache->object_size); - WRITE_ONCE(entry->pid, current->pid); - WRITE_ONCE(entry->stack, stack); - WRITE_ONCE(entry->is_free, is_free); + entry->size = cache->object_size; + entry->pid = current->pid; + entry->stack = stack; + entry->is_free = is_free; - /* - * Paired with smp_load_acquire() in kasan_complete_mode_report_info(). - */ - smp_store_release(&entry->ptr, (s64)object); + entry->ptr = object; read_unlock_irqrestore(&stack_ring.lock, flags); } From patchwork Mon Oct 23 16:22:49 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: 13433198 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 2E99BC001E0 for ; Mon, 23 Oct 2023 16:26:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B9B866B0117; Mon, 23 Oct 2023 12:26:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B4ADF6B0118; Mon, 23 Oct 2023 12:26:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A3A3A6B0119; Mon, 23 Oct 2023 12:26:16 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 913A06B0117 for ; Mon, 23 Oct 2023 12:26:16 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 5EE24B5C49 for ; Mon, 23 Oct 2023 16:26:16 +0000 (UTC) X-FDA: 81377253552.12.CAFE17C Received: from out-194.mta1.migadu.com (out-194.mta1.migadu.com [95.215.58.194]) by imf10.hostedemail.com (Postfix) with ESMTP id 96C6CC001C for ; Mon, 23 Oct 2023 16:26:14 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Ne3J1RXv; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf10.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.194 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=1698078374; 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=uhMrVBrzgAoT1gwViC8KxLkH9OMZhix/uhvoEc70W98=; b=cB4FiwHZpoFxe68+G+XodRVGwWmz9mJCBAhLreNnTMLkM2t15hDQev3lfiNeQgcVOEusuO oUcrXHcgSNPd/XdpXA6SXAsFn8UddkVsm5BrN5va4NTOwg1egQN9NQM6GE/+YzdocprXyF XA4ix5PI9VzNVHfSPm6BHLtGLZdywWw= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Ne3J1RXv; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf10.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.194 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078374; a=rsa-sha256; cv=none; b=CoK/GZGPIuS7QNovPkcaQWeuONCN+7v25qmTL3Yb9fQns/gKR27oab9YMlF3yMFW/NcKmt WG2fuQ9XtVOUk5H1Z4MXrJPqIOckBaJZR6cTyk2t6oitFktbu1eZXToIjXC7kXM0OyrLvZ FVvrzzmkp5PAYkc1DT3thLIDORLJTq0= 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=1698078373; 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=uhMrVBrzgAoT1gwViC8KxLkH9OMZhix/uhvoEc70W98=; b=Ne3J1RXv7CfIkYMjEOEAo2MOXXrLjJl0MG/nn5TuOVSCeJC7ACQ8Zc+U0+7fVaprnL1TwV YcPWlAN0bBL1/0CJWM+oWPOfzdH5fmC91XjSmronfj3U3HMmSByKdvCQOOSWPbS9RJ3nc5 z58ccvrjKa1GY9FZizpTtMWzArqCzuI= 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 18/19] kasan: check object_size in kasan_complete_mode_report_info Date: Mon, 23 Oct 2023 18:22:49 +0200 Message-Id: <97721c54e9ccacc494dbcfcd0cbd5f5aaab6973b.1698077459.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 96C6CC001C X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 3zood9smphts91cx75uhckeasmownjyd X-HE-Tag: 1698078374-506509 X-HE-Meta: U2FsdGVkX1/HWsVfVT3tWUXlstqC0cPf5bjzu8dRhBEk8YIGuxkby9Ou9l/Cc11/EUlkS2ONuECuIrW5fIlNz45aDKBgOsGDQs5/OijzVYZxfboae8fAUbb5O883lM7IxkfSgvTgs8CT3OdGRsnU0ld693QG/z155/i9vZZyqirZzCzONlF2K66v9HflFb6Sa+mq7BbDkYh4+Z+/JEXaOxnGSLZqijSaoqvXmNcDwcTY9P8bOyE0hidEsWvix/nQXzAEjZXsjA97i53pPjM4HYW7y80eAkCEi1xBFLLer6RYbJwzBEVq8A96HmBEzV0WLYFCwNwLn9nLF42r317bhxElEiIjKsrmUe5os9RHDewYfMioghepxNOPsZn0vbxdPP2aqQNrUtTUEBYemic/zTqeLdBEfLooKrGzQ9FDx9wvdJHimm9BZizwHDAW65dfE6uO5MgkiKM99x0NFc4zTx9oqlEN7w/Y6y5+6B95X9I/5BWBBqIWiSnVPojjF8ZGW1zpFm73xVVnZPuyR0U6E7LdxqeC6fPhtapm8tkG1J9RX49w397U6YpfujBcWH2f4tk434L3mrNDv6JiJHfYd6cnbYl5sU/FDfMUu+Wd+iqoQLxLvUJDwQf6+Lt+lfnLtNyZoCp+q60+tOnCNBcIcfBOZCaTB4ZAdIc2lpB0eBCL21FU6f6DWrYHQQsOZq6EHJQ/m9OnFK6ZBVFr7NAPZ6IUoPTHIJ/Jihcqcs1pwpJZJhWLplhSH0fcBP/bm+wBU7k3ZLljMCKShtFnBbeb0lx/kNUiY0x/Tkk0un5t3iBCfmTGQzMsJRGIu6LJmPZ9jLvdVgmQV4PfWsR4AWfF9lqCrLDzVEwpdVsPzXu/5H2jPm48bOnq85JXwWlbjFTK9EsS3Zd2f/ht+7wBXK+RqcLEwY3mw8CN2F2xt2FU3KUjJrvkGpnSOck8vBo+NE8Se2ViGXQi8UB/IyIQFOf SkwRRngL SJk0YWRp6CIgK6XR+Psc/lwZ984Ay602x+zic6wZyVBN+CnoTB4Ju7bKRYWH43tTWDLk9IDrDqiC80Fidfm6RvnbcbcAdFbr02rp88tfyheNtPYcsBLx7V/3mpO51EP6QclBEOrajOdhiErhpVZT17X+2PBI41vc+lFGkqfE99M1TRA8drUVYQRrofw== 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 Check the object size when looking up entries in the stack ring. If the size of the object for which a report is being printed does not match the size of the object for which a stack trace has been saved in the stack ring, the saved stack trace is irrelevant. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- Changes v2->v3: - Added missing "../slab.h" include for accessing a kmem_cache field. Changes v1->v2: - This is a new patch. --- mm/kasan/report_tags.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c index 78abdcde5da9..55154743f915 100644 --- a/mm/kasan/report_tags.c +++ b/mm/kasan/report_tags.c @@ -7,6 +7,7 @@ #include #include "kasan.h" +#include "../slab.h" extern struct kasan_stack_ring stack_ring; @@ -58,7 +59,8 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) entry = &stack_ring.entries[i % stack_ring.size]; if (kasan_reset_tag(entry->ptr) != info->object || - get_tag(entry->ptr) != get_tag(info->access_addr)) + get_tag(entry->ptr) != get_tag(info->access_addr) || + info->cache->object_size != entry->size) continue; if (entry->is_free) { From patchwork Mon Oct 23 16:22:50 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: 13433199 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 3180AC25B41 for ; Mon, 23 Oct 2023 16:26:18 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9A0DB6B0118; Mon, 23 Oct 2023 12:26:17 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 928D66B0119; Mon, 23 Oct 2023 12:26:17 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7078E6B011A; Mon, 23 Oct 2023 12:26:17 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 516166B0118 for ; Mon, 23 Oct 2023 12:26:17 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 30837A0AC8 for ; Mon, 23 Oct 2023 16:26:17 +0000 (UTC) X-FDA: 81377253594.07.18E600B Received: from out-197.mta1.migadu.com (out-197.mta1.migadu.com [95.215.58.197]) by imf19.hostedemail.com (Postfix) with ESMTP id 512DE1A001F for ; Mon, 23 Oct 2023 16:26:15 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="Tr/G6YIY"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf19.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.197 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=1698078375; 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=Qc2DDhk7eSddbt2Q3kjJLSbCNGbLZWOl9JsGZF81sG4=; b=S7QVzQSwLk0Yjupwq9cv8p16IHoqlBiMo5dSgIjzMMU4KzbLo9h7hab8cncjAB4ELaPmoC UwHC/19n5+gLx3HCO+pnRFp+a9g7D135EtTA4Rz7YOnC6L+14QGWlJWdeDK8TpEeIbL8cN AjMFlfLudr9HthLDMOang4uTY0y2sEY= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="Tr/G6YIY"; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf19.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.197 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1698078375; a=rsa-sha256; cv=none; b=iNkVNn7pBPNYE3MebU4AJrA7DB+6rGRwC2Y7W5YK9hzkfKWIPlnmMK0CBuO3YVn9KZU5zD r101R5s7JsurjoXoVYls9g9UQb7fKROQ5ycBzgzHl3GzfeCUHreqf4y52NZNh+XgXtkN0Y HkCEUCKj9uW2zmJO1l7QcA9rPqNzXfk= 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=1698078373; 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=Qc2DDhk7eSddbt2Q3kjJLSbCNGbLZWOl9JsGZF81sG4=; b=Tr/G6YIYPrWXP9fx3O7aG2jl6kXfEVrm47rOgoEDzFX/BZ50Z23L4IXwsR4vCJshDX5TDZ HrweRnvTS3UanFhDAX4boFO1pOlEosl/bvhOjewwp0T7l51nrmSzESgRmuTG8srBHitAMu 47CJygk1kptnKFhCWZImQgFTsPZh9NY= 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 19/19] kasan: use stack_depot_put for tag-based modes Date: Mon, 23 Oct 2023 18:22:50 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 512DE1A001F X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: stnpi6rg1yr78x9xz8iob1p4xms9gyeu X-HE-Tag: 1698078375-791442 X-HE-Meta: U2FsdGVkX1+6VD2sEUmm5azYMV3NVCzqiIHl49ows2Vh18l1PHhpiLcEY+qc92v32orMEJI3oplJaH/DKAfdn2X+thVHhwPeneqpFuN+uYLHNwnKodL2MuJqpT6IUgUjSdP7g5G4EPUU3AuaCeCFacLjJEl42/tnWKKkN177DuduLM0Cl9xRIk04Knpz0oeirtxXV4wuNRu0IzSX7o6aAo+bo5fRfRgen72UMpROuydBNpaDEdjltJBjwT2/iZiY1K1iQ2wE6NLjajkjBGCe1JVNo4rguFVN994dGi6oR7H7ovw/AwoUqIjX3Y+3a5sYKB4DtGcDUTG4xmw/VGHWXTbSLUuJZvD3X8eEtGGY3x5IvFWYvdBY9LkCoGduYQyQzoxUk0HpOUvCo6hgxIymNZBwxmGsNYxvcBk2DWmR2uuRVBQqLDpNtIlMvw/sWXfxmZ1tDE7Gohe2xKRSNnDDwUrnoR+Qjx6NN+Xo135RAWm8tr2VMK9TMcueB0uj/g95iEQHsPEF2k33UuN8FfUPIxVlQ0kFmMi3N6aVl2yoYo/nAFb1mOpIZkLRTD6oX9xMjD8p/xWiswIgmso93bBzs4mJ3wM+6VkLyBKzyTkCs89XrEXucgbRRKFikMYDcc+ATdR5iOZJNW8F2XRDuDzPmr7l4uze/QwTImL6N1RcoP/DpH/xp4/l9YPdjpvVJ8e5F5jm/NZpNa7lhKiokPHi1ah+XxZ45gP6WgXalvm5ycINMTOjbgr9Ss5eEq0T/DVYqW28sPd1DqejiRB8jmnPS7jU8xFfgWRJwNG0fy+Lg/KNHSz+E/uCSadcBHV0Z8fkK505SNeNTNtrBZIdckSoOQkwSg26ekuP+hllWKsRfzAeLrf45RGM7+wRHvoS+8PliZUQ9L6FQqgXCSJ/GVP/IHjPm8nuDM3xqcX7u0kNaVNSGwcwhV9yf/50/jDsCqe7zGahdiuimKtdmJXTtm+ gR5FHzAB mXTw6gxhL58DCajX9V9C1EM+hbJsFNh1XwQQyHSdIOXzy2dwyyrpQ6lGB39UEHD/PR5DxVcSogpJpybTMCjTABhETjgpZMKyNeJcvUYyURaI+o6OgpDMHNBMbjuLTkBdaoAYvrdhSRR3NaFlWZNPF5BTC9A== 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 Make tag-based KASAN modes evict stack traces from the stack depot once they are evicted from the stack ring. Internally, pass STACK_DEPOT_FLAG_GET to stack_depot_save_flags (via kasan_save_stack) to increment the refcount when saving a new entry to stack ring and call stack_depot_put when removing an entry from stack ring. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- Changes v1->v2: - Adapt to the stack depot API change. - Drop READ_ONCE when reading entry->stack. --- mm/kasan/tags.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index b6c017e670d8..739ae997463d 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -97,12 +97,13 @@ static void save_stack_info(struct kmem_cache *cache, void *object, gfp_t gfp_flags, bool is_free) { unsigned long flags; - depot_stack_handle_t stack; + depot_stack_handle_t stack, old_stack; u64 pos; struct kasan_stack_ring_entry *entry; void *old_ptr; - stack = kasan_save_stack(gfp_flags, STACK_DEPOT_FLAG_CAN_ALLOC); + stack = kasan_save_stack(gfp_flags, + STACK_DEPOT_FLAG_CAN_ALLOC | STACK_DEPOT_FLAG_GET); /* * Prevent save_stack_info() from modifying stack ring @@ -121,6 +122,8 @@ static void save_stack_info(struct kmem_cache *cache, void *object, if (!try_cmpxchg(&entry->ptr, &old_ptr, STACK_RING_BUSY_PTR)) goto next; /* Busy slot. */ + old_stack = entry->stack; + entry->size = cache->object_size; entry->pid = current->pid; entry->stack = stack; @@ -129,6 +132,9 @@ static void save_stack_info(struct kmem_cache *cache, void *object, entry->ptr = object; read_unlock_irqrestore(&stack_ring.lock, flags); + + if (old_stack) + stack_depot_put(old_stack); } void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags)