From patchwork Wed Sep 13 17:14:26 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: 13383614 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 A329EEE01F4 for ; Wed, 13 Sep 2023 17:14:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E3E756B0252; Wed, 13 Sep 2023 13:14:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DC5DA6B0256; Wed, 13 Sep 2023 13:14:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A69AE6B0253; Wed, 13 Sep 2023 13:14:52 -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 926E56B0252 for ; Wed, 13 Sep 2023 13:14:52 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 5ECF71A0DB9 for ; Wed, 13 Sep 2023 17:14:52 +0000 (UTC) X-FDA: 81232224024.14.D1F85E3 Received: from out-213.mta0.migadu.com (out-213.mta0.migadu.com [91.218.175.213]) by imf30.hostedemail.com (Postfix) with ESMTP id 918088002A for ; Wed, 13 Sep 2023 17:14:50 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=NRb1zL10; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf30.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.213 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=1694625290; 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=BPZVncfru3OabCJHja7OP4R3eLd9HFHgZkZvHal5XZrqeWSdxexfebrdeVYoEr1WBM1rJO bkd0tz5s6tdRwOYxacL34yWVfZK0ORyJ48ThV0Lb8DAhR4KHxHyMq/ykRg9SU3tzCX8jzA PPTjil0+QROPrZoiSzIZvRwbOGP3M8s= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=NRb1zL10; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf30.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.213 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694625290; a=rsa-sha256; cv=none; b=mqUYCZ0EwOMhqIptYxSTfV3JEtm7xiLMUpqcXn2utKzCZvrJ2NNynq4noxw8WmI94jkKLY G5AVIXRbVpF7p+QSPTRPGLFMdd4aF8bjMTX4X6kafewRtVVn8oCKPDnwVndKcRibJ7eiGG HIFBXpNsbnx58x8gQZmp5q8CuFfC29E= 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=1694625288; 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=NRb1zL107/rDRfFWZsp0fzET+7d1A+RLuXbBLhV7sSeKYAUQEbzOobo5W1rWEVCjdjE0Dd pknESjG2AuY5ulwmpFZM53Irkq/aSdHZnmh8PdDGcyQD9/nZGmn8edi4yz2Zs/OVi2gb63 BiRpglut78hvsz84TfRJUgNxqmxUdqU= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 01/19] lib/stackdepot: check disabled flag when fetching Date: Wed, 13 Sep 2023 19:14:26 +0200 Message-Id: <66bf1f0ad22d2c49ef500893340c71355b71d092.1694625260.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 918088002A X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: h6ssfo3wen8atg7wwj1o54nq4hhabkrr X-HE-Tag: 1694625290-544829 X-HE-Meta: U2FsdGVkX18eUERSBv7I7I1e2jMrEzLEANeA9HP4EF655WNh91Kq5idDOPZdgh45tG/qMvasKD9aOgR9VZk82/QnJAFduur57ZQw9f/Ux9EC+Fp7LzgORSuDMh/TNxvyNcF+Cc+Q0XH59Tmmv168gDEulCsge0+TKJNSmrhyQwSp/NpiPhgUaWTfPvDYspysj81E5CUnytSQnmWXaihNGhJd4Pwa0BYqk+Er9WXT6+8crM/VnFzjBw7rSl6SsVK7+QUHdqjVAc07IKSWq3P5XWcebRMuvRAWwbj5c9jRcstKchKyyfcxfbYVzumnIHG/N0AeGxu6OXTYad5WYaGQewtOFITMuH5TaUfbI92+YZuuYq0SVTktlbebViT3NOSVCTTfrmfrHJE1GogRDCRUNI7M3dOQWcF+spSnh13U2WncwUosi1bI2BctS+UYGDii9zWqB5H414sbCwPYNHLjRhUIaW5G7H11Q0q9B+q4vu1rhOAg7KFIVOTshXfAsU/LErlCnJhWGzcVXGVsWl/E6rV2SP1Bl3s9Got39Fsvxy0FDMJ//CzZypOgS8CMZiP4eX1ZsTmhO5mBIezKMUzClAm0T7OIhjMCGESCcG2Rva8JJuSnMAUx3NjHeIxOV+XQShVtPKyj8+pvgPhXmMMMQojTTijl+cZZgYtOZuDvQoV4d55OB9Tt2eO/0Ab6lDbQZAAdigqkWaPyK9nvWFZWC1Fj4INwRprBcm1GrMxIvm0gnUxT1QUIpgJWpap6GBPRHmlPRPsOIHgm61V545JXDjYI6ZQb/bMx5vB72vPSFBv++XCGeRvIlgqfr1WiRXM1eQTQRgDeHrZFA22A8qU9dZgfTPJ503j2sKGnebZemzaF8IxcyYlDOZ+ZjlA1ptx5ZkL8RmC0CBFrrftF55dIsGSG3uibYOFykgYeaT63oUnyN8G2CxV+O6cxeixlv41wqT3dSkVTVMZgTkz4+2L vQd9YQHU Q1uxys73w7Br0MyGQNb/yYVUBxkP2LVfRQo30xOTbt0UEu4JbvaZj0YXd62UUsqtv+FbV4eBF2d4s+yluO8GakMwlkQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov 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 Wed Sep 13 17:14:27 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: 13383615 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 96152EE01F1 for ; Wed, 13 Sep 2023 17:14:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 16FFD6B0253; Wed, 13 Sep 2023 13:14:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0F5856B0254; Wed, 13 Sep 2023 13:14:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D56136B0257; Wed, 13 Sep 2023 13:14:52 -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 C0BE56B0254 for ; Wed, 13 Sep 2023 13:14:52 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 97D581C9FEB for ; Wed, 13 Sep 2023 17:14:52 +0000 (UTC) X-FDA: 81232224024.17.CBCDDE7 Received: from out-220.mta0.migadu.com (out-220.mta0.migadu.com [91.218.175.220]) by imf12.hostedemail.com (Postfix) with ESMTP id D582A40016 for ; Wed, 13 Sep 2023 17:14:50 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=pRcWYiA4; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf12.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.220 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=1694625291; 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=0VCT8XP3Iu/BxAcKam+zE9PXNuwR8cMEp/77oBHfwAQXYkK/FrsxGmdJRR0WutiMGqkVYw HHHkhXkuTG3pHHzaXvbwqe1ChICs7MmKf1F6PQFGGsTJlH5np9RPa1yEKqTz6WpcSPXw8z fHzrwubvVk/qeNtdIRiN2kf39D35AX8= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=pRcWYiA4; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf12.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.220 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694625291; a=rsa-sha256; cv=none; b=uIay0L6c0jB7vaDmTVD/qUgu24aADhiA8+FuO68KcQo0YsUwlV8aYKrEO6+Tsi/3tb8HlF sPIkS0rSN2JsVyPq00oez0m/jTWsPYlNHtjhvCsnlgcKquQq0WIm/qBcQ0O1x3V0GKStbY wC0r8qXXUndH38Aw8r7C74vzFU5+PBE= 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=1694625289; 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=pRcWYiA4V9DcqOTdN2gLjPxezJ2SlGOHPh1Fz59jRDjJpcStfHaMZtV5oUBDvkHZp9pcN1 2sU42kzOkV4/EmNirmCx2z3pluIo9R1jBj4CZupNvF1dF6+hUeuhGaZZv4dXI+nj7Z61iS xPrZS4MeqQS5+CE7Icb238Gz0TxGYDs= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 02/19] lib/stackdepot: simplify __stack_depot_save Date: Wed, 13 Sep 2023 19:14:27 +0200 Message-Id: <3336cf19b8e53ed5449550a085cff9bddec4c873.1694625260.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: D582A40016 X-Stat-Signature: fsohfdxxao5k59qw9z3qihfjmiyt1s7d X-Rspam-User: X-HE-Tag: 1694625290-311960 X-HE-Meta: U2FsdGVkX19gi4NO6UxEZKOs2do7RdhNenaqzbd7tmKNBRYagAfCTQX6yBuMyla8/wgw7bPn2X9GbM1D0pZViv2htn/vfGKTiSRQBsneiWmIefiL7nFmefzrgS8/htYIvwjLrC6pPq8IMnj+GSQCEye10Few1ip2tCAbEz5U7UWUsoRqarIzcprVTZ/ZDnInDXLVBY+D4KMt1jQlM3+/ajlPBWq2k07yFVPWoFdz0UGvNNC1Jxmf0tdusTb+KI516tJ+RCZjMRJSutNQ+6s/n1cCh/4Fy6bSDwbpgR/I/WRJ9SNe51BtV7qAG4IBfKl5Lshl3UWgnfT7HnKzn6FlLgq7pOqFBEr8a5yFD9AtbOBOU8znUuhnaZZfksszgC51WlX+8FWM5Lq3n1lASlJJ0hkXeYZ7iRp7EP5zYuDMf5/x9BYncJAdDX/aOonQoTqYdJD+7Iq+oSUKAHWaw1VKfBT7ay2+WefINdobeT7ov2qyuMoVtZC+ipVI/ThH3U8NshTeWIW0nbwqi30Cniu1O7bcU6zAHhB7l8pYJEze98avHUaIXp3e9i7Fbt323Sfp1fxwT+3WobbO0mkff3faynB00zoM0Yb9FsvitfGj3loOmv2RUeeM46+YD1G1LYgohTdH85jzA2c8W7dshyqrrkIvwnDSwEcCYHMyM8pjX+us9GfyjJ5EEmcimAiDeRH/KG11I/HRYRGmRTEfVf1zs6iEOh/20zlk/0mH5JnFe9jELa/wdvANFBx8zteUXJQxN3IzbBhVDaJO0I6qzFvkcQqDfZGrLb0PW6C8voBdTf6f/PSwNmCvOBtyBSCu5addIFp9vKVo5lGqaj9bm0cEwP3sREpSAHCy8rw6XwZ+cOAABjJgJPzDxNcOS15I0BjQ35Ob/FMrAiZpvkArhehwpIx/MV9nl+ei7E7IieeDmMIX/nzEm949mnxorNU+N2K+VJUC7Mmj2Klf1wX4gUx eifDUfPp BoK2KsOs6u9aBvR/fEr/L7Wf7UZCpa2BMjhVihNtVCSMp2nV6NIMftQBuwinkag40KONLSQ3udYGVJk9iqtEkWFokm83mLGti/QTV9s0hW4ZTPWLS6JycEVghP/j4aWQfi+v2gtI1FAVSShaCgA6T4J49kQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov 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 Wed Sep 13 17:14:28 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: 13383616 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 70FC6EE01F0 for ; Wed, 13 Sep 2023 17:14:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 980C26B0254; Wed, 13 Sep 2023 13:14:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 8BBAA6B0255; Wed, 13 Sep 2023 13:14:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 75C9A6B0256; Wed, 13 Sep 2023 13:14:53 -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 5E6636B0254 for ; Wed, 13 Sep 2023 13:14:53 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 3769C1403C3 for ; Wed, 13 Sep 2023 17:14:53 +0000 (UTC) X-FDA: 81232224066.10.112F3C9 Received: from out-220.mta0.migadu.com (out-220.mta0.migadu.com [91.218.175.220]) by imf15.hostedemail.com (Postfix) with ESMTP id 800EFA0006 for ; Wed, 13 Sep 2023 17:14:51 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=XFAMKTE8; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf15.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.220 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=1694625291; 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=s8Tp8vJoo/zwyyWYelaTOKPFLXqc6d4VCcSzzDh+BzGbp6GdUqj/E1i18Kgmwi/pqRoWGa 9bTc7KbhnwpddHVpLrMQkJKhmRxU+i+ECePiSBG0Kp/DNKgC50ZBO0OPmy/TCxgRY34/ew k6dYqqbhWHa+KcGdxyz26FYMdXu5/t8= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=XFAMKTE8; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf15.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.220 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694625291; a=rsa-sha256; cv=none; b=p8VW/oNpQ0rtwoAiZW3YxleYE/rr+GjM4WSxBF8GRaGS6m9b0PLQ9FnZjh4963pol7Giu4 lJjzhaXnxJiXoNq8mzgwBmu47qyDN1BPFAimY0QIw+KnkshOO4+nTJyKNEPDi3VtiIk5FJ w84Fd8cnsBQJgLpqFwYc59r4I2x/U3c= 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=1694625290; 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=XFAMKTE8wAgngJGqU3Ho9bfesOIML66PlRFcmLlbqUwEjvPJBgA62jyh52QZkOwLjDPuzU AyQx3wfOttMTFmpwW6FPSwPLuzweuqHN5H2L0Pdvtlpy0XaMKleBiTQWXdXoBao5TkmHyv tHA54Ka/Rk4e/x6SqQ17ainnSJdFZLQ= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 03/19] lib/stackdepot: drop valid bit from handles Date: Wed, 13 Sep 2023 19:14:28 +0200 Message-Id: <5cbb8235fe6418f970fd0012450defdce598abcf.1694625260.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 800EFA0006 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 9r94j96xhy6gs4mtsn6qc4b5gj55sn4b X-HE-Tag: 1694625291-235935 X-HE-Meta: U2FsdGVkX1/9/6BRvZwY3F2o1LEjakOfVI88Y6t7gl8p7DIK3LSNxi6SPyzQTjt43feR59L+IC9Z4TH37fRse5r4ojFAdMNLgz3srE57ZPUQYPLJdsKMT/y9IOwl3HmIFwib1TIXBHqYA8MHKKJ143Y8KuRGqwRL4nuNL9HROOgxcHtqLEHlD9NDvY8TkVvDjdOs6XMnBWlY6HRt+uXZ8ZIcKZk4dEPaGfv85K5b8TfTUeBF6qqao7tckCFyt7thIerKUUz5chwcP8eL/Gn7mMemqPL6C+5x3XbivajmfCoaJPnNs8/0z0AkEaPB7ez4crjZvwy6NooCdywpoMX9q+TqZSnQcdsGRJ42fw0L8HE4HhrtGvgeNEBJr+Doy3YX7Kp1vGtVrBegrw8B76ib2IFHAJcX2rOwIGnstY1ZViaGqVol2bUqUbbTvwgQbyv7C0Idca7hRAclwYRBRHfDCowiELppvmSUdBktDN8iZ1kgRqSYDQmhVtCrQ+1+WvBwXCmQ835FdXg5H+csQfYtMV5gJYpmTzc5JkFpwcrmG8wNE+kY1UDBanZsKL6GdxXBI8C7P8SbJxUI8PKctTnjoFZTRYg9hjmzgYxkoe5ihq09YM8S2+sQgtE0JBSlEFC4uidDKcRV/ycIZxRkA/roj+bOfLzki/qGLaXquksHTAh54BGmH0GvRIbZjQNRe0qEclupOgmZT3T/wBr18ZnIMX7PeKQ2Z5cvkZF6HrBnJ3XoD/agK+UmoYbUpF+Caft6cH9ouz5L9UNkX6224hCdNKz9XHcAKdhsOf2zDa/4NiZbMKhs/yd+QkvPACQPH0Ciw9a8JD7cVtIkP+hqFMJ0TxLybLbc+kwh5uWKvFNZ1jmGAjEfRZ3D8QcKR++YnxjzsMs/ptxzF6VG3zwx1kJLoJqUKe0AZvPfkCVdpx/L5JztEstigfK5mIAGjhfHhLmTemWvJadMpmTjFiwzAeh g5TBF5DU gIYuDJL+3YYD11EuvN4hhC8L0ypu405i0wyBNBKfNfsWziS3mIPGlH0kY+wK+dVpOz/FQ2/YwnRzXDyr09ycQtq+ODkJIbFYwV2rn7/izp1x4jYI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov 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 Wed Sep 13 17:14:29 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: 13383617 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 B39BEEE01F5 for ; Wed, 13 Sep 2023 17:15:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 625846B0255; Wed, 13 Sep 2023 13:14:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 587C26B0256; Wed, 13 Sep 2023 13:14:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 38DCC6B0257; Wed, 13 Sep 2023 13:14:54 -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 1AF6B6B0255 for ; Wed, 13 Sep 2023 13:14:54 -0400 (EDT) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id ECCF980E87 for ; Wed, 13 Sep 2023 17:14:53 +0000 (UTC) X-FDA: 81232224066.30.906EADE Received: from out-218.mta0.migadu.com (out-218.mta0.migadu.com [91.218.175.218]) by imf03.hostedemail.com (Postfix) with ESMTP id 2B32720008 for ; Wed, 13 Sep 2023 17:14:51 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ctPU1P8P; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf03.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.218 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=1694625292; 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=58HfvPPo4vigRypI+3Cubzsf+vk4Sn5BO94rciUGR1o8Slyy1P6SCLp03AyZuV79RoIEDk M454xJaIa+8WKbPE6WtcoKd2T8+Yl2fh90MCQtXvtIEDDo/5U8467OB8tNU7ICL/VZMiSH zVHSITKlrvWhXA2YL72zvHXoWW7myMg= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ctPU1P8P; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf03.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.218 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694625292; a=rsa-sha256; cv=none; b=kjD9aTm83jwmnRtfKXirQicBmtBJ8NlNiEl8F6CpEiKJaq82BXEGx38sP7k3y0wQPhWjaU lZGNrI2hF7eNeSeR7b5GZfHEctuEZ6ifFrAaQaqjFNB1oJu5liRoDLEuv4f8vFL/+4l8xj Cvj8KUnbOJ4UkZvqrzS82xWWFwwrhq4= 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=1694625290; 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=ctPU1P8PjzANEZxxufrmMlhavpf6MSmIQazNfrOE3GkL3F/j/nvFZZSEB8ovVp30c7s6MY CBKIkfoanapClJxqvm57Tjw1Y2V8mO/SFzga+leuMilEeLfLnIn8oak/3JOrpDVDpm0SEr N49MYNSPajkSI5g7JXyddu5oLOfkpEI= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 04/19] lib/stackdepot: add depot_fetch_stack helper Date: Wed, 13 Sep 2023 19:14:29 +0200 Message-Id: <74e0a28a38b05f27f3a4b54d5fa93213672fcd30.1694625260.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 2B32720008 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: x4o3guwubsneunfco44fkb4pxscu64aq X-HE-Tag: 1694625291-8870 X-HE-Meta: U2FsdGVkX18mi78+SmX/fISowkJ3pInqwUGcPPzp6tOx0Pt59dAe9OxioguvOF4yvyrcTx0F18ulsFVyQrQo1H9OO16aS3SzTWdKIVQZM/Le0ee+BScTJYVaNHM+aL5I/sNO1vPL4/0x+B0IRjLF7+RMn0RNdxz33h2nylWAlH67ofF6aZWHctitq1SB59ZoB1WXX+fSoeTj4cjJvvY7RP3R9UQp9Qn2Lce/9928XPnFms2H/vHfLXxmD4lcYM/UYhdv3jNK6mH4jkPN741tCkewzUdWbE7oha1d0i65MNJNmbOqv7zgNTUTj9y5fVoS/W/RzZ7a7w8Le8ksXpGdHZaC9iBJI4oTujB4szNsk/GmxOUYpr/zhD4mGutfdpb0Qq7WiTqZWSVVBg+TpqAx7a6SfQDeeXeTw5uL/1cMHdd6QRSkXEE+t4V9BCzZhVAnS/x8QmlDlfb4onPjZvCJMqou8CqMhIpcsCXBbeu3vWWaE8iCFG8NmMeK3zjYTdOrSIzhxKrZBjaTfW0vlCYS/i6EoE3WDZYAfGWls/qnTvahkeTKnNlqWFwEK1Mn8HBdeWKcm1xkpqToqj1VNsmFZ/otKW4pXArzS7qZ9XWRJUGZnCIgQsrx7Iqd7e3901yWflvDWyEj/fKurvgoq7dfJqpEwL5KqyF7/wO23LJCxw/pPRRUmaiQ3BCrIIkXmrJrgQ/LV5MNgNtZsY9dqUXj3cKLKaWTVLzf0BS3Z6b9jRxg3deUc0ATTpBGYdA3109PWN8xEv0qEmx9DCKhtvPUfTVFHXjCjK5Qx8BIJ6FzGafuffVN5QMxMwxlw+JYjsX6I70dY21bCFW9RGFPMfCz/l4rGb4NYAAoVtp+fWOMSr9GV71V51Kw1psi8irBVgZ7UJrJjiiD3PO3bUrqpuXX+7HjfIfFJQIx+YTqtYRQjpV9tAuf/pivG3mLVJzpy6na9ptLzA7OOhLOKDwjDTY lC9oZPf3 BUbZdSqxECepIT//Xi0mYIxGTOlhSSNpnS6ETmuAGeVVcEf4+DOJwBycxlz/Y7kXYgyFniFrBjTn6gbUOmgtccexUuzJODxyvzOIGgo9Uxni/s1d8ReSDvWKMnjliVT6xjQF2dfgXXyWjLJzdSXTDo9b+KA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov Add 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 Wed Sep 13 17:14:30 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: 13383618 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 E702EEE01F1 for ; Wed, 13 Sep 2023 17:15:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0F7B16B0256; Wed, 13 Sep 2023 13:14:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 00B106B0257; Wed, 13 Sep 2023 13:14:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D02356B0258; Wed, 13 Sep 2023 13:14:54 -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 B30126B0256 for ; Wed, 13 Sep 2023 13:14:54 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 837681A0ECF for ; Wed, 13 Sep 2023 17:14:54 +0000 (UTC) X-FDA: 81232224108.25.7CA4140 Received: from out-213.mta0.migadu.com (out-213.mta0.migadu.com [91.218.175.213]) by imf24.hostedemail.com (Postfix) with ESMTP id A4371180012 for ; Wed, 13 Sep 2023 17:14:52 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=S9us7jlk; spf=pass (imf24.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.213 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=1694625292; 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=gml8DcgHPrqnYJfUQnrXQLUWDOAsSsS+HJ/vnfgqmKg=; b=DPgYPLERUOc/FB4QcYtZMqCxtpIAiwozE/00p1Rk5ykuXLD+smwzZWXHVfQvb5sVhF5syd 331SEHRlH0OIczIuln3cGsFom/bsEiPq8MGXTsqW53hM/G4a+MRnhSu199dQZd1e3wDkYI xD2PsaWynrm2YaO7jNQv7X9UrsSAyfw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694625292; a=rsa-sha256; cv=none; b=JJjfSBrYKXXbujilwDdMz0rka+uKoeIeS5mmgBfBX63++Mc3+UN8fWZaY6R6ul/Wif9KOS cJGfVmsyur8+b6kRsLclFL8wBaDbRcw26+/wEQvSURxLeznS/kzdZ8KHnvKOs8ZNEBfvHx mdd6YFBiiNOVkZYoX54pgmtlO3OE4NY= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=S9us7jlk; spf=pass (imf24.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.213 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=1694625291; 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=gml8DcgHPrqnYJfUQnrXQLUWDOAsSsS+HJ/vnfgqmKg=; b=S9us7jlkKOCVp2Io5I0Tx7rlmHRdUo7dp4fSfiusTxeMqFjArjUdWXOxPxa7TRsS+UTSGt szqyWIxKS4Bt5+b4uJDHImZmEPS4RWHjhTLuJQarfEMznDy8FYztx69TSQNHWeON9BhC+T q2mjKPkhq8D4BrojemcgDiouTfk4yj8= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 05/19] lib/stackdepot: use fixed-sized slots for stack records Date: Wed, 13 Sep 2023 19:14:30 +0200 Message-Id: <658f5f34d4f94721844ad8ba41452d54b4f8ace5.1694625260.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: A4371180012 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: s8g97wrdckzggou9z7zrk3mzcp3xiia5 X-HE-Tag: 1694625292-653536 X-HE-Meta: U2FsdGVkX1+kUNejxeJkoax9/E3AJA7FbJ2TQ+ewBrFSS3tmZKfFNyCmnTONPJc83O2OZTk0849FJyATckoNCPwD30flSLscl0dfFpEoOITakbT5l1CHN+4lJSeQjtJi3GF3E36dZO7M7eo/dvBIwKQLsASfcYyDotTbk74/0S1ZiNMDoXkXShvKDykj05XU6D5AX31Cl1Xs9em2GjM99Exo3s3Tf8G0leOKg6w9ndG60FKOitloYuV8zzgORmZj5wizu5QNDfCStqlD/TM81WS4qhIKcYveBhdRtimU8o9aEYRTdY79avFCvpR+V2bAynu/S//tpUfF/Lruf4VkJtO1uQO1pxkP4rr3BW/jxD2FxX5txenTa4wEo0qENfGmhs29YBXzCF6V6WRn5J3DvbHXT8XoGT41p5HPhrDd5KANDSACadQgPgMAvf6PlZaI9MBCXi0O6pH4fZpDtKfgvFqGcM1/D55SJXfvR/kBcx7B293qpxfTsexWk1jkQpsa7AgqEI2CjTMez0x9oosJN1mT2Xzlx8IGFqeKnAVQuI9/TWkUaHWWrixN29yinZqKu1qqXCi0dH3kQy+fKfEHKSeKwui7RXtxz3kPg0zYJJREdQU7YR3t3eqG3g+sP+O4or9nxXUO+p3OdafTVBUMCew9k8be3Tws9Go/VIlYIgZoJNcJnoUXTodcdc+E9Z8SfCAyudo2/yN9uVutmcgK+bGmls+dOunEXXUjbdmNMg1dZrSxeiwB97hbwzKABF0ZNainUxOPS4w+yeUzAC0syJ3z3rYsErDBhktnfDgZ8uxNWbfiEbft4qdCWAua8rdCsPS6w8pOAXEpY47pOxkFIGHSO8UnbijxaaoxuxmJpD5RVXBuNv1uxj9Qn8e3WAhhtd5VwpZ+apCeLtPthkITsRdDGJzo9HIJ4XmZFAZ0/hVNok/OIvTbEWjhZ4sydp6/wxIuv6mDoGI1EBH4gv7 YBUtRuuY TaT7ie69x+hQqB3GalAP/Cc++PdTO9FlssROB5LoyCGWeV0iOcsxH8DLuKghFR3xmwcJ6VKQ1yPnXGKMNiCp61kOshBAYqbBnyRsB X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov 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 v1->v2: - Add and use STACKDEPOT_MAX_FRAMES Kconfig option. --- lib/Kconfig | 10 ++++++++-- lib/stackdepot.c | 13 +++++++++---- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/Kconfig b/lib/Kconfig index c686f4adc124..7c32f424a6f3 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -708,13 +708,19 @@ config ARCH_STACKWALK bool config STACKDEPOT - bool + bool "Stack depot: stack trace storage that avoids duplication" select STACKTRACE config STACKDEPOT_ALWAYS_INIT - bool + bool "Always initialize stack depot during early boot" select STACKDEPOT +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 depends on STACKTRACE_SUPPORT 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 Wed Sep 13 17:14:31 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: 13383620 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 0D0AEEE01F5 for ; Wed, 13 Sep 2023 17:16:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E82026B025E; Wed, 13 Sep 2023 13:15:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D9F306B0269; Wed, 13 Sep 2023 13:15:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AB9646B0260; Wed, 13 Sep 2023 13:15:58 -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 8D9306B025F for ; Wed, 13 Sep 2023 13:15:58 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 4B515C0F01 for ; Wed, 13 Sep 2023 17:15:58 +0000 (UTC) X-FDA: 81232226796.24.173031B Received: from out-230.mta1.migadu.com (out-230.mta1.migadu.com [95.215.58.230]) by imf12.hostedemail.com (Postfix) with ESMTP id 4A61640024 for ; Wed, 13 Sep 2023 17:15:56 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=TYMgdFjb; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf12.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.230 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694625356; a=rsa-sha256; cv=none; b=xvQRJ9zRUqeJhY80oMDi8KzLElv5lTh+ajD7mr4enH54mC5ch6MR3YElniv6zk4BXo0hcW /ig9PoG8jDqELOCgEgorthtMdoJGtDxhsMo6DZVhQ0YlenXJKasPXB6Eou5VzsTYKlu5bh pBI41+t0H+E/R1xdgraI3gzU6tcDH8s= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=TYMgdFjb; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf12.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.230 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=1694625356; 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=0rCvfJyQVclVcl+nE6KRDaf9jMIyS6A7Wyxgy4VzHmE=; b=5A/FVTlc/u7KmRA3JPsNb9lokdb17vdVFe/EG5QVBo27s3+ziD+be08rfzi+/B4Z2BZo05 QcxSvQjqd3W8dz/XkjgIYF37IPJOI7HvQPdsuxHUY6GJKxbAqkxEk4uLutHTNIuIno8Z5o Jm2C2UR6yEq750PXcFqDKnCMIItUDI8= 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=1694625354; 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=0rCvfJyQVclVcl+nE6KRDaf9jMIyS6A7Wyxgy4VzHmE=; b=TYMgdFjbt/Eo9/c1f+k7VBMopiQ5ELQvuAFxZx2mG4w5fYla84GACRt9mHYjc8VPC6hY1n PzTFY9MQ4IbYJVEAwdIh5X1A1wBrieX7KoSLaA+ZMLXosAUWrOS734vnP0yfp7RNTCW3/Q RaHcPG2lCnUbh38nQx0ZOsudkh1SFKg= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 06/19] lib/stackdepot: fix and clean-up atomic annotations Date: Wed, 13 Sep 2023 19:14:31 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 4A61640024 X-Stat-Signature: osab1uxtqcig63cjz8p9i1183z6gpg5p X-HE-Tag: 1694625356-18898 X-HE-Meta: U2FsdGVkX18oubyPPyd+GPbjPI6tr1KpXHSrIZaHObBJwkTh2naZWSnSuUQHfMGKWfNfx3TXTtOgSOaEj+PUClnRE+X0g15vffX4SqudrOJFtH2qB1BVJA7BpXOkPNK6n2zzuKkc25b5AZPdlOfNk2b87KMNfrmSLDOOBOYbfGiRAXkaIscFMH+CI4/4o07CqQVOSJUgi3Cz5bZt+EkJ53zSuPtJ/oFBJpBz+8UJK7i9VAgUDwNVUC4XtLaJTW1ETLaeLw+1xRfm/b6X+8V/JBamti5wXme9Aos0WMZJCi+OTcqA4iuJ8PXPjfjTJafDlXv1v4rQ8jAYTBLqJ+q9xAxwRdNmDb9A5+ttPiICqHmZ/C227dFeni7biUepynMpaSkNQRVvlUKXcnTkWhS8wZS75GY4ZhVQOphyzRTNy8pOA8SRcF+4ak+E2tGk/68AFXO8jxSHRuJvm2HwKY4k/k9s0TXqxVJODrIjTEwfdi49YfiANyWs3QGrFQvKhd5e6dSFy+GRbfxtb26ZA+ZiYVNq44X8djjWVKh76mzVPs9QmVa1SPbYztVY5+awiUXjD6eRIPnQcWalGMs4ztHWzsOYPeBWHqKTvZc0OvbjdKzG0u/qYNMT8G/0gnwG6A15pDm8FTvZHuIuwfcx5Mlr/x1mJQhGUrBiMsA/LNnLVKMQg8n0ru/8f+9gCeOostskadynsga4nCo2kwhnQVRZ7LsIRURwyHlNiBK8PgSxzBKiyTMzOXf1xMNnJgm+RB3+5FvSAJ89JNLOM3FCnxPSkpEkwt7gyaFN48v8NGM4+D56qx5uNh7FeBUnosbzkjrDoL4WkE3K4hDg6kNgWu5iC0k2sMjy3PNE+1fBUCi+5Rfwtk3YrJG+vnn+O24hmGSksGfm6QwTffnbd1FVh7OLp5MbPxhSOmMe7IgGyQpv+/XrjDKxYV2rW0QtN/zbElb8Z8ebukvPDxF46d7qNVT ub8SDwVI YguAkklBac2ByG+16smFT3z//tn/IJck6xF3JDkaLi44vD2Z5XQ6weJB37/vEs0sixR6Xahj0dHKc0H/rlR1+QlG6EPDbd8soKUhM X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov Simplify comments accompanying the use of atomic accesses in the stack depot code. Also 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. Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- 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 v1->v2: - Minor comment fix as suggested by Marco. - Drop READ_ONCE marking for next_pool_required. --- lib/stackdepot.c | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 128ece21afe9..babd453261f0 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -225,10 +225,8 @@ 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(). */ - if (!smp_load_acquire(&next_pool_required)) + if (!next_pool_required) return; /* Check if the current pool is not yet allocated. */ @@ -249,8 +247,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); } @@ -274,15 +272,15 @@ 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 - * stack_depot_fetch(). + * stack_depot_fetch. */ 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. - * 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); @@ -324,7 +322,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_alloc_stack. */ int pool_index_cached = READ_ONCE(pool_index); void *pool; @@ -413,8 +411,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 +421,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 +448,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 Wed Sep 13 17:14: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: 13383619 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 3C2D5EE01F4 for ; Wed, 13 Sep 2023 17:15:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BCACF6B025F; Wed, 13 Sep 2023 13:15:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B2ED66B025E; Wed, 13 Sep 2023 13:15:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9A9B66B0261; Wed, 13 Sep 2023 13:15:58 -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 8477A6B025E for ; Wed, 13 Sep 2023 13:15:58 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 5B09680454 for ; Wed, 13 Sep 2023 17:15:58 +0000 (UTC) X-FDA: 81232226796.20.A748FC2 Received: from out-217.mta1.migadu.com (out-217.mta1.migadu.com [95.215.58.217]) by imf08.hostedemail.com (Postfix) with ESMTP id 9A82F160009 for ; Wed, 13 Sep 2023 17:15:56 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=puVK18ov; spf=pass (imf08.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.217 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=1694625356; 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=gAOd+0bdipGa5TwT77P0cix2trL96KYDpIy1afA6aX4=; b=q/fC8e2bwPBSexeYBD0OCAHIAqY1+SKFIRYLnR5BjAbj/Z/84sSwNYqmcB1cgYuQB2Lv0Y tNisUnKUVS+U6svSikXeIJ80tMqCIsJs8hPJUdTfJJ74/lxqrlw4tETpfS0n4+G/kWymNQ kBXEyxnXin7Zde+WfAlYqpldUzT52cM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694625356; a=rsa-sha256; cv=none; b=BheBmIaw1nzptAnZODXEAgue3iHWOw9tVJxE00XQqjgj2njnF9TJ/dKTlj8KY2rdNStdPT yzOxLb/MYxVrdRsTseOOahtCL6NL7toMpZrfoGptJqfo/p1e47WuoGr5cRQpZF9ybLo3DF te2FbyfQjipniM6x3QbOnXaQRWGJiVI= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=puVK18ov; spf=pass (imf08.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.217 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=1694625355; 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=gAOd+0bdipGa5TwT77P0cix2trL96KYDpIy1afA6aX4=; b=puVK18ovwq5iY1p+xtahLNnbqGMtfLRPDwUGhyhUQSv+gXInLzJ556ocSpipYuAJUi0IMk O/5IT0uIadYTAipofo7r3zp1kwfW+8ahs4/NkRwA7zFRFCmipzk5gZ6+1AqN6CDXnhRBxH zjagfztze/wMDtvIwTVJol/qj+Z7myE= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 07/19] lib/stackdepot: rework helpers for depot_alloc_stack Date: Wed, 13 Sep 2023 19:14:32 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 9A82F160009 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 7os83yx1qbzr43g9fjw1bqttwwxe5iit X-HE-Tag: 1694625356-712844 X-HE-Meta: U2FsdGVkX18Qcd4cYEwtNd1ATdRUNyI6d3NTJ8nVhZBPqRB4DE6tjV4Rul9KUZMnr568j9WQx2peoI97d96gOe6pvB1/rRU4X5i7Xd11+BcQ8z6rleke8ZWqKeZlSn9+52iC9YobkBQgzFksJJn8MTOwYYZ7o1mhuyalxXCttyWzmexdrNvHD2s0MtIYrRLWqBbNSJ3Oogl9QrYJ7VGhByMIlrAxJ0ngjuQmqP7OUQsKQQjRuesJACbhKxZntdsI/qJSI7v6CNP7uxoZDuyUW6iUTwHoHb1BfEb5Ty84lWJ9whz09He/jg48B8x2/0ERCB2hv9ZIc0ZAoRwoNlC9BOHSv3gdHONlsKoYvmlSqmiMeUu026TRDtgN1masl9oHGucQP3rGXZcFbkwJUicAulCHeOqjeG/E6naXCuw2lfqWwp1a9ayXm7w8m1CJYdAmYcl2/mlMccwVCWpgRgKjmhTRXR7hPCfZjEnlV91VcLzQFCveRDyn0SZKKT79tRXfWd27bq0Jn5kssCswW2rT1U6HYtv81MlrsjjefsIFqn4Qldqigf0hLlPZ+/blDmAmVQTySfzIAmSTLhNH1tQEimXkv1IGCbFvAHSNUwGLcEIGr6qA15ENlJ6XoD9hDjjqO3QotLV+qonuq81fkpaTxvUon9Lbsf47IERtyzftRmRsefomADNx+zMCNf85vPNW9AeXJcnmnoPW9KBSxNB1sJIAzRqvCp8jB0kVjsrKPem+Ynyd5HQ5i/VfaKT+E9ZPiHd67sSHKl2Mepyu5N4yb2G96xyt1bEEKnJU7TwSYxn7HlAv2zDVMmzGsv7DanaNeLMb17CELDCkeUXVT+q4NchCyM/Njw5Mk6thDipEFNqrW4ATFqbWmkMK+cAeWLc+4zd19YHbCpdJKvKpKo3f42o+5yPFNaoCsVAVCHKTjvTWmYlomef3txex25ZYuMPH6VAi1gRccVviibGBeHg JDIsJpBE y2mdsDVWYEg0h+G0cyF3JcTtro/Yw0m/VX504KrN56cogLZrsWwaeAnBZTPbmDZ/ru+8wlM37s3QBPZCq+WPYKTzKhaM3dDL+uTdu8Tu91VuH8CajLjlYbpWN8Zx0NfcEx53Z5U0kHIyofvdA+2VI+MxLsg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov 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. Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- lib/stackdepot.c | 87 +++++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 38 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index babd453261f0..e85b658be050 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -219,54 +219,43 @@ 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. */ 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; } /* @@ -276,9 +265,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. */ @@ -286,9 +276,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) @@ -322,7 +333,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; @@ -421,8 +432,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. * - * smp_load_acquire pairs with smp_store_release in depot_alloc_stack - * and depot_init_pool. + * smp_load_acquire pairs with smp_store_release in depot_update_pools + * and depot_keep_next_pool. */ if (unlikely(can_alloc && smp_load_acquire(&next_pool_required))) { /* @@ -459,7 +470,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 Wed Sep 13 17:14: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: 13383621 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 CBDFDEE01F1 for ; Wed, 13 Sep 2023 17:16:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3DE596B0260; Wed, 13 Sep 2023 13:15:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 318516B0266; Wed, 13 Sep 2023 13:15:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0322D6B0261; Wed, 13 Sep 2023 13:15:58 -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 D3F166B0266 for ; Wed, 13 Sep 2023 13:15:58 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id B1950140DCD for ; Wed, 13 Sep 2023 17:15:58 +0000 (UTC) X-FDA: 81232226796.18.BD116E7 Received: from out-229.mta1.migadu.com (out-229.mta1.migadu.com [95.215.58.229]) by imf25.hostedemail.com (Postfix) with ESMTP id ECEAEA0026 for ; Wed, 13 Sep 2023 17:15:56 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=n8pGZUmY; spf=pass (imf25.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.229 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=1694625357; 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=0/KQC+g9eK7MZLi0iRkQvuX1rPqJBRg75npInF3Ouo8=; b=svZiVMfU0lwE7Mgc7evrrIAuFmsZ3Ovv7rXwGVR3uhaoaoDelAW5SYADgmFImzCokNbJS3 y+V1kDVyOoVcK+h3EdS07aaQiJdwuCUcaRZaHl5ER+s6oGMgLsjEF3YfGLM5BLDGbs+SQ+ z/IC395fHSfn3BheaxeSl9KFtiHpAfU= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=n8pGZUmY; spf=pass (imf25.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.229 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=1694625357; a=rsa-sha256; cv=none; b=HB3V1/5pbEVC6jBkP5gviUTwMwwgVxrj0TZFAs26dUI/WABhPld76DdPEg+twrBS8a56Bj Zu6JBrT/7BKSNIy4QE0BuS12MEsq60BR3Pcy34Fzc7YXhA1PB6hWsGv3b6lvVnh1hVcsdY 0kcMuF36Nc22hGvk8eVt0WpBcpJVKf4= 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=1694625355; 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=0/KQC+g9eK7MZLi0iRkQvuX1rPqJBRg75npInF3Ouo8=; b=n8pGZUmYCVqQ7/ite2vF9JMhwV+glcABYHAJ/a/1kWV1LzwnX+pz3GngJd9Jqqc1/Pv61I Tc5+8k/YmnNs3tTelE4rd84gD248pTV9VF628E9nZrnCTuq8FjxCXOUUKGof/AhL0LIypC 1oXVFUG4VWiBhHDjNZdJF0SfAvv8pl4= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 08/19] lib/stackdepot: rename next_pool_required to new_pool_required Date: Wed, 13 Sep 2023 19:14:33 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: ECEAEA0026 X-Rspam-User: X-Stat-Signature: u7xqzz7p3aoo1fmibk5whhcbudapy1yd X-Rspamd-Server: rspam01 X-HE-Tag: 1694625356-701294 X-HE-Meta: U2FsdGVkX1/wTQhavG0Q6sQdvLrIJu7a1Um2i3hungvwl1+UoJnKoCyYTfeM8zJxK5/V7F/B1LsFlO6rjH0Qya4blBc9dV0mT28qGyzItu3y223ZGzGNRefiAlhgr7HfaH0HX7pAMz/+e8iCcqexE+vnLBBpfa7qygaLzNu38Tu4onbu8ecrNvJcPCfkzEqu/CMZIbKNyEaRkE27j1d7GMLqziGSYPacFD/et91A89HK54YmJn7doobkLkJvFA/zEDHrbWHfu0bWJOChXcoa4tj1r5f5sdeWtqcC3YNBIoO9IwybHiWE8vNjNr2/9VS/oo4LijQ9xPB+llIvi+SsgbUSuTmoDfjfrZw/VJ4pzEzdXncvKPFlWrY6hQTfuP+AdYJ22Qc69B4adaALeP3CulI+4rJfSaIpqfpaI6Nixpz+cLtKuot9z59oOZj42KqPd8enyCKGZJr+Am5IEjuOMKG7Igj68HdOJrblIJs+lROcqnJ4ijCahuystnj5haZIAOzm1e8gVYyoNnURL7Uoghne2c8OnmIutZuOBUfuHYSlWz/0Wi/Xkgo2ukWvSMViM4DS6t0pJJFOJfe2e7pZiBq9fv5YcAe5OX1JrUNES2WMwZiEGoxVLs2hjfalhFDbq8JJlmGlDJiHR23gBqQY/GhjAH/2IqiVJvuwX0q7tDfQsvuIaol0pP2Jt3hD4CoV3Hfo1ojeP5iHJJwoaCHLM4273Tbt7uakxTvFvr3wp7ahtLTYIggUzjslL8OO/GL+Z/9ekN3pMnT2zLjFFa3b5xre7TgUSty2YaeQyIHH7CuvwSx967Pu9FTuKU7j7xWFG+SFO9L2uSCvSH7CKTHCRKRUERLvEACGYcE5SO/dfupvRzNUV1mWY/P5nnKb6UafZZzHvxJDVDuOPg5PZjDZjAKu274vXyoOqoVXcaqpTsdQk2v2NWG5TgQZO149DAaTlMqcHPI1r0GVYIKfWDi OcrFB3/M 2QV4mE2RmQilFIveVqGbhJntZ3bVsySwmvnDdYtu/uRRttnfLNxdwozVam3ir79vEFU0zMl38IhzjYbsK6uKpLvpaV5W8xa2zq3BfYkf4uEKZ1MAee7/9Ve++UVdNpXSy3zvGukBJUbpVzhk11bH483m32w== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov 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 | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index e85b658be050..e428f470faf6 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,18 +218,18 @@ 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. */ if (!next_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) { @@ -239,12 +238,12 @@ 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. */ @@ -259,7 +258,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. */ @@ -268,12 +267,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. */ @@ -284,9 +283,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; } @@ -297,7 +296,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; @@ -429,13 +428,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. + * 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 @@ -468,9 +467,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 Wed Sep 13 17:14: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: 13383622 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 01523EE01F7 for ; Wed, 13 Sep 2023 17:16:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EB27D6B0261; Wed, 13 Sep 2023 13:15:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E61B26B0266; Wed, 13 Sep 2023 13:15:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D02F06B0269; Wed, 13 Sep 2023 13:15:59 -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 AC9D56B0261 for ; Wed, 13 Sep 2023 13:15:59 -0400 (EDT) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7A600A02D4 for ; Wed, 13 Sep 2023 17:15:59 +0000 (UTC) X-FDA: 81232226838.05.19D8859 Received: from out-219.mta1.migadu.com (out-219.mta1.migadu.com [95.215.58.219]) by imf16.hostedemail.com (Postfix) with ESMTP id A4B8D180030 for ; Wed, 13 Sep 2023 17:15:57 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=wHA2oEp9; spf=pass (imf16.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.219 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=1694625357; 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=iF8d5PIu0hiypd5p1X1vzHkKz0HhegtzJ4nwY5HqH4s=; b=i7SB8dO7MSjqrAapTUW2mSAiHOqGMyQ9baey5fsTvBwjz/vqZhSMTPmoRHD4wqCnewIBq4 1LU13qsXwrmw8mA6aBmhq5mmpixO7kNEEc7Nb2UC98E8e/pHZ+nkvNozh5nP+i+XFGzp1Q XrmtlIqMhe+fQUsRfoPwnVMgPbAPVD0= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=wHA2oEp9; spf=pass (imf16.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.219 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=1694625357; a=rsa-sha256; cv=none; b=OVivM9R2bkjvmMy+Q8eKg/79AggNFFUpzAhauEW8SRVW84av7Bv5b+WYhGcBf7VEfKyXib QoJptSUm2rjqGUCYMmE2LC4jZ0cTmXuH/lXbXMEt7RammouxX7UuWSD1Z3FD1KELPN9HZl TxwwV01JAwH38Sn2XwM00GZIuwctZQ4= 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=1694625356; 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=iF8d5PIu0hiypd5p1X1vzHkKz0HhegtzJ4nwY5HqH4s=; b=wHA2oEp9M2Png7tDBErdFmtaGLdMmLLdtrI6Au1BGNIa11sMWCsUSSkjE5ssXZQMsf89kF /ktIdpIwNBEW+HewSCwtqwLFJguCHnjOmSkrdVpWer6wxYm1qnbAq12KYjmRjrBs1FoPap FFJ3/1H7E9nEL5HyfuXogMs6GDWTN4w= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 09/19] lib/stackdepot: store next pool pointer in new_pool Date: Wed, 13 Sep 2023 19:14:34 +0200 Message-Id: <3a7056408e391ff0c66b5f50c460a7b9f796228f.1694625260.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: A4B8D180030 X-Rspam-User: X-Stat-Signature: gf9n9astw78taxemnnf91kht8iq1yx54 X-Rspamd-Server: rspam01 X-HE-Tag: 1694625357-607500 X-HE-Meta: U2FsdGVkX1+ywWXGbcC5HAfvquvR1tclhkEC3sjLVTmQLsERdWwbIeie8UZ6n+h05IWa9j4fgUsniCRbJz5VP2f93tklO4IIrDPSZnXVyUf9R3hkcWeTzoQxtHfIKKjxmWgeDa4hUhCP5pyur7sV1AXLRN+RJqbAwHRRzWhc15kz6wJXq8NGg78XiSK/3MqBOYk1RjLmEA5OzmQxcxqlBWGpBQ1+iA2doji8IeUSrsAZfrTkg3mbr+h3xdR4hx1xKU6au7tk1o85foKRv2+bg74FJy2x7hda/ICxctYgznVJm1Qgygs/6Z6DnAuTGeUCije7U04+KfXcM2d3/0r/gdxaRvY9QW4eJTQPuQxD/C4ypv10/+qyinTp3+/hfTWHMiYl2zB3ImIg9ZIp4vIU7Cfo9hyterjBzdWd7yocmr4D7QzrXlog91Ndg6I85wqP+fFEjEIhTWxwZfKsHPnCA88PHDDOh1BeCreUGeL1ZMy5U4CPTj4/jZMvPLKQWEYkwvOGJCQTx7BO8Uwfq6bNVWd6prLG/NYvw55vuIX2W9EQGuxOumtG+OaP+ndlxM+AYTl4VkveCriEplpCq6cgCYc9Ivll8BNtcL9w2f846fIo2yFcRBTo5SZVx5IMxjiu4KTTPlcq4KuZ90ouXLwJSZRNM9Qq5YK6+ple57RGTysaUScfPUUfR+TaGZVvPRSCInOWdvgAeANqG37xIAnbh5sZj2GPVZbvudGfV3T/+9EehOUPIjfObCPXygT4VlAi5ZC6ajHXhaiHyC5UqMlB94izgRZZpT+77YL0ggmuKhQeGDeSi2ZoexhrH7Fp3AsLwiHzxUlEBAH1CUKNj6984KaWO6laHil+XD5xeed2ZIkSB/x+/nd5nmiYpbKueOUck4AWcFCjOwVJsXTBpukYemI2T0lMhIWShSFpP7I+L39Vo3U+Um3+jGkSkzYuw11+PxrGOupXqMmXQd5cYfi yuzN3k/I gTlj8rlHhC9R2p0QpJx+n7IHfEQ9RRO3W+J6s0T0UpjWQXn3opGzqFDl0LX91CaM4hbi0NtFfKf0rX5fpHoOOi4/1nj0zbSpi7+HrgPTI7US6amMqAletB829znaHN7R4rDuj3zyvBtERynE7TsDvuu0kSw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov 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. Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- lib/stackdepot.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index e428f470faf6..81d8733cdbed 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. */ @@ -233,7 +235,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; } @@ -263,6 +265,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 Wed Sep 13 17:14: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: 13383623 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 791FBEE01F1 for ; Wed, 13 Sep 2023 17:16:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BF5896B0266; Wed, 13 Sep 2023 13:16:00 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B60556B0269; Wed, 13 Sep 2023 13:16:00 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9590C6B026A; Wed, 13 Sep 2023 13:16:00 -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 79EFD6B0266 for ; Wed, 13 Sep 2023 13:16:00 -0400 (EDT) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 28AD7C04DD for ; Wed, 13 Sep 2023 17:16:00 +0000 (UTC) X-FDA: 81232226880.25.51A41D8 Received: from out-218.mta1.migadu.com (out-218.mta1.migadu.com [95.215.58.218]) by imf05.hostedemail.com (Postfix) with ESMTP id 47F1C100016 for ; Wed, 13 Sep 2023 17:15:58 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=sHXeZyLg; spf=pass (imf05.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.218 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=1694625358; a=rsa-sha256; cv=none; b=3TIskP3OcExrmb47TqXoGs1JagDDgRgktx9Bj7T0Gw3Aflr8rM3PWtIzdY9cFPF2PfH0pi YWThmaHBLg56U8Jj4jnUjxbIYqQ25iU1F1ZOoogzKv8PhV9V5q6S3pbTg2krPxih9IVwSz F114ipqfDFqKbXswfWSo5eM//prZjQQ= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=sHXeZyLg; spf=pass (imf05.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.218 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=1694625358; 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=toA317coTc48scxGmgrPy+Qu0+ZANZtHQIsZ7v5QrVU=; b=eZy5pNHuILKOSA5xAqqNj0E0sQYQRlKma6SoRIFpsIr3+/r3Rky0SSFGLTq1WICsocvo/N Y+l8Ln6A9bowSePdKdfR9ktEs8wldTsxRQjefCK9+TF9BJraI9esOTa+p3X/QjDSOUZX7b blVEW6e8AAtwpMlQ6d5wBKlKL8vmd6Q= 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=1694625356; 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=toA317coTc48scxGmgrPy+Qu0+ZANZtHQIsZ7v5QrVU=; b=sHXeZyLgBNYMFPbyE+xXimJU/cWOgsyZs9gQcIuMN7tDfd9VZD/xdZ1RUuIm3sCJedAeFu e0aDkvFDJoyGmmMnm/t7DX3XRxFvKBMSMlx3Iyv3M86jATRb7q2FVbkDE5t+RkcwVcYLOC CxMN6cHXInTQgo1LAFhXDzMsY/RvU0M= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 10/19] lib/stackdepot: store free stack records in a freelist Date: Wed, 13 Sep 2023 19:14:35 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 47F1C100016 X-Stat-Signature: x37gq9kh6sts1ay1hda8cr9y4p8ypatc X-Rspam-User: X-HE-Tag: 1694625358-404719 X-HE-Meta: U2FsdGVkX1+wXmwcs5vOvUejkXTGBpkohpO46CgVBxGFd6pmqvL/IaDN4dKwQPIFW8YZ7C21cUPaYbP0VVlcBZ8oSdhjfr1fVOUjWI4BhpRoB7fLCSWplQuqGN2E09cEpOLNcHv8fLX0w0vNApZcIcsTXQffcdtBvF3xLMdX70CsQiTMeka0oZbQXF+oIAw2gOo+qIk9RcqqgXysXa6xGwd0G7pprnddUA5deQzkiBOZvlUBUzeknyaK4l4fpEV3C9Fhbf2fcG2mcY+Z7/3ksGjx5L8WyL3mxlhUuDvbSGWHlQ4Et/GwVRB7SNR00Vhn4E0e08t9JA8GWWoUaU0n8Y/2nQyKq5nlrkgY2AGeJbGCYgVIJtFgzi7ZGAZKZyzqBwhRXPhlQaHkZLqVk/oRTUsCYjrrMDVkkIFRr3spal6uWgaEz6GScfjlb4f0FRJyASEZp0e5trwZwYasuqTeHZBwPjO1YOREyWKqWsDnz5vW1fzLB4wb3L0V2BM6nelwM9ByQgE+dzQ2oipYA6v/rY9JJpSuinU/VovN1KIJB5W1Etu6RpxhJeCU7wfacg+TWAN4Je3E6L5eRV4+e5+nJO13ZNTsqovLpRHEtxHjTc+QCwdh5L7AiWNdNKVd7x2gMsTs0BWFsXgu9wfaKdlfkMpqb7zoHiSSDLXyYmjiWqT5KXb+f4+/o9qneZHspBBbYD/U+C6qq5JwJo4cwD0sDTGKmHv25Ew7WZJga4f26hevmgcgtqciE3vJmk0Z0h4OiCUMAjWg3napSLGBkaTHzbXwTJ1+EnBOrFz9raShoVZG1JFr0Aem33CT7gjwszL0rodKGnpM58uXXNO2HwfPz9B8tik0zQQZnaO6Qyb8QSHX1FLcqGTB9wshtEtNuhhpUPRJcFpHUwuvn6C8NP8/9ZiEaM5lN+8RVmyhrYj5Nk/Ta0UVzr/mQhqYovYE2H3fF4GirEaPkYpG3mMQou8 QgvwSE5e GF/gAQT4THbAm0wILv/HwbyIhDf88MScRZmU3HHv+TjB1qdkAU/uI1HhZkHGvUQo830CS+3B4fICfEdfrGkVBXgBU28UaSaKVY3kx X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov 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. Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- 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 81d8733cdbed..ca8e6fee0cb4 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) { @@ -234,7 +270,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; } @@ -249,45 +285,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; @@ -298,35 +331,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; } @@ -336,16 +369,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 Wed Sep 13 17:14: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: 13383624 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 19E6CEE01F0 for ; Wed, 13 Sep 2023 17:16:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7AD7C6B026A; Wed, 13 Sep 2023 13:16:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 736DD6B026B; Wed, 13 Sep 2023 13:16:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5B1A26B026C; Wed, 13 Sep 2023 13:16:01 -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 435806B026A for ; Wed, 13 Sep 2023 13:16:01 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 1A8BF1A0E87 for ; Wed, 13 Sep 2023 17:16:01 +0000 (UTC) X-FDA: 81232226922.09.B12F9D3 Received: from out-218.mta1.migadu.com (out-218.mta1.migadu.com [95.215.58.218]) by imf29.hostedemail.com (Postfix) with ESMTP id 00B4A120025 for ; Wed, 13 Sep 2023 17:15:58 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=L1ENvY04; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf29.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.218 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=1694625359; 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=CyWM6kbTs8GrrlAHDj3nsmHfEdZOGlJXB22VnHOfO0M=; b=ated39EeU52r/rDiKatNkvi+w0NwXqbKKCfzN2IMlSccHKJI5Hkzf3XKuetUszNP96uNki lQ+mzlURDWZJugLG0wbtYuIbSF1TxkI3Ae3PCqfVATXkkV4s/yiyYAkIgKxNGizghskkvC UB19El6o/kYcz9Ml5Rhi0YQK33fP2dA= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=L1ENvY04; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf29.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.218 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694625359; a=rsa-sha256; cv=none; b=3PQe94Q3Y0dlq4eRxrDj/FUJMmphFhZ38mN9zBF0EYCczge8hrt/9g6pXBNaF1CRnmy/q9 5hQgpGQVKVnDZSJMT7qgomGLjENgvec55T43Z6oMHnjkP1pT5Y7PUhhgDm3ddlQ230xrtI +ZuJI7XdQjYydHLu6rqBuk7ulGcTmyI= 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=1694625357; 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=CyWM6kbTs8GrrlAHDj3nsmHfEdZOGlJXB22VnHOfO0M=; b=L1ENvY040875Cm/nc0yhrIVtsqNyOhOIXKPx6nLMXWM8PkG8yixRDVFUggO39BNfhc33bX af0kEGdnfi4PSIOXOiDRNlbzmSLurESzcExebX+lARTNP00ezblXTQhVuwb+ijuW97gd3o YCV/x3uEcgpqwXtFTWUA9zbuwGx2h5I= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 11/19] lib/stackdepot: use read/write lock Date: Wed, 13 Sep 2023 19:14:36 +0200 Message-Id: <5c5eca8a53ea53352794de57c87440ec509c9bbc.1694625260.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 00B4A120025 X-Stat-Signature: 9oresee9kzgg449k5qxzxexzfybueum1 X-Rspam-User: X-HE-Tag: 1694625358-882029 X-HE-Meta: U2FsdGVkX18Q6e+L9MEp3knTfPnuPCRT2iWZMLcZaPKOOhyXI1s4PrFgvzzjt3CzWjF/m6CvHUl6o9QZPLdQW+uPBKQov6UIIBuQqhFTkeZoXfgUFt6dmMyCiDQ3i6bzdlYgB4GVP3/pQmCobMttWF+Z53q86pXIbaUyZUb7bi1Am5xW0B71hnw0kqXTAGKgRJbG1MuLKni5qakBa8PUUSWM4cax5UWsjMpKSLwWjDxow/5RnEhnlfN2d/OFjfPIOFUusWybUiIiWaoRu4dsiBKKrunbtD2QzD4vjfBu3QC8fC0FxvQTkWCJdC2t9MP7n9gycTtwBl2yQwv9+AKh864EbJ9PdXsh5fpg0Kau4S3dAWJFLF6xUqOIITLWtmjK6jCypGEVMJ0gZGBDwXS/Tn1UtNOI+8dV/KcqArLem+tIR+RoFvROa7eC/oZXcbWfFXqKXIcsvcy2WQfXw3t9fSWdBVWgSZsllHscgR2oIPoyc0H9Z3QXD4o+Aq9wmjyZ+L3W8zHQynDDxZZPQno3w6qKQllxDmS6hfV883FA0S9X97fUOIYzhQKbKvb90rQ3OHUG/AQgyNwIM3V5RVe6EcJ2PSOPOgPyVOeRli2qJTCQJ2djqkUyWs8tmc0YoYYx572Ud7PdI6LU/oWVgqldZeNccjYtGWbyh7TU/2ZCEI8nvQ/dh5cwqRQd3ODQBgUsky7xrrIckUMRwXG7NKAbYC6Enpd7GJyy4MnP0OOiKub2FFbfsrBNlv3iM0gdwdZ4meJbPYJ0W93rSm95NhsCki2+OwmLuy5AI4EzYNwhHzmKeM8LzqV9R0zBWTCzxKys671CDqwSpXNPda2Y8CnR8R7r4X8gzv3m6EpvMAx1Ib9gLBF4Llmp/UMIr07kPHOekrUAiROHUKnHTGyQ0fROHUchTBniEVAuz/ZWlUnBCufST4YZqPWL/qHe6VQ3nnOcqNR2mZnDM1j6BzTfo0l Wl5VuZn9 fXVfX61/y7X2+D3IxS6oVNvnXUdYUQHQ3JlqfTs6gVI10Lkh+x+59iNMEn9jkHjp/U5KA6j1PGscLfbOIGDJDHYL7OVgF34+JN+gD X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov 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. Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- Changes v1->v2: - Add lockdep_assert annotations. --- lib/stackdepot.c | 86 ++++++++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 39 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index ca8e6fee0cb4..0b4591475d4f 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,19 @@ 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. */ - if (!next_pool_required) + if (!new_pool_required) return; /* @@ -279,14 +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; @@ -298,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; @@ -332,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; @@ -367,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(&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; } @@ -420,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 && @@ -437,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; @@ -456,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 @@ -485,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) { @@ -494,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) { @@ -509,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. */ @@ -533,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; /* @@ -544,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 Wed Sep 13 17:14: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: 13383626 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 EA733EE01F1 for ; Wed, 13 Sep 2023 17:17:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8C03D6B0270; Wed, 13 Sep 2023 13:17:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 86F436B0271; Wed, 13 Sep 2023 13:17:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 738566B0272; Wed, 13 Sep 2023 13:17:03 -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 645E86B0270 for ; Wed, 13 Sep 2023 13:17:03 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 2593C1604E6 for ; Wed, 13 Sep 2023 17:17:03 +0000 (UTC) X-FDA: 81232229526.08.CDE3016 Received: from out-227.mta1.migadu.com (out-227.mta1.migadu.com [95.215.58.227]) by imf27.hostedemail.com (Postfix) with ESMTP id 38CE44000D for ; Wed, 13 Sep 2023 17:17:00 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=R3Jti2k9; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf27.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.227 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=1694625421; 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=4oWt0Nx5j2++AoKVMMksnS7f1HGHVd77ggPp9l5g/Uk=; b=ZUkWIbWzRkVeVBEUCRfnuuCoz5R6SXYBafYj/L6pCjWRs+zsISHsmuoFwiffegdCQ82pa3 UlCcqbTkHFLgw1rZ9HIL/yoxvdsISBWb3DO1gZ7e4T9qn5xVsIP3oijKcGEGoromqlsR/w sn4YS831D0xMkUpxRhEHt9Bq8EVt61E= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=R3Jti2k9; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf27.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.227 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694625421; a=rsa-sha256; cv=none; b=RpYnAvm7glhkhj4eSwhtBqCXMK5oPhqNPPRTPFEbff3f05faBMMlNW8mmHOoexft+Co0hV N0SBVq7NIqDShKYHAUhWS8DMCrSs285cd8dF89+yVVpEIuNKAcgRWLlmiSmQcTh3btOyHE mOLoO+TeUcrS1Xrd6snM47r3d9Pu2eE= 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=1694625419; 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=4oWt0Nx5j2++AoKVMMksnS7f1HGHVd77ggPp9l5g/Uk=; b=R3Jti2k9kB0yy9qtF62OlZ0XsrhyZBxEgfMpCsSO0Xtmx5zn9sCpRYhM/HxR9dhZXP7w4C yeT9mFQVFs12hS4aIQ0U95P2I39dlKW9Yh997irLZ5Edx98Q9noaq/ksCuEF2tKwOTWVLN 0q5css86Vw7XJNBgfVgPCjGAbeXVxCE= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 12/19] lib/stackdepot: use list_head for stack record links Date: Wed, 13 Sep 2023 19:14:37 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 38CE44000D X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: j7cq8ukdpsrra6qhaigax9qtmr9kmfwh X-HE-Tag: 1694625420-841096 X-HE-Meta: U2FsdGVkX1//L0socaqcTvfT8UCFLWQBOE8qxknDTVkER7Y4TApVuEPOqhjT4h54MR1QfFRPREzWQK7cxZVeQaRGR5aZ7QiYxbwp9MCMp/IMEgAtaIgY/YGhFxgmXUAXyuCPOXqVkKYFugg+Tc7cUk2aQqudTjthLoZvg4i84T2U0/OwFboO2bdE37u6Fe+c1QpaAWHu46ti6RpUGbafZ8NJa7KVRy6zIF0nFHnrwq2mdI00B+nRSVbual+V61j/nncbt+bAjWYx9hQNzypU+qURBwub0XKOBc2VpPhjwx+eE7bY4cQBWpYlYwyy7M3o3oOiVTaG3kYB2uNo420j3T0GTtvXbh5xtxYLjXFF2aosJQ7McHsR7sC4sWELqboRmo8eKYUmDfk49+RQnlT4kXggpdQb8qJ8XrMGPGaTFp1eYtf5D/XuLSysLecK5FZBkw7FYkPmhaOD+O2L3igMymrKa03n9UhehpDvPRefol/cvQvya+3iHC0dt5S7J2URQrZcD08urw0QIqoO9onoyKZJaSjdkDEXF0H3fYSELr7BieVxtkYnjjR5DC4Z5PpBx+xd/rjmC0PPOAYaod43VapOpOsQX6jbis2BChWrDT66yBmnS0mMndLxZUUjiLVZhAlRFVS+doPNNGngcbNrIVwPM8eUVxr2srs64evt76PDZwrcopx76AnNoLyJ2+EXhQPLD8Q/9lWzCV0WRKa5jemJPWCfx5KBPxdRnNjVxGRQEg3zNznGJ+BwOQ6YyAEHeZlHPNHE0SuL0FA0s6vk0kvojLtS/fuJNdJL22DV03BYim1OamaBQjBWq1jrL2vN5YESEYcBR75Ab7QtKmMPdVqR5ZlB1qbwBjOET+YdkRHE0SJ1NIeXFFAZ25CcRdzcXJLl7owte7VxsJTGCoNro4YTaJXEczSmVHYcZRwO7PeBjNsMTgJsR31WRHcCsYUnGtWlwVk9vxVi42uTLTN eUyPiQjo ndbCw82FXPxr2hjRJ/hHxzw5IjGQaNcQYZ6yBbIpPwtVd96rCDE22uiSRQnzO++Yvk+EVT+n9mxAKbFNYMOJw3vOUCjLutJHlcgnmk/nBmjfwendhOrSqU2nzhyg9DP87Ajhicf0/HpUgar2DW4U2Umf5Aw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov 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 v1->v2: - Use list_head instead of open-coding backward links. --- lib/stackdepot.c | 77 ++++++++++++++++++++++++++---------------------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 0b4591475d4f..1b08897ebd2b 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) { @@ -152,10 +162,10 @@ int __init stack_depot_early_init(void) 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,7 @@ int __init stack_depot_early_init(void) stack_depot_disabled = true; return -ENOMEM; } + init_stack_table(entries); return 0; } @@ -205,7 +216,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 +224,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,30 +236,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; @@ -289,7 +295,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 +346,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 +419,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 +442,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 +470,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 +502,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 Wed Sep 13 17:14: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: 13383625 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 B96F1EE01F0 for ; Wed, 13 Sep 2023 17:17:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2098B6B0271; Wed, 13 Sep 2023 13:17:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 1B8D86B0272; Wed, 13 Sep 2023 13:17:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 07F7A6B0273; Wed, 13 Sep 2023 13:17: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 E77A76B0271 for ; Wed, 13 Sep 2023 13:17:03 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id BC534B40F2 for ; Wed, 13 Sep 2023 17:17:03 +0000 (UTC) X-FDA: 81232229526.27.D6BF2D3 Received: from out-227.mta1.migadu.com (out-227.mta1.migadu.com [95.215.58.227]) by imf06.hostedemail.com (Postfix) with ESMTP id 002F2180020 for ; Wed, 13 Sep 2023 17:17:01 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=riuufySF; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf06.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.227 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=1694625422; 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=DC9UmNBNkxmGXhCZnNWnsoHzitcm2pHd/lCALP3GjjY=; b=KP66c98h0+ECuksDFYiNr7pKkp35UI+9gGELEoaPOqSeh40nDnrcvjc30asWmSI+KBMY2U 1oxIAo8lnU2ko+WyNIUoEvgEFL8OwK5J0GvjWLDL+k2OR83awUUw8upbiAjAJIq3j0Ed4e J2yd3M48MSqi1sB0KY7544nkTdmpZZU= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=riuufySF; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf06.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.227 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694625422; a=rsa-sha256; cv=none; b=pdh8K+Sm8f0jEoteHsIMAke+PHj4P0v9vjlxz9EC0LxIF5uQ+ueuSObJX1to2/sxdfTTTF hQbSqYac7YbfXesDKnG7JZMG7LkPbqrnPC2nkrftvZmIvKOowPbaH6O2b2sy2j2StDd+2h oMB81JD0sXgyYIOUp+H419mG5r9+s7s= 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=1694625420; 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=DC9UmNBNkxmGXhCZnNWnsoHzitcm2pHd/lCALP3GjjY=; b=riuufySFxtO02s+5vb9CAH57DenGXrZTUkunEd+rXmQlCvLne7cXHWGh8H/JxSrJaHldo1 +Ti+81/7EXiWikASrBXA2uiFwVtaXJN9opiGB+/Z3fX20LDW1ZVMd8ZuCg94ye7sNtPe/p DZTAmOjByDol9k695KLblDDTg6NOK9s= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 13/19] kmsan: use stack_depot_save instead of __stack_depot_save Date: Wed, 13 Sep 2023 19:14:38 +0200 Message-Id: <7f6b58fda637238ffc0c240e7fd3b3a6673d9d91.1694625260.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: 002F2180020 X-Stat-Signature: 5og8r8ehu7e5rmttrkctbhnaj8y15yn6 X-HE-Tag: 1694625421-114981 X-HE-Meta: U2FsdGVkX1+z4rXcreAv6Gn7a8n3KdH9/79shLCk3EMv6f8ej04dWV+AAD62NGlcSte6qntbntoLEqxahV6SJ8YaDIE4MXjgDy1SL+ONvifpb6QDKqxQnCph/c2lR1oRAG+ReLqRYuFJZrM3ZuOsHPgs40dnhwrSszTzkWFgOhT6U3iUO739JaJZZKNwfbGcnBAPnZAc6sABJmIzwfn2eYFrS+DWyr8LqUaY0+vRYJlXPNZRe5H3ZDDz/MyZOopJ6unmg6muPwz4Ft131J17r2UIrwsWp0aVCfDmtvbGHf6xEgZBr9EOw4bmA4dLLzT8YFyjGkUUlqnnSBuL11/tn76sjuhLWVkC4MOxbIZvKTczFa2tYYHLdOuZ8MlOoLUmFWEQ2ZajwTlfh7AxklRN6N4j43HEHXjeKadIDoqg9VR8J+u9DMy9lkPjY/b75mQfYMPtVZ9cBmLG1eJOK3NG774ebRIMDDN+/Oa6a6b1ZQdMFIjOqxHfRJl3xSwrRErA0uaSnMFeahEatH5UZ7zFmft99sIazG6OogHXO835Bp+98mIMx4gYrcIlwFYgmOzF3BHeyqLtBoqhNkouC0G3FLtyCaqVgja8iRWhMSdny9K5sDyGaSJ1vqebtfGE5/McpGkE006sVeyxutzHaRP6t30Y4q0qsLzHoh9xF5f+ma1GwPNvPEOlXyWkQ3PGxPDrpImJidc0Yae9Of2DJpQajXsfPFj5ZMXUNlI/+qDaH+596CpvWv+6AZueSmaKDj6fDthxFJyfRVd8Wl/uulmnAbJYO76SeTwLN1t5+l9kGEFOukH1yoLLmOwnxbvFj0+ajkC3f+erlDdGYXTU3O1mKsI85JZ055s01elki0PbXGckvU9aenTts05ScPVSs6Vk6Xb5M/376BuI2nRcamWsjV3vlxsA+JAHqVybPWeWSkxOA/9XDLLMQP0W/wYggXqrd6k04wgHCD5EbYTW2P5 egCtKUfp EH+mPvLzAYVE9qovbWYDoiwpcdmdeunIAWmB80h7o8bSAg9eLueMIn92QrlYmCIyy3eShhRYw29Ni4y83X49hQ6Sc+6xAetg5gS11hFiQJ2fdNzWHCHtqF17Av23FLBCFR9Ko+SrvNBvzF37LmGEjXGc57A== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov Make KMSAN use stack_depot_save instead of __stack_depot_save, as it always passes true to __stack_depot_save as the last argument. Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- 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 Wed Sep 13 17:14: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: 13383627 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 9493DEE01F4 for ; Wed, 13 Sep 2023 17:17:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7D5206B0272; Wed, 13 Sep 2023 13:17:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 785BA6B0273; Wed, 13 Sep 2023 13:17:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 600476B0274; Wed, 13 Sep 2023 13:17:04 -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 371F36B0273 for ; Wed, 13 Sep 2023 13:17:04 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 17CECA0453 for ; Wed, 13 Sep 2023 17:17:04 +0000 (UTC) X-FDA: 81232229568.23.9B4910C Received: from out-229.mta1.migadu.com (out-229.mta1.migadu.com [95.215.58.229]) by imf28.hostedemail.com (Postfix) with ESMTP id 46CA2C0022 for ; Wed, 13 Sep 2023 17:17:02 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=iZqHPx4f; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf28.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.229 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=1694625422; 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=Y6OFfwxwVZx7oqRr59fNDzzOOQmJw0k24eVuDSDilTk=; b=LqaURqiuOWurhxWy1vSbZCgOjpHGLSTaalR2cEgTr8A3KDVDYus0Cl3sxn5tfhbQRgYcKP ONtZwFyF97oDK0VS21S9Dc321mC5Jp53rKCYsXp4aZUsakuQMyNZatM3dPsBZjtVB8nvRX /fDgcGYVInNAVX88BRGJxzE4vwo5d/8= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=iZqHPx4f; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf28.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.229 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694625422; a=rsa-sha256; cv=none; b=F/zKXhzsw7nU/JTuqspSTtcOAr71x3nViSJd2WssJZBma5Vdt/npdZzI0SAc4E/DOs/X9V umMsHBevHpNb8HJwwTjrOg6URRvSHVVf8R+9MzP+mAMv0eDQcd3I6f15cdYV1DfZVUpC6h OwowKCkQQBLuJ1B1RYAy/GRRhqePwRU= 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=1694625421; 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=Y6OFfwxwVZx7oqRr59fNDzzOOQmJw0k24eVuDSDilTk=; b=iZqHPx4f/wcAAX6htIe3HmT20tHPT3znA2vDH4S/em2UcGFdml1qecOGFN5tBuJcXsaeSs NiEGa/AdC8wq/6VxUz6W4Joy5rfNfXtgBfp4X3vBzhkONxZ85SEY4j5PzMbE8moO/6iQxW P9SPTh0BEOQwZjBHsaixmdVGcJAg7ZU= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 14/19] lib/stackdepot, kasan: add flags to __stack_depot_save and rename Date: Wed, 13 Sep 2023 19:14:39 +0200 Message-Id: <2a161c99c47a45f8e9f7a21a732c60f0cd674a66.1694625260.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Stat-Signature: e8ixs79x3cauay1mgfi9qfm3ac6pgxi8 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 46CA2C0022 X-HE-Tag: 1694625422-229533 X-HE-Meta: U2FsdGVkX1/4BmzSGCLSZcg7/l7BHA2zinleQQlHEBCAJfGyhpoipVabn64uBGp7GXSiyqToOuI3S5pBjxPV45ZQkpAvYZqyC1x6XmNM0mAdPUIPhSGYFoSRkkCnAGDXjEtx+RAYyqIKfYw8w6SycvYiajk8xPp6IAy8q4Ga0d2nxPkAwXP8Hb92yZENyzjb6ZyBPZ22ac6kZP/AzIXN7BNZE9ArwDlwovyWAf6kLIithlbPPAxIZDZGOSSiCjbiQK5hk+0nuaW48waeVcIPGtCWXH068NMwrOPLdYqmhKXUkqQijNK2qieZsxNQkqutPv80k6DLHvSa8g9okB5PJEqJtsMD/yKBLjS8hX2wauqbvX2aO+qHeEPguVX6mCPH2bkaW4fYj3Uqvv4gsDRwMR7b/gbj2MICS1Tebz9rStkrZRUGaoJcn/I+M/cv4uAG2iEgUcfFqCsiPsqUAr5hqYJXoEvwhvINdHIZHYzzf6n4LqGy5tmFhEvXc9FkWC3moNoUvT0X8oaTxUjax6SQNfPtL19ql07GOXclOt1YD4s2LFvVnmDuD/JwF2NfCEbl7ttmI+UQ0ka+7vloG+hKxu0HVsR81SiqfzNoVGkYJMkothv3SxCtcjgva3Omfq+MH/fp153aolH2AlQQgWqMW4D8jwFixUIn5A0ZvbQL24kViM9+E2JiaH4nXagzVBbOV6PRe5Iymhpxeq3T4b6nay8irpKRpuX/5yRy0x61IMIw6X1DcNQmebrhOp9fJVZeqTxlBK3Rvqe9mubHMblzhAsAz42XNgTHVVgZhuP942T1tE3MOEydyZue3qSfef/h8Cw1AyTZa9pFtnnT7jKBcTv0eVXukQlFuW/gXLnWaOANzHxGqCHPSPk1pzcOzOVzSAaEi4Cuva82675rh6H9+LJmuxUief1fX/t7HhCUGT66iRal7ftNEoBGZabsu8oJAhdD80wEIkW57b8fjmX mP16WBHh 4kfPpT3E8XL/mavQXRU/jSj7gc5NbQ5DTHPFm/R5RL67hXIDbIMy8B56Fzu90p5V2V7Ye3TeuGnDZJQgb9NuRM39XMGwO70seuHPFOPfaih2yMHW8Kod2c3NG2Qz1TiMpAKeE3aqrPd3QSaB3ggjy9/S5CA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov 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, Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- 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 1b08897ebd2b..e5121225f124 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -438,19 +438,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 (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. @@ -529,13 +534,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 f70e3d7a602e..de3206e11888 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -370,7 +370,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 Wed Sep 13 17:14: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: 13383628 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 72251EE01F0 for ; Wed, 13 Sep 2023 17:17:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5B3466B0273; Wed, 13 Sep 2023 13:17:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 562CE6B0274; Wed, 13 Sep 2023 13:17:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3DD9A6B0275; Wed, 13 Sep 2023 13:17:05 -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 27A8E6B0273 for ; Wed, 13 Sep 2023 13:17:05 -0400 (EDT) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id DD8A180412 for ; Wed, 13 Sep 2023 17:17:04 +0000 (UTC) X-FDA: 81232229568.18.526188A Received: from out-211.mta1.migadu.com (out-211.mta1.migadu.com [95.215.58.211]) by imf23.hostedemail.com (Postfix) with ESMTP id 0BD4614002A for ; Wed, 13 Sep 2023 17:17:02 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=rPLyIHlB; spf=pass (imf23.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.211 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=1694625423; a=rsa-sha256; cv=none; b=EpSDglW9M1EtBHd2bjOIu+ML5r+tCSbmCKxp/yBdtpmDKI98Y7uNTlbq03HZx9LG18FK20 weKxkjq8hwRHfmcAWeNCJ/I7NMbt3gcD2t3gpyskPUvtEwnNia7uZqGCWSUlKtkcg+HIJT 2pDLh+DDrW0WkJRRNXLA6Ga//gVF07k= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=rPLyIHlB; spf=pass (imf23.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.211 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=1694625423; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=iWiSl9lpWFlYTb7e84Z8P7HowCNwXEfuw/46H+rnea4=; b=7DuflG/QgN0YIDR7Z8lFfI1QffmjVWDgXMoucZMCQq9EiSxzwWsWAnraYmKfp5wvy76675 KFQiJEDJL/PpoUuXEySCQho0W1c4ACCNZhQqumNJC2FvdYKgT9OOzz5WRsQuZ1ClhdXm8t hyqMvM5n9Ax1fJoluCGACag6Vz4EeK4= 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=1694625421; 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=iWiSl9lpWFlYTb7e84Z8P7HowCNwXEfuw/46H+rnea4=; b=rPLyIHlBbsaWO60jp650y+oiahqwCIrJ0H6GB7tgNClg622J9RmQzHLj0jZ3Mte7u2yjvx FoH7zGtya7Ki6Pakd+45misXntm3w9rYsO9/Q7XXHlRkAQ3iapWRAe7bP72+7cHr7LmOPW waAC4TKXVoO/HQ0rZPchMh9OZqw2yzk= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 15/19] lib/stackdepot: add refcount for records Date: Wed, 13 Sep 2023 19:14:40 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 0BD4614002A X-Stat-Signature: kf97d17xkukfpt993189xttba7k6dbat X-Rspam-User: X-HE-Tag: 1694625422-21405 X-HE-Meta: U2FsdGVkX18dzUDDMnTaF0x+I0uJq89jHtnDdauorNyFEZIyYtNkOb+gl4MjmmtKihNp48gOHSOBPHBO6brO0wuKmvQ4CSE9b59Sh5XIPxryexgDjd+Fwkgdtefi7HF2QK7LMQAc3i8mMcKjf46Z6mw8zBpAE6wV96GRt6bgye2tYgUhIcIU1xuuIXUY0AKxpF9ffj6YfQxb5yi8Cl6+dB/3JZ/BlUX27LOHOcQv9NsTYKNw7L87Ce8CObhwjXoGX1Ll1WfJk9KiaLnAnd01SrC7ILrT2HVgi8QRf1sgAd9+y7cguroSUQMLJbE2heVA666JvzeLtlCvU7Lh0NO7GwdS98KnsLWjFKRhZrfvNNJoS6fxt4O1tKjcz2FNYYEVS2DcwjpLBxeB4wrcJJVeOl1wrrkZBJ4T27c7s3CSNOUM+W7Ugm9vUhANBWyUBfbptUiqSYgOnttbSowzwjHGw+DzJNx5Pe62TtGBcgsnGknSHvPfHUJKXo/Vh8u0LwaQK5VpzCZxIfmehhVhe2NBovKcmebNl3VQLB9JC6sxufKO+Ak47SfzZKOsJFUp8iZm0zn/9vgRrul1xttjfdYB7fek9XRcZFBOYO+8eh9ricw9onBocOCVis1X+LHuxP3nO5pNC+QfqYuuEY8ZQxe0dlkgNIeJLqsu1jxQ2hjYCi1xgRvWzlUKVWvDkldGCzwUYFIrRpwWyw66gnOzYHOCxb/2o59SvbQVLNMZKVL44Gow3gaS9XE5IvBdv5HYTYeIhBdo/FJKrBydiOhCZl5y7lynEvcvuSuq4O2f7u2arpj3M41b9P8WdUD3bjS7esW6Eq4ebP54QExh8uEY5z7CSDqacSeRfMur3iJJzm/tyTJkamIy0hIBZZ+CP9JRbbg+6usLks4Ge+TQvjXHaEZ7Sg6BhOxtPdTEx5xMsLETWuzY6o4+0VckALF1vn5C6p2TvVuRSc64AmKwL9fpVBU 7EV2dEw+ DWfRW17mymFJ1CTjgXzh0bvxnLAUBBkL/oGqkkzKHprk/aUnpeMG4rYdeTWAFote04iHtNBLHUpZk4QlW/UeePCs6lw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov Add 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. Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- 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 e5121225f124..e2c622054265 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 */ }; @@ -361,6 +363,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)); /* @@ -477,6 +480,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; } @@ -516,12 +521,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 Wed Sep 13 17:14:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 13383629 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9DAF3EE01F4 for ; Wed, 13 Sep 2023 17:17:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B5F6F6B0275; Wed, 13 Sep 2023 13:17:06 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B09956B0276; Wed, 13 Sep 2023 13:17:06 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 95E126B0277; Wed, 13 Sep 2023 13:17:06 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 7CFEE6B0275 for ; Wed, 13 Sep 2023 13:17:06 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 586251CA64E for ; Wed, 13 Sep 2023 17:17:06 +0000 (UTC) X-FDA: 81232229652.26.E10B19B Received: from out-228.mta1.migadu.com (out-228.mta1.migadu.com [95.215.58.228]) by imf04.hostedemail.com (Postfix) with ESMTP id 8D6944001D for ; Wed, 13 Sep 2023 17:17:03 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=F2Ii6+s+; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.228 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694625423; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=AuXPIx9M1sa3aovsp0XdlaM599Z45TGxV3EusWHlaB4=; b=QHGRfnfPeo2LGWbhSH6ZgFSZd7kX76//2/bZISJ6pOlr5guY0prkIPsQMmlRYH2sigO93d H75IK3jzODSDEw2RpN/2LVxmU7uDQ3wVRoGoEApIVBJ6oP4hUDampL7HmJjTSPvIHzmCu/ saN5rtBfFfZwa+08wDuBpOWD5zLCQ/8= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=F2Ii6+s+; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.228 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694625423; a=rsa-sha256; cv=none; b=Rj/kclVY9s7KKu5JNw3PuUD1jf+uMpaK02Wo06GZxfvEYimSa98+MjzgDqeI392Ghy/a88 mCsiOHeRdyzjR1IvftPA57VmtpLqiE9da21/xtcorNrUyMt8kH7FIK2YIt3JHSaSAnA8Qu vdJBfkRmGlk0ZGJF7x+n7A9ZfrEkET8= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1694625422; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=AuXPIx9M1sa3aovsp0XdlaM599Z45TGxV3EusWHlaB4=; b=F2Ii6+s+cdiC4H7z0xYGquHm32HzsvJdDEssh13Gmfxh6dgxf0ImSZW90i2I0Ul8XHXFZN EabRGGiqHKx8/fjEiRgDu0SjJmNsgtQaiOLP1FFmQ02FkMsAmcJU65Zs5fSzoSf3rveOpt b+/xe3k7MacpwOlBvRZDIYPeX/d3pF0= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 16/19] lib/stackdepot: allow users to evict stack traces Date: Wed, 13 Sep 2023 19:14:41 +0200 Message-Id: <1a3ea1e6b82f95eea644875756e12daba6c4cc7f.1694625260.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 8D6944001D X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: zfyfywkippueqwdgfarhynr4btkzsdqg X-HE-Tag: 1694625423-46454 X-HE-Meta: U2FsdGVkX19RjSBG5Cooa782mxyO8tB4I+Gh7mx9Z7zlfuEZu5b2UbRrYK8T2v0pWUF3GNiIsGPQ1C4feovdiIGTYT6AAj7dbPzem0a9tWUQjHq5mg5PEUO+AB+SGQqM9oCtw4nDB5mRVkNiiLfiMUkhoaryKTGsRHSu1gKKk9wwY2bUKofnCGoKRCgf/jM2gIgSjajy96mYmt7O5eRdGYWsoP+kfqsg4Fvq0LgBvEh74PKY3UuAiRZCj2oLeZwIP7MCHrwhfvyOt8rKEtsVyoiA7CTeu2FCvjVh+MwhsBkwYzdgEq96fOmOHjvvNmQ68J35ximgBRBT2TiJ8cpaXrzQIqEvxk1oB2+hwtTYtloi50/Cx5KG/xtYFGnLQ2Sh8rmWLziGiH7su9dVhCl6eQCjBgX1cC0kA1D6NZvZMnpLZ0pyyC6ZDMM2BIA3MkjgO1KmDOqvrynlqbUjuJUgVkAwJAQNMOopjMqN4USP7ocz9TuzzTxB+/kNPiW+o3M9uUdZKmovZZUxxLfhJ33LTkzIcDs/icNo7cKVLHE3MXDxEtVVxcyVGNvi4JyRnx7wPD0fYMNuS1igXcRQlueq2tOoLNYiQ2aulGtA3XIH+6Mi+QjWXTzy0o+oJWBdQT9gzBS+oOVVMtW1Xjb5ckSeZBccf6SLK3fXRApmG1tGUYcCJrkCR8T4OXt0kuadxy0aRQLYiIV4FvFXmqxSzOVeEFCgPnJZZT4/2PIrIja78wxTFp8Skk7dFb0Pa96anrVF7zhcF1tgCfMQADlzIzkTVXiNX1HpNHK/CT4lBm8CfUwQLEN3w8MMi0gLWzLYI6/KOOZwH/XT8vbiSLYAxELhqIv8Wb8gNrv1IGaEBcxhd5sE13d2PSx1V7lSj5eah8Jvi/8CqEerr8HOzW4jp9oNwQvSoA0TpJEiC/MOv+zCd/26fE+7aiRafKMSgLvGatutOAoRTz5pBjn9PAyJJCF yaF8fuhB 8qlGe4DLiLtKfLbHoyv2nvDGv8v+GjeS1B/HJdfsRL7qgeRHONwBeujgcjgrNi8BWcUbWdz4biNpCDSp7jX9xtt9QQO1piN0IVlNN X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov Add stack_depot_put, a function that decrements the reference counter on a stack record and removes it from the stack depot once the counter reaches 0. Internally, when removing a stack record, the function unlinks it from the hash table bucket and returns to the freelist. With this change, the users of stack depot can call stack_depot_put when keeping a stack trace in the stack depot is not needed anymore. This allows avoiding polluting the stack depot with irrelevant stack traces and thus have more space to store the relevant ones before the stack depot reaches its capacity. Signed-off-by: Andrey Konovalov --- Changes v1->v2: - Comments fixes as suggested by Marco. - Add lockdep_assert annotation. - Adapt to using list_head's. - Rename stack_depot_evict to stack_depot_put. --- include/linux/stackdepot.h | 14 ++++++++++++++ lib/stackdepot.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/include/linux/stackdepot.h b/include/linux/stackdepot.h index 611716702d73..a6796f178913 100644 --- a/include/linux/stackdepot.h +++ b/include/linux/stackdepot.h @@ -97,6 +97,8 @@ static inline int stack_depot_early_init(void) { return 0; } * * If STACK_DEPOT_FLAG_GET is set in @depot_flags, stack depot will increment * the refcount on the saved stack trace if it already exists in stack depot. + * Users of this flag must also call stack_depot_put() when keeping the stack + * trace is no longer required to avoid overflowing the refcount. * * If the provided stack trace comes from the interrupt context, only the part * up to the interrupt entry is saved. @@ -162,6 +164,18 @@ void stack_depot_print(depot_stack_handle_t stack); int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size, int spaces); +/** + * stack_depot_put - Drop a reference to a stack trace from stack depot + * + * @handle: Stack depot handle returned from stack_depot_save() + * + * The stack trace is evicted from stack depot once all references to it have + * been dropped (once the number of stack_depot_evict() calls matches the + * number of stack_depot_save_flags() calls with STACK_DEPOT_FLAG_GET set for + * this stack trace). + */ +void stack_depot_put(depot_stack_handle_t handle); + /** * stack_depot_set_extra_bits - Set extra bits in a stack depot handle * diff --git a/lib/stackdepot.c b/lib/stackdepot.c index e2c622054265..56f2abc03717 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -398,6 +398,14 @@ static struct stack_record *depot_fetch_stack(depot_stack_handle_t handle) return stack; } +/* Links stack into the freelist. */ +static void depot_free_stack(struct stack_record *stack) +{ + lockdep_assert_held_write(&pool_rwlock); + + list_add(&stack->list, &free_stacks); +} + /* Calculates the hash for a stack. */ static inline u32 hash_stack(unsigned long *entries, unsigned int size) { @@ -580,6 +588,33 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, } EXPORT_SYMBOL_GPL(stack_depot_fetch); +void stack_depot_put(depot_stack_handle_t handle) +{ + struct stack_record *stack; + unsigned long flags; + + if (!handle || stack_depot_disabled) + return; + + write_lock_irqsave(&pool_rwlock, flags); + + stack = depot_fetch_stack(handle); + if (WARN_ON(!stack)) + goto out; + + if (refcount_dec_and_test(&stack->count)) { + /* Unlink stack from the hash table. */ + list_del(&stack->list); + + /* Free stack. */ + depot_free_stack(stack); + } + +out: + write_unlock_irqrestore(&pool_rwlock, flags); +} +EXPORT_SYMBOL_GPL(stack_depot_put); + void stack_depot_print(depot_stack_handle_t stack) { unsigned long *entries; From patchwork Wed Sep 13 17:14: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: 13383630 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 E09C9EE01F0 for ; Wed, 13 Sep 2023 17:17:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6ECCF6B0276; Wed, 13 Sep 2023 13:17:07 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 64D956B0277; Wed, 13 Sep 2023 13:17:07 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 479606B0278; Wed, 13 Sep 2023 13:17: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 303CC6B0276 for ; Wed, 13 Sep 2023 13:17:07 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id EBE91804A3 for ; Wed, 13 Sep 2023 17:17:06 +0000 (UTC) X-FDA: 81232229652.22.8AD36B4 Received: from out-222.mta1.migadu.com (out-222.mta1.migadu.com [95.215.58.222]) by imf22.hostedemail.com (Postfix) with ESMTP id 209EAC0016 for ; Wed, 13 Sep 2023 17:17:04 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=dUDWfm9G; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf22.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.222 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=1694625425; 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=P7zzoSFiMvzmGoSw/qnaD/dTzTEPv955C3ixzUuxzB0=; b=yAvQ3OjNmAi/QdqWSYa3xPYVy2VfQUFoDNdDifmJLLgrvSHlErd0jM5xayuVs9CvhAjkRd svLvD0GhjWJQSpijAJHip0UjzhMm9eQFmwVMBJwuHLF/k+fP+W/gMTMd8aGaoTg49UDXTw juGsl9KnN9/WmzKZ7XN6uxjUg9TldlU= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=dUDWfm9G; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf22.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.222 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694625425; a=rsa-sha256; cv=none; b=anJ1egy1rEwNEl02MfJQyYSEIDi/CIIEYecc8/uiEWRJZOWVd6WUlAhGVjgTC5SL72XMKX KidJUV+vyiLJVpCP5xXem8bAXk3Q29LlGbOHhQkEL+iNZuMMHKpO63kUgrRCwUhGDfqzvY htnuJ+JKMzy4FbeVQ2mgkgePYtQH6fE= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1694625422; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=P7zzoSFiMvzmGoSw/qnaD/dTzTEPv955C3ixzUuxzB0=; b=dUDWfm9GQJCwXn5Ns2lo8cMMrhaO+r45+sngvwNP0vGullsCgE1WTcWwBv1boZZKILIxby Iwf95QD/YaEE0XqYwS1ZkwuDWLFwJogmyau0zsHzueAIHLAi5uTqqsd2dYHi8ddYBLSeuh ZXP72m6TnkiIFBuVA1VHgbYB3a0DFuM= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 17/19] kasan: remove atomic accesses to stack ring entries Date: Wed, 13 Sep 2023 19:14:42 +0200 Message-Id: <556085476eb7d2e3703d62dc2fa920931aadf459.1694625260.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 209EAC0016 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: domysw565mharj53zpm44sdaszh5cnqz X-HE-Tag: 1694625424-548670 X-HE-Meta: U2FsdGVkX1/2rt9XlFp3ufVuWQXxd+c3Y03WVoe4GywF+iwY/kR/tMSBfQRMXXepm+618lBQI+NC9ugUVWIuHvS045/17+NpixP0bnI0dXTtTye9L0k5+FDhzfvcFdRRvsWNspuJojq0NI7pRYNdCRE6Kgnxn6S/bzaEfpC70LMKUrFFO7WHuGMs++BsyxxhoS+2BovGEAEoevHQK6WRgFT7M2xTnfJMm4AiRBKGnd+Bk3PcoB4fd9ksPvdAUykqlm/0TGg/SOM8/0U9WP13li+BaK8Vb3Wr0PVtZOzt9209kr5jsL57A3C5iArz3Kdw3Ix4IzXUV6EvF/fbz+78fXFloayJWeAWaGJVIUEIdV4IUT1X05Dd20XfrOANAYBRkrAY8AP8XasDtqMuwG31WiBiT88grXJV4ov/a6afx5wBFvtI5gewtCdmPKl19IaxCobKfWNDGGZ7o66WMItMZR7RyYvEDk3VHNKVVIWfhkE4azeJt+3ZcBryCcevWFJc+E3wqcrKSH/jdB2/KP7L6mPCkFwiw9+uSFlLvIJbP3egeHlI7LRuwwBKtp/Zq6uhJHZcnTSrs5wfAmDB5vHTz5yTEhxdHRHwhqxZyVGnADDgf9UCCb3rfRpO3ZKueTwYZfmQy7W4dju0w1lSQkXWqxCtxA4enjS6tCdimQNlle6H5z56Db3V7TR8vxT7/cBDKarkpNMu0HLGrQx8yqK1Ye4mcqe/zSXb9dFmoQS6jFyFmLC5ZtpuvKOoLdRHq22HgjJ9SSJnV1/GXfK3+tpULwZLiahUvH5PbGSOXBmXbGphFtdkA2jg8MtuHS6yBLbHkF4ItxwRavETp4tW34NDuQYawYJeO1iFPzRbSVlhBqfn/JIBX5510JJv0ITHVI6tkm2/xZKdJHPXiv0o2S+xsP8d6OutvIXuZnox0R25PdyuvHjAFaUwFzz8CmEAhgVUcp/C4GrJLU8bBgjPai/ RgIuSNFr bFVUNecsRrKmHkp4CDI/zeUPJgOQCnaCx87IGj+mEA8rmwsXeyU/eB8qPJDo0wQVkoy4SN7wZd5+yruhX1DkFpQsZ2yE6a6Vbe74s X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov 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. Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- 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 Wed Sep 13 17:14: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: 13383631 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 A948BEE01F1 for ; Wed, 13 Sep 2023 17:18:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4A1606B027C; Wed, 13 Sep 2023 13:18:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 451E76B027D; Wed, 13 Sep 2023 13:18:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 31A6E6B027E; Wed, 13 Sep 2023 13:18: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 1F44E6B027C for ; Wed, 13 Sep 2023 13:18:08 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E451B80454 for ; Wed, 13 Sep 2023 17:18:07 +0000 (UTC) X-FDA: 81232232214.04.C37CA87 Received: from out-230.mta1.migadu.com (out-230.mta1.migadu.com [95.215.58.230]) by imf02.hostedemail.com (Postfix) with ESMTP id 2EAF780029 for ; Wed, 13 Sep 2023 17:18:05 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Kykiqu4a; spf=pass (imf02.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.230 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=1694625486; 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=gGKJxtryqSg7zyzOR+J62w3w1hKrmZ/huaWffkkgHB8=; b=hHQ3AbbM/UtgRxHlWtvyPfxbwwdHs7FLjiTS2bWm4crnb2FHcNnqh4944EXK0Jesg6EDzt W0NxjXcELOXt2JaojiE6deCqzO0hJUHgpskH9eUu0ZfqjZa4Z6UF/rgKWUiCepR2m68/HV gmQa81o01X3gBvltQoBZQB9mpmGundI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694625486; a=rsa-sha256; cv=none; b=G7dcw70ao6PIHXAjFcATE+5nO63JoNwIAGg1D6mS+MAYRrJDvzDvToNiM5C7W0zUe/XNQ0 Q7av+c59tixf6chBUCgJC9HgBhpHdiBan51hUL9lQuyRG+DJ2iVy9NByfV0p5a69iKlCOa 777tpvD6O8V0hQanETYFZ4UQtE11es0= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Kykiqu4a; spf=pass (imf02.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.230 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=1694625484; 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=gGKJxtryqSg7zyzOR+J62w3w1hKrmZ/huaWffkkgHB8=; b=Kykiqu4aZApYf03YebHa3Dl/Iiz3dyOXmVXrrBHmpydAQBwIdJhL3HLDTzwCu05bpW7SeU 7532l7l5s0ccuWBVga6HibBP/hh1Jt3T/r7Xexqvuo0EKwyd5lqVv2v2D3IiN0BK5IbTfw g5lfY7rNPM1SE6bV/kcFjLK6Jf0r1ds= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 18/19] kasan: check object_size in kasan_complete_mode_report_info Date: Wed, 13 Sep 2023 19:14:43 +0200 Message-Id: <293d73bcd89932bc026263d3df8ee281ad3f621f.1694625260.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Stat-Signature: dm4ud9t1tj4nmgm1ztwe7e8u78akgzkf X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 2EAF780029 X-Rspam-User: X-HE-Tag: 1694625485-650634 X-HE-Meta: U2FsdGVkX19FY5ZzWrYYWSquRODlMXV9KOjfCjLGsqxoq8DXj+WfkgBx7u5pc2feYzfW2MuVZheXr7lozmTwvzibtIxOX3WQnsNSMoa5DTTsgLXUD/kcGl35/HKs9MSa2F5tEZp9rOj1t2cQeh9qgb7xVuX36CFXxba4d3+2jVhO9QUa7ggLV/EsQnLtkxCwSIglSC1yQ+ACzBab6/nTKz1FJtv2ZQ/CZiHY7UsjiNk3rWXiHCxqlTKhLpf7ez79DhDcxMp+1hNn2XVqcjf3/AmOhVTxOmFpA6tb4tTbbZIEBWb8yJZEiKahuQWAJEYL/C25Gx5O0RT0CNlLBT50WPhnboWfIvvJMH1EJGAi8w0B85Q1LrA4UG8cNa4sClSFvBGsJIs1l3ZwsCdolFrKfqcVF04pnkztbN9OOR2d7b3MmFHS+zuLlA5O6q9l6zaVVP+TWH0/PU6y8Po1Yl5nKV8eMuJ6YSXQ+U3PhkQLDVUA5hulW5e5aGYhq85lbEffLwwy5hpaakNOIc2FJmmCdx1wOBP4urjpyBTcsmXqCSmhA4SKZTWauixC09Ya9xElQ7NpOfZwqyIrNPCUq/46R+eB0aPBSX5eJlk+yLUzcwIl5uta2xg8tQj2bwOMR89VbwoLjsbSCvSgGuU/4V1W3yelDG4xfzr18cIy+OEU5dUnmKsizgXzZPDk5/X1t1IXQT9eQAfcyIVP+GB70LTxhXOqRlUJ2Yiw52P+aB7VYNg8Sa1SULPxrx8k3b7naKCRZ9nFQRYu+u6GR+SFGAvMXNg4ld47LnXsLqey7sQ4F9TvBRxQHB9tbfI3Is/orgHplQHZqcKduNqoSIB0KQ+bh6v8vpv41rEFuF304Es+MkEj6lCJCbZD9WPHeyK4ZsWHh9sOL8yhoFsNjMT6cI7Pvnt0Q9LcRGrE2AD0d6pNZ3BFUfT4hSJ5vN2uA229/FINXE9EcfPntXWxS/1Q0XR tiDq1zEE JGkCV+kBSNP8eu1xbTNNE5xLi1iCv2Js7gDfLI2VaV51SBKEKNe72qETU4jlCRUvltA+4K4sNhUCNGs3+RsxqhF6Ykyavt1XVun0L4SvautJ2vJFE8ghzaPZjqVuopBB63Psl2TD8hJDQLxvxRQnrj7/pBQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov 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. Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- Changes v1->v2: - This is a new patch. --- mm/kasan/report_tags.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c index 78abdcde5da9..98c238ba3545 100644 --- a/mm/kasan/report_tags.c +++ b/mm/kasan/report_tags.c @@ -58,7 +58,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 Wed Sep 13 17:14: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: 13383632 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 63B84EE01F0 for ; Wed, 13 Sep 2023 17:18:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 872B56B027D; Wed, 13 Sep 2023 13:18:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 822136B027E; Wed, 13 Sep 2023 13:18:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 710C96B027F; Wed, 13 Sep 2023 13:18:08 -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 588E66B027E for ; Wed, 13 Sep 2023 13:18:08 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 395EF140DCD for ; Wed, 13 Sep 2023 17:18:08 +0000 (UTC) X-FDA: 81232232256.03.4798D31 Received: from out-229.mta1.migadu.com (out-229.mta1.migadu.com [95.215.58.229]) by imf27.hostedemail.com (Postfix) with ESMTP id 7D29940028 for ; Wed, 13 Sep 2023 17:18:06 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=aOUyG6zN; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf27.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.229 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=1694625486; 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=qjH3MV6DYl1G1UYfCD5hIyhq0uUm2VbQwACKU2fMzJc=; b=jU9OAxv7M3c30Kjxzm1F7oFkJ/EBuUImT9tZlbOVvcTYTcJXYMwBO9iE/BeLdRbKI3psIs XlEe2gcpkdQhxcq4DbMmXZcRN+l7thJdwhc0WCP3Xc1lovsizhAOs90bmt2m9WQyernrrL ujYEeIgv2IkhbUKtZUexYy4xeTq23vY= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=aOUyG6zN; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf27.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.229 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694625486; a=rsa-sha256; cv=none; b=37L3rOZzmNGpFM4U5xocOy1YhJHEIBF0qb0lQXqpAy0LditVCTsPNyxlLTaiJLcnzhM2u4 dkh8qAXU93aC/xQDSJ+k0LqY95h3Rb1KnzU36ZHO4KwvlZ75w7WPP9XuT+gYojA/nZo9pj 7Pcotk7teA1yR7Z20WJCRsge5OCRUlo= 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=1694625485; 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=qjH3MV6DYl1G1UYfCD5hIyhq0uUm2VbQwACKU2fMzJc=; b=aOUyG6zNU4qOExhkFzpcOcmJrKwkx0ZsS/bVVWhskgw/aBPt6S1+VAaKP4oT7FsM6oyJVg +IUqSRli1eaRlhDIRzKFieirB2EVqYsJdtwVbWJ4TY/mTzb+JzNUxW7Lbi7K2Q5uY06kf1 yBI+2ElH2T2LDtZB7yOsWKeboKVByXg= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v2 19/19] kasan: use stack_depot_put for tag-based modes Date: Wed, 13 Sep 2023 19:14:44 +0200 Message-Id: <6e2367e7693aa107f05c649abe06180fff847bb4.1694625260.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 7D29940028 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 5co77ixguikxhxnirxpuqjsm1x178t95 X-HE-Tag: 1694625486-982971 X-HE-Meta: U2FsdGVkX1/1Iu0kuEP0DQMkx6ghmNoQeFYXjB66NY1UgNFHVkXd55/WHkvSelsPRlcxICSGpiMFk1OrZ7OHATmes7zq+lol8zxMvSHwogfehXHNi8AuuUUNJDojUkLwFTYgosOX+aRgOR3WzRMc4D2h+pg5uSZS5PSNEHopaoMmGkAh33ZhVswmuvSshjToxUA1/Lr0CkC0H7GqI0itLFK3s/YJTZ1EKNoAkmyr+dj2su+otw1SArEY5BCuG1RZGcYgnU6cgnMpQjkEld5GFE5OdDrETIWG5Af4slujJa3PDoEevkxdmdvIhZiZM2Q9BSj2WGMfluMmCYtZzAGrkaCLUaykvSnsYtOoaspYo4DKiEh177bO9DvHJqB1DMUsZvCBWymT73blJFDzsv1HL6cq+tEQ9bhPZQacHOLGv5qGwUJn3cdiQbpjN6AXUzLxs924Rpc7jkYV+HnQ3ukm7rBM7LX7TW/ko53KVHsR3U49j86sDyzRaDp5YHhiejB+qtHvRLC8QHweFw2lLxBnFUwifWrYPCLWxSb/OqHGd/NP8RfSH2VK4GnQRHoO57bCB6FopDMyij5I1hFaEcJY3N4a/m9cZNR76w2EcZ3oMx/zVrI6iNTVUomfABmxhL3w7ewVMlYalEr+kOz3TnicUaAQ/iwE1bJOUbbvmCDJgFsgHvzqomzSdT5I6S+t0/JNMYgj4BKz/PZsiy8KQE4E8+6wPmucnFvirj2bHO5+0aikFSg8Y6fjPW6GAEgvHoE2yie4dNvNMOQoE3ZAARTR9RyEUPJdpL7KCHEW+c3+2O+T10gYFQcruqd59FJ4qBmwb27gJIVI1vwjtmy8eqBtTwwz7qbsRmg081fFn5JwBBuYIo1yejxYJ0OjWq1FLgnvSOL+6OaAVEQgUNKmEwWg2mAfC8FMZ8jcoVA/rkhMmfvX1YjN8H2iOQwlWaYTepk+5FgbzW5XJgRVk+aS1jI E7c2Cdka LVsbcpRFZqMh3DARiqyt7UOc6SSSjSUFZLcsxQM8CF7Kj0otbgJRu439ovnuGK9yWp4yjIyblyyb5zXVsPsmDuYVhlrT9UlpHsA3qtEdCSRSmbNIvW4XWxT8ZN+jvtkMapMz+ X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: From: Andrey Konovalov Make tag-based KASAN modes to 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. Signed-off-by: Andrey Konovalov Reviewed-by: Alexander Potapenko --- Changes v1->v2: - Adapt to the stack depot API change. - Drop READ_ONCE when reading entry->stack. --- mm/kasan/report_tags.c | 1 + mm/kasan/tags.c | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c index 98c238ba3545..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; 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)