From patchwork Mon Nov 20 17:46:59 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: 13461816 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 D2D74C5AE5B for ; Mon, 20 Nov 2023 17:47:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 47AE36B032A; Mon, 20 Nov 2023 12:47:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3D7756B032C; Mon, 20 Nov 2023 12:47:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2788A6B032F; Mon, 20 Nov 2023 12:47:32 -0500 (EST) 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 EBFB66B032C for ; Mon, 20 Nov 2023 12:47:31 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id C719CA095C for ; Mon, 20 Nov 2023 17:47:31 +0000 (UTC) X-FDA: 81479064702.08.BCBF2B1 Received: from out-183.mta1.migadu.com (out-183.mta1.migadu.com [95.215.58.183]) by imf06.hostedemail.com (Postfix) with ESMTP id E6787180004 for ; Mon, 20 Nov 2023 17:47:28 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="bR/I30d5"; spf=pass (imf06.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.183 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=1700502449; a=rsa-sha256; cv=none; b=3cgpInNiOA9IbQH5+fOzVyjMb6OOCHz9c7N2wq7Zq0GyEiZjnwusHu5UkEg+MqOQC6mlUl bvxd/SoNaI+se+XRsiZkhTDZyL5BGyFdtwq+7TY0ZG7UWWbiwfHKVgXe6cdyxSMLCOQgYg Y5zmE0W3O43SiynKQBSLiwyyWcUCcy8= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="bR/I30d5"; spf=pass (imf06.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.183 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=1700502449; 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=9mwqfN/pqk9HRQ931HoPPP333OlumxbY++YPCTeY76U=; b=foD5a90xRewX63QKuNdG4kg87Fu7Kn3FQd+H6EsVa7EwUOaNJSLzFunRuRlVd5xZqRZEwB GzAOkWDY6MO4PsbZG7pwEpbd8KasM2KEeIVdi9x2g3LLdLZowjoMRrpPBoOOxOf2IuDo4B END6yzWjgknIXGVOQibpkAyU03vnVFg= 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=1700502447; 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=9mwqfN/pqk9HRQ931HoPPP333OlumxbY++YPCTeY76U=; b=bR/I30d5ng/lYBtNQqgdoGfH5paO2CtcP21wojJGbtHFG33q9OSF6VODY4kRaiORiokLQo iZpd9D4Xa29yBgsAA1WjhOAcCg0lSOCGSH3njrApHXcLDY08VxXeaX0yqJ6yzjAbR1DXy7 0Hwm3iz+UTEkMpPA3RFCPkvRL0wjaJM= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 01/22] lib/stackdepot: print disabled message only if truly disabled Date: Mon, 20 Nov 2023 18:46:59 +0100 Message-Id: <73a25c5fff29f3357cd7a9330e85e09bc8da2cbe.1700502145.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: E6787180004 X-Stat-Signature: h1aedyny1n6p6jo4ydtehpdz81r5p6tw X-Rspam-User: X-HE-Tag: 1700502448-914579 X-HE-Meta: U2FsdGVkX19OUjbljdsGylBMZko/x2yHFMmY2JnhlcNTuS8fU2IxM8XHti2dwOL1mrTUEMWVgO/1/QZLTyMzVbcXxYAmcReGYYTmH592RAXs0QVj7XMW1trhtWmnnZjRgMA3/JAEhYD1rIxvqrB52zt6ILatcnmwWGgs+IsF+LItCkkPoX4IAo/kz5dvAhen2NxN/CtGvRxLKMiUM8prrI+O9oHXD7AE4P9g1nvi/iUaLa7OgyqU7xenoAPS+ECba6L08zD8OVlGkVMmY7BHo+QC3q6+QSq0/4xvko063aqxlDOmDiI/4ZnfrK/tzxtsHobie3SHFDRCS/viQ2UVwhmEIv2sAndLUAnBBwWs9ZgMP56znk3d32+P1s5GLaBPpTy6K8lKKLVpWGjKmXTqv2I4Q2RdGFp4Lv12rAt4Xj4BYtcMwydwpxXP/4lDKRDauvVTbGKuV7Jaciz8M6Bc78Oi0juxtBxeRrG5eC+6J0Q8Nwen0iOcseohnV6s7JrLSWeI8G4DPLsrEEQ8aXhmAnjkbrryfrVNu1tynhasAdeKqBlx252tCf5SARJBRpUry31uiNes5VhF2b8zdi2O4nwDAr316ZJq+hzUN/nv7xCXvLGPrFSJNJrKFsAdbKDcyGNcKDnVNmJEf8ghGCnhw4vg6PIUvrFmx2GRde62QJeELafbV2ald3oigG+a4Kz9IKpBQQze5a4yzc0no4WzQanYXRUZaQgtfyBKMps+tgNMd9Ne/uBDkxSXC3b5nvn+9z5bASJOsZ847YI6zjme6o9tPeyrwy4LWKw49uX+yC0TUFAMGlhE8ZyxYk4XNDBvnWasHiqz14SWzxKP3TgVnbgPg9Y7aAZznOn5gOsXab629SDvxDFPps01gthm5QA/q4Zj97OMID850Y3LX+9NeHTkXgtNTRgFzJJQvWdz9BfmmxTjEd9/FUbOGmYND4k9YBTErNJJyskea8iIUKR rbj4BzZD db4TJ7TJrh1r5Zx2taJskX3JJmS7WGqbuO8AXX/SXa+evmOmcXq+5+4/SQI4QF6kukj4C X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Currently, if stack_depot_disable=off is passed to the kernel command-line after stack_depot_disable=on, stack depot prints a message that it is disabled, while it is actually enabled. Fix this by moving printing the disabled message to stack_depot_early_init. Place it before the __stack_depot_early_init_requested check, so that the message is printed even if early stack depot init has not been requested. Also drop the stack_table = NULL assignment from disable_stack_depot, as stack_table is NULL by default. Fixes: e1fdc403349c ("lib: stackdepot: add support to disable stack depot") Reviewed-by: Marco Elver Signed-off-by: Andrey Konovalov Reviewed-by: Oscar Salvador --- lib/stackdepot.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 2f5aa851834e..0eeaef4f2523 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -101,14 +101,7 @@ static int next_pool_required = 1; static int __init disable_stack_depot(char *str) { - int ret; - - ret = kstrtobool(str, &stack_depot_disabled); - if (!ret && stack_depot_disabled) { - pr_info("disabled\n"); - stack_table = NULL; - } - return 0; + return kstrtobool(str, &stack_depot_disabled); } early_param("stack_depot_disable", disable_stack_depot); @@ -130,6 +123,15 @@ int __init stack_depot_early_init(void) return 0; __stack_depot_early_init_passed = true; + /* + * Print disabled message even if early init has not been requested: + * stack_depot_init() will not print one. + */ + if (stack_depot_disabled) { + pr_info("disabled\n"); + return 0; + } + /* * If KASAN is enabled, use the maximum order: KASAN is frequently used * in fuzzing scenarios, which leads to a large number of different @@ -138,7 +140,11 @@ int __init stack_depot_early_init(void) if (kasan_enabled() && !stack_bucket_number_order) stack_bucket_number_order = STACK_BUCKET_NUMBER_ORDER_MAX; - if (!__stack_depot_early_init_requested || stack_depot_disabled) + /* + * Check if early init has been requested after setting + * stack_bucket_number_order: stack_depot_init() uses its value. + */ + if (!__stack_depot_early_init_requested) return 0; /* From patchwork Mon Nov 20 17:47:00 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: 13461815 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 AF7CBC5AD4C for ; Mon, 20 Nov 2023 17:47:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CFFA36B0329; Mon, 20 Nov 2023 12:47:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id CB03B6B032A; Mon, 20 Nov 2023 12:47:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B77A96B032C; Mon, 20 Nov 2023 12:47:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id A990E6B0329 for ; Mon, 20 Nov 2023 12:47:31 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 70E2C1201BE for ; Mon, 20 Nov 2023 17:47:31 +0000 (UTC) X-FDA: 81479064702.10.E781F50 Received: from out-186.mta1.migadu.com (out-186.mta1.migadu.com [95.215.58.186]) by imf20.hostedemail.com (Postfix) with ESMTP id 9678F1C0019 for ; Mon, 20 Nov 2023 17:47:29 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=qyhdPL9y; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf20.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.186 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=1700502449; 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=4GD92F9WT2lJ15sdFM57D7OnzHXCVz8Opyiuivl9/aY=; b=2jG4aj6TokHUpx1ZBUKt51vmWCcF6lE7wt+Az37qIE+IpRjKwUChVMpLfInzewTb2golF7 0eo9qe5frlJfTfiFUdsHALoknOVEZ5AYu5R9OFR70HRcvKRQB7FBCnpuWubw2HWyEeHTLz VJ3EfaxMvvnRuJtLglXc6isTHnpnD0s= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=qyhdPL9y; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf20.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.186 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502449; a=rsa-sha256; cv=none; b=kT8b0fdASYsNdXa/FzMQoa4wu8tjiWEfl6DPAyKTeL5T+3tArr2ovtFWCcZWHMb2T3VeNi zztFNowUavW7NiV65SNIewSP9WyS91aQNNNLvpVXjjMc1+8n0fE+fZ7IFo9+pguATxd5KB hdtsC16VBu4ljHk0aUUGztA2fHqwuLU= 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=1700502447; 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=4GD92F9WT2lJ15sdFM57D7OnzHXCVz8Opyiuivl9/aY=; b=qyhdPL9yHGZwgbX97BEztXeCz1Zt+rIj3RmbbGcjPTQsTwh98G4ATjxhFfnwka3CKqD1b6 x9kwKDWmny/BO2cvVbgqEwg2/wpgt3BzZx3yBf1KxUz7o5wnkBkYXhX4a/sfMby5QWyf2c 4HelafeGlMBXbFW+ItTdiGsKvqfzEZg= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 02/22] lib/stackdepot: check disabled flag when fetching Date: Mon, 20 Nov 2023 18:47:00 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 9678F1C0019 X-Stat-Signature: jbippse98e568qyj5qoiejjrarmu9sxu X-HE-Tag: 1700502449-488136 X-HE-Meta: U2FsdGVkX195tQo8ydXNci+n9yr/Zu8jOVX4lrsEUkX401owwyQco5op8cCCMuy4AymmBWT130XplzVw068qone43f+NP17V+c8/G8eStxf4WM+izngoWuMn1JbQRX8iKKScpeIIOrJOs/idWa2Hp2NBfiBA+YiAQ13VyO3umNrwZY8gugSkWVFurog0GetBQOvY4ntPqKXEIxrKHY4cHekcadA+wu2/vjuynKWchYEenJ4iioVfjIaIQ/3+0bjkYtceFm+dZXCi49Dqkr79LPMAdo7z60qd5RFaRZ8gS7D85Pxb1OghsV6yYL6VqcdSmHqhSVq2ND/30pWmWCaWqsYku/dyWwpo/1jbmo7WLusxk2KKMxLmx3Wmc2sQVIHFwNwAwpbS9DUORQLi1Q3aN2Clbar3O1ErjggxnxYZkzbj+2ZdQTHyXdApiRV4BzjpGRksJAKQJnLUilYzVUSwMSK1IrWSHqwqS0HX4ssMy0TaEkmERpyCfnPGkKzOxikr3dDb/1e6R/wjRTdxUZZ6NQrQ/Ow01l/lNAvnq/F+dzCBWiNDHJ291QIYOTKQ2SQOOWf3vmZMWK8vHMExFrRuTc7f41u7gjRcHJ+ERO1bwe7cE6ItzsCuB2rwcItJlh+Bs0LPGYvsHXO2wzUeuDCeNuA5djA1p/z6kPxisij5Z1RWVIqTnY//Z4Cl54IY21rmO9mEy3xO2/8OfWj53INpdBzG7NMUr3JLKMDIj8OsyeA6x0ugMTHDskzV1N9X9Os694IRNVAw1Xp7e/sQ5r5u4nX5W0ENNTHVQ1A53sm2jtADUQCDXt+ja8Af1ojN2P6O6JL9v3iNqN5B7qLobi7CYFxdFMcDnT7EJyxb2+T6BoXyyxFlrXCU7HH4JJFmNN4ut/Ee3lhkV1x4HYQ2j3p6tFyZIT54VleznaM+YRcC8AF3UNuICiPKFsCZaSVBU875Ury+Qhn0f+uHjwPTYat ZCAaXVi8 wGwFDSA/yPj4GmRQS6i63/KlXvFNtrNSRJTIxetlEZ4Mi8z5X6xQ4vyWD3bxTfUKql4Ke X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Do not try fetching a stack trace from the stack depot if the stack_depot_disabled flag is enabled. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov Reviewed-by: Oscar Salvador --- lib/stackdepot.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 0eeaef4f2523..f8a8033e1dc8 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -483,7 +483,7 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, */ kmsan_unpoison_memory(entries, sizeof(*entries)); - if (!handle) + if (!handle || stack_depot_disabled) return 0; if (parts.pool_index > pool_index_cached) { From patchwork Mon Nov 20 17:47:01 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: 13461817 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 06D40C5AD4C for ; Mon, 20 Nov 2023 17:47:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8BB9A6B032F; Mon, 20 Nov 2023 12:47:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 86AAF6B032C; Mon, 20 Nov 2023 12:47:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5ACB76B033A; Mon, 20 Nov 2023 12:47:32 -0500 (EST) 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 3ECA26B032F for ; Mon, 20 Nov 2023 12:47:32 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 08F341209D1 for ; Mon, 20 Nov 2023 17:47:32 +0000 (UTC) X-FDA: 81479064744.03.4F80B46 Received: from out-189.mta1.migadu.com (out-189.mta1.migadu.com [95.215.58.189]) by imf16.hostedemail.com (Postfix) with ESMTP id 464F218001C for ; Mon, 20 Nov 2023 17:47:30 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=DpwTRKlG; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf16.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.189 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=1700502450; 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=VQKv8ipVyqmLqtwmF3tWarfk+ElO5xq6wgwn4qj22hY=; b=KpXrewssA0A30txq2+0LZMxaNBNRgcw7lha2WakUfQ5Hvx6ZYo08DGuIsk6TloJhqYlhXy Fbef/voGLe9W3OtCM8f3ESy4d2BEIFdf4ztbMxXHCubConZqLGfFKmNRIbqi+HU96r8S/k U6S9MoFRxsvg7fTk7+PuKrE3RHoMwCY= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=DpwTRKlG; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf16.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.189 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502450; a=rsa-sha256; cv=none; b=u/Y4bAiB7x0c7UhNieRZbd1jE4amrFJFCBpNoa2u055+zsO9JNPz2mJ8mCSyEGGLVGDWTj KY4ho1kteTYvdOHU0bE++muACH7H41JoRxDT1glgcQ4KkO7P7bIS7RujRIRISBQxiEgpcA FN+7bcKaVk5eAr4eAMOWss6x6BC/khc= 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=1700502448; 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=VQKv8ipVyqmLqtwmF3tWarfk+ElO5xq6wgwn4qj22hY=; b=DpwTRKlGqKrBB4M8buYXFUbh5C4t1DeCOJCZmUpqezNSRGsyD+Wdylo613HdhfnrTQ5s+e pjLcUIFVsXiRTj7skGIcX0r4YxbEBcZF9ntx+1uASDYWt1MOFzRn7mamvzav2NVLFYw6Tg zTiWDgpYpFtnG/OSWJ9T8vQPvfnqvc0= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 03/22] lib/stackdepot: simplify __stack_depot_save Date: Mon, 20 Nov 2023 18:47:01 +0100 Message-Id: <3b0763c8057a1cf2f200ff250a5f9580ee36a28c.1700502145.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 464F218001C X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 8dmhibswgcywnrk1pox99r83rcgfeoi8 X-HE-Tag: 1700502450-163337 X-HE-Meta: U2FsdGVkX1/rDK61TF+QpvRU6sHx0RPsPuJI8wTeeDk5fGQcgFqk1IG8yeva++/kiCzmitGogeU+UxLm7YV2ka3vkQY0LNI1lhjFpXRhgRBBV3RKxGq/L/4flHNlhlw8L0m433mBx8XPkphDmrj5lW8Cuszqfi7ZVwPi9JlXQjxLIHrb8BJH4P7fWNym7tT1tw7Su20c47M8GEot63xTyFTkiKeig6KZROKhIPB0bNpEm3zNnUX62ESExL9q+bpjXh7Cr7AIh+q8Di9MCnqgbBir25iwwsZy/xsmA71Qk5OT41teQQ6CllzeEVheaFIcI7ggsgnWCN8j3pjrz3ijsoH9aUL9/NtzSGdJUux4IoqH/JBO4ZMBTUwHGtT2+V1WoBDAIWT/83wk8vlSLQe8ShoqcUsp7sOQrd4mIZQ/k4QtkOnkVa4Omhgfi3w6sBLq5zqQZlT2QnnSt/imtlTEztHar2ByIqm0x4nHG9Nc9iftkhQps6/6lk3YNr9eKmzY47Z6bnr4FrW6inTImHgnGLEUroVrS2Sg4AQQgVrgjLZ2yLZqxNcZ2iHq4gsLyAHs29P8yD+Fxe6nX+pVb2LqlwyZ9oIFsdwl2ZK2gWDCYXtDI4Q0wSfLhS1VQ+/e4uq0hwW6QhFZfyzMbNzwanhh6QcHdhP9EkQirvg0gjF//F0ixFy31cOx50nRLTh1SEepPhaAIFAX/ObsrY4k2aHhvq66VGqeAanIyQ0pJ5NBuuIrqWHDqqx9Wc2jdPNb8O1FwT3/FZSORM5rlfIFs704hEdM6tdLI0xyKUgUahnlRS8CuqkqqwFiz4WCi2Eut3kekwl37eRufYFphC0TVIG/kTn3y5LYPMmiBPwUjBM750epGzHmbfELwFccJJWxXTSWhggqtveH6D40tNNUsZRzdkPWd7SWXjZ8CjbMFkE6z6ju0Q6kr4jJNdmjVVXWa0z8xUipO+y7rM7ixO15Ps4 GL7uwB/w xZHtJW3Jk9LS6WESbniHG1lMGyArdNBNoBRSKUBH2c4wuUAKrhEJNqvdDWL+fWB/RrYGfIBOVla1x+8TVilRH1kJ0GjdD+o4yde5o/xwXkjE4FqA= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov The retval local variable in __stack_depot_save has the union type handle_parts, but the function never uses anything but the union's handle field. Define retval simply as depot_stack_handle_t to simplify the code. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov Reviewed-by: Oscar Salvador --- lib/stackdepot.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index f8a8033e1dc8..3e71c8f61c7d 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -366,7 +366,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; @@ -383,7 +383,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]; @@ -449,9 +449,8 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, free_pages((unsigned long)prealloc, DEPOT_POOL_ORDER); } if (found) - retval.handle = found->handle.handle; -fast_exit: - return retval.handle; + handle = found->handle.handle; + return handle; } EXPORT_SYMBOL_GPL(__stack_depot_save); From patchwork Mon Nov 20 17:47:02 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: 13461818 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 DEAD9C2BB3F for ; Mon, 20 Nov 2023 17:47:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B029C6B032C; Mon, 20 Nov 2023 12:47:32 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A66AC6B033B; Mon, 20 Nov 2023 12:47:32 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7F74A6B0339; Mon, 20 Nov 2023 12:47:32 -0500 (EST) 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 5F07B6B032F for ; Mon, 20 Nov 2023 12:47:32 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 389661209E1 for ; Mon, 20 Nov 2023 17:47:32 +0000 (UTC) X-FDA: 81479064744.28.E31E206 Received: from out-174.mta1.migadu.com (out-174.mta1.migadu.com [95.215.58.174]) by imf16.hostedemail.com (Postfix) with ESMTP id 6B3F1180020 for ; Mon, 20 Nov 2023 17:47:30 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=sig9IVvl; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf16.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.174 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700502450; 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=lmg5zTuKt1w9oWZyNpXv7HY69NJCp78Dr+tP0crczmk=; b=KfQ1Bl12/FrBivcutXBpDN0BZCF2PCMWbcixuSOcERxIdUEl0RpnbBK/HLxUYiKspoAxtW hNck9cjdXoaxYydUemlig4mOAcDis3CVMTfdwSjHg827MvOTeQquwHf+aSFyzwe//ZuFYO 0uiqUzEp5Kw9Vi32Jv9gkp5QbVWO65U= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=sig9IVvl; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf16.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.174 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502450; a=rsa-sha256; cv=none; b=qTWHXAfN2nF5GQUpEm6TPArIZDXNTbf9my1M/HagltoB8zCtE2ZM0bvhnxENFGpNrdiVfy GIPxnR+pAmf13cTa3zE6cZjrLzF37nc78nBKzyn0PLuBqgAnM4EJsAvTUZThE+RpQ8NPKO lqplfgDR6wGr+CihLNdPbLkTOz+yPOc= 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=1700502449; 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=lmg5zTuKt1w9oWZyNpXv7HY69NJCp78Dr+tP0crczmk=; b=sig9IVvlHqVaQ7yAjHcKyEBZSXKZVvVyW0FFm1WUvwt3/VhlR8ohBHYAlXrR7cabYtXtPi 1x7T5BIS3RF5oH+C0wW4PoHF0fJj3mAq0pXEuDuyXI6LIJomNt8wyDqJivUeLox2pQT6re fzBBmv3FWure1h0tN/rWvxM6gmS3lDg= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 04/22] lib/stackdepot: drop valid bit from handles Date: Mon, 20 Nov 2023 18:47:02 +0100 Message-Id: <34969bba2ca6e012c6ad071767197dee64dc5723.1700502145.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: 6B3F1180020 X-Stat-Signature: 5pnik98hhmdhcd9e18oxhkmcnrnso57c X-HE-Tag: 1700502450-384577 X-HE-Meta: U2FsdGVkX1+Ip2HwUo68P3RnXrU0peXJ4brwloyC12zNTOcqaJGRFxZQVoooW0pTdyoQFHwp9/IDK51yHjFJdX5aQ+xleiLVaSGiQtmqfpveYfiLxCTL+R2rkr6okkLSB+vd6qgYHA7HsGc3jnidop3LUQ118Wq8XoA+XrVUu89nbvaJbJgA/Je5WLoI/qBUUQhIIAFdHa5tbY1M911wVUA0Z38WdCyuNBbHDimTZqf/B6x72LfQiXpt38hBv1xAsMzwKjExdUmo2RRrJapv6VkEUqhPPC1w+RsZdneGNwj2n52NWzt5lsIa4PZ9egJ2SliM8vKFpK9NsXXwIfc89PAgLipXp5egvNvHH85AgTTphN7GjgsgyLyKro9Vu/aru0H2v6/017e5qHZnGsDRsJun+m3qcNlucVp8H3N2R79iM3uhx73N1wrsFZg3pAVqq9OgH59BWdaB9LAI0wtr+Q9WdBZEoKJuHnysbEuEox1E9RUymMw9F7fvT/abszQ5A2Y5x95vcZSIaXfs+wlIybuz3qQiMLs5xJdWORajIQb8bemMo+0Rbfb9I7/iP/CGNtAAPCsWtr5tcZq767h5Oi3p9p1xIcTOBXQgPQYln5iEUOMa9oJzSaZxQpy4wS2wbVYbO8aPKH0g326C8LIKdayhBGNsCmt5gXrFB8ZgJb/ntpapuU+zoHS4s/+lb8CuAJ+o6tCZ7nb/9q2WKKqS+zMMuLcUxVi+EC6ODFMo9/hQOH+cZpDZrUEij6gXAsVzr2v3SdEiZB2wJ6FGEFiaL1VH8iFdg1MmMqqYFxf2NV9pZV6/vhYdC+f2WXbrCNEd47ciGapjPB1Xmi33233DsGdI0yUFZUd57chWDpjRfaIca9Ngk8jm7Y95+zYXWiHXlYHYbLQGEaJzD6SEoeBr3Dn/+dsSoHHclqj5b4/W0kE7zt6t3W5AY/ohgdETUV2ORpqm3zsfgqz5qYCXTf/ x35yPbta rCF8MASo9ycRxNsIISb0Dtbcgxu3xXV8o5NOOKpm1SMNo3Yu6Cw4hPwmxS7W24dLinP49 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Stack depot doesn't use the valid bit in handles in any way, so drop it. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov Reviewed-by: Oscar Salvador --- lib/stackdepot.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 3e71c8f61c7d..46a422d31c1f 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; }; }; @@ -309,7 +307,6 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) stack->size = size; stack->handle.pool_index = pool_index; stack->handle.offset = pool_offset >> DEPOT_STACK_ALIGN; - stack->handle.valid = 1; stack->handle.extra = 0; memcpy(stack->entries, entries, flex_array_size(stack, entries, size)); pool_offset += required_size; From patchwork Mon Nov 20 17:47:03 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: 13461819 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 DCEC0C5AE5B for ; Mon, 20 Nov 2023 17:47:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2C6BA6B0339; Mon, 20 Nov 2023 12:47:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2770D6B033A; Mon, 20 Nov 2023 12:47:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1185B6B033B; Mon, 20 Nov 2023 12:47:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id F04756B0339 for ; Mon, 20 Nov 2023 12:47:32 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id C31BD140502 for ; Mon, 20 Nov 2023 17:47:32 +0000 (UTC) X-FDA: 81479064744.22.76CC157 Received: from out-187.mta1.migadu.com (out-187.mta1.migadu.com [95.215.58.187]) by imf17.hostedemail.com (Postfix) with ESMTP id 1389240005 for ; Mon, 20 Nov 2023 17:47:30 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Ql85St5S; spf=pass (imf17.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.187 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=1700502451; a=rsa-sha256; cv=none; b=H9z4PLra4e5RxNMvNRcNGqj+v8cLZXBtIVMlYcf2aKAZKN+NxwsRNN202lYWYy4wuUFNqN 34lKwvgZzT/wSxMRoCAEtYF+skb3QN7CzVQ8V3yiTnrdQwvMr7k3IiWBts/AjY6pP3Iozh RSH2Gg2DFRLxGHSLqteQrThvvppIr88= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Ql85St5S; spf=pass (imf17.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.187 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=1700502451; 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=4+Y9MGkz9f3yZSUENZCgBAvLs1CfO+kuipQPdiyYS+c=; b=OI5x1RNFVJHziEh8k2wU619pV749hhhCXEsP7HI/mFz5r04RTFEzK6LwZK/8c08+l1IPvP SJ0+aB4LE+13RQHuleyT6hleTbhsq8Y0gxvjHQmdAUyg/O+IK4oSqd2SIguCg1Q11Ws+20 zac3Vy4ypLlmOjg41lTWHm9nAQMWnL8= 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=1700502449; 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=4+Y9MGkz9f3yZSUENZCgBAvLs1CfO+kuipQPdiyYS+c=; b=Ql85St5SjYl9dK6UKQo/wZBfC05xwZ8kz2f2txy485DwN0EC3gx7bgEAGlRsR3amTQZM6i Nep6MT9W4kXFWBOfaYnIaeXGDBapToM5EgH6SXSUwG03GDKgl7MtqpodPgiSEzEwBXoD0L 1m7pwV0+V5c6VKCswrtvLTavWAO/Q/M= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 05/22] lib/stackdepot: add depot_fetch_stack helper Date: Mon, 20 Nov 2023 18:47:03 +0100 Message-Id: <170d8c202f29dc8e3d5491ee074d1e9e029a46db.1700502145.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 1389240005 X-Stat-Signature: u7idgwoihgnyybs4kbjriweys5dt9mnu X-Rspam-User: X-HE-Tag: 1700502450-231028 X-HE-Meta: U2FsdGVkX19Cb8lWPMlDRbf+hLNxOyIBvbrh/j6lS3o8Y1EJ7w3oLDAtMCx0zffRSAvvOCufep9irZ1WWjDjKmel+I/OTvIeyjLEb8Affr00rr5Mac1dey202SYw1DkjEZSTG/JxSQWyRpHQWRM1q7nur1mupua18kHArmqGaACpejlLY89gh5/LPCAJm6AfP87Qex4AVNicaSEqaYze48oC5AxOcZ436AKOnV1BXWVPsyecgW+PUO7Io2Te/WViUZQYquuP8Itl7jBGy9au1jKnTU9I1Zt0YjHt/TbyPbZ21iQs0xFvUXhvW8BuJSON7ioik1a+tlK8sqtAp3kbAkSoH79NEda8xZnFoBxndD0+xODDXSg6f8Pqms6abFh0GgJnybOXaVR8W2szfEl9jJBgHso8G2ZOmFK3PtdEAmCQR9Joaee7SRTgs6BtJ6rFVaTtliunXBcZj/Z3ZZIjNdmYslOb2JUrLB6CR1nb857CWWTWjndwnuwjX1YHxlZjKm0zcZQjdSFrWLv4yuWav/pN8uXyyCXqGQ0ldLN9/aui48pmppu7I5A9cxgPpnW4lMejOJHKgEEJXHQuLgZ9Sm2kgvUX6hjxCzSlr0MGLPpQHwBE1Nt/UEF+4rP/pecRkbFpsdXWV88CkgUgrtkqAIkNKkil6Ywew/cYkoothqIlftty3P/o/GbaCUSh9itV6J64G5yZdZELv4quR+FYj1+PXs/cmA0b1i/Ioo1oKxsBEdw55bHKpxhC0XtqVcnafOhEJf68VjqvqGEbhVj9hM3UTaIQJB8enc0y9dPtVeG3Zz9VPEew7piMg4dMK+T+/5INGuexehc+nJEYao2EgD/7MofUlhzDRvyKEaA9bWL5Aes7CfS/Crn43wlVNw2TwiBuHhYAhn0bbMmvKceGU/gIEtAsjK5uG9ptkNbbxk+IzDJ/PNaNRWV0LjZnq6ACIk8neyc27wyN5FwOStl 9a54+Eb3 5H6YaXZ8c5g20bVb71obQdN4g1m4tL/q84KjCu5nm7LbGsTJc5sbUNTY1N9I5YA+F8lovImEfJx9eQ5Y5/I8kKXlShbAnh1bpV4vT8mdT+Sm42Gs= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Add a helper depot_fetch_stack function that fetches the pointer to a stack record. With this change, all static depot_* functions now operate on stack pools and the exported stack_depot_* functions operate on the hash table. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov Reviewed-by: Oscar Salvador --- 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 46a422d31c1f..e41713983cac 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -310,6 +310,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. @@ -319,6 +320,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) { @@ -462,14 +489,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; @@ -482,15 +501,7 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, if (!handle || stack_depot_disabled) return 0; - if (parts.pool_index > pool_index_cached) { - WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n", - parts.pool_index, pool_index_cached, handle); - return 0; - } - pool = stack_pools[parts.pool_index]; - if (!pool) - return 0; - stack = pool + offset; + stack = depot_fetch_stack(handle); *entries = stack->entries; return stack->size; From patchwork Mon Nov 20 17:47:04 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: 13461820 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 985BAC2BB3F for ; Mon, 20 Nov 2023 17:48:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 316446B034D; Mon, 20 Nov 2023 12:48:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 29EF86B034B; Mon, 20 Nov 2023 12:48:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 140D06B034F; Mon, 20 Nov 2023 12:48:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E941D6B034B for ; Mon, 20 Nov 2023 12:48:36 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id ADAB5C08DE for ; Mon, 20 Nov 2023 17:48:36 +0000 (UTC) X-FDA: 81479067432.25.2A1F014 Received: from out-180.mta0.migadu.com (out-180.mta0.migadu.com [91.218.175.180]) by imf11.hostedemail.com (Postfix) with ESMTP id E3F2540007 for ; Mon, 20 Nov 2023 17:48:34 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=v29OoaCr; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf11.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.180 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=1700502515; 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=pu1XFZGC+3mxYm/tNoBY6t9qKRkAeXfQVUMTHQNQJK8=; b=REHwa+2WNLymfnv+JB0aUl7pYSuwMtrDRAsPBLyKc3LOuukXoaZDvh2osfTzCRa5lI3sxw bkHJhU+4PWNecAr5z4UxT18gzr8AM3LCXJemIqVBxAtYmYAfnS/nqj44ZdVXsGcAV47YoB y5yEeLJobl8rWjuo+L33mu6gacQkCMs= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=v29OoaCr; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf11.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.180 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502515; a=rsa-sha256; cv=none; b=tOIsibUgWeAgxKL4h9GbzrWuUTQyBF6RwYK2f9RoS19ksiVzSF3Hyb3BeS1oKbPTrPWIE4 qu6M0ox79YSGL9Ky6+5RoVDC+SUuwE5vDX9Y3LOukoEBJk7w2Zx/f14ODzisTFV5bH7Qbm +y5d4MmbWK77QAp6qIMKmDmLmrTYpco= 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=1700502512; 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=pu1XFZGC+3mxYm/tNoBY6t9qKRkAeXfQVUMTHQNQJK8=; b=v29OoaCrQAFVYXx8qE1accX78dCRXzOu4rgaHGTPoImCDsSRXsXJW7zty4lVMppXjkqcg3 cICBXUXycfKNBxolMNR9vSSUR3Qdi4ReyKaR9Gp00MSY7TEAkMubulEyKPVq2YOWSy/vMK Csf5CwyODxYj8dOCpaCpXMz38YUJonc= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 06/22] lib/stackdepot: use fixed-sized slots for stack records Date: Mon, 20 Nov 2023 18:47:04 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Stat-Signature: nr9oda4ksmm87o3wz8njot6acit3zda9 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: E3F2540007 X-HE-Tag: 1700502514-775065 X-HE-Meta: U2FsdGVkX18EzvQc/qmWhfM9YdRfiL6x80H/FwkQ6i/kduEjDOWYkcnfLxQwJA7xlF9v607VI25KSxhkTz+mhaIBiZ8eW6+sarwODf8SSBpoaPjh6FwE86yq7iZxmCxkxQzKILHZJ+Nbt8tQ9KVnJQu/Z20ixdGmxyAN5WE6KJlULE1EtbxKn0pYJGTrwabP02CiIrYDcbgDjnukSwfCgKuTRqRXRH1eWGLyNLCTVKg1+kHmKwwHlv2pci3Fqw/UkKuLgwUyhpzQNUduOBFYVsb5nSofNo9/JnaV4yvDxF2umCrf6DfPK1WGzHbCYqhUJJOPpQZpr0PSawlvI4qk0IXTH/4UCfXMbnPxLDbfjAQXfoML9QEWKZiAzV7nAn0arKtqy1XgI3jfMjC2EwschRk2EmVLuTRKmgAZ0IWP+icQaX/v3OTT6jHbbY82qC3SQ3TjuxAtKVlTle7Ct5X1kz3LooEqCUunN5VONvn7tedPM4Fs7CJIg5rEipC76PIIGAvOBVhQpvUwu3wn1i5W4tl3FFmDY9A9QvDUFfcP4TOHCHmxybOH3G5EB26KtXrD/FIZFnzIGWb5wXPd5n3S27ES2aPbn+NgsFVcwlEHcT/v7YGtG2uoEWlN2l0BMrlGxJFH/1VvC9uaAdEFg/OLPLg4ApU2BmR5f5Z2VatceQxiW1ICsl/jwcpSGmRvUxXoQ2lrg6dDrgVLTnBStVzFI0GqoWyn94nhk2iiRGoW8q+4SKu4hxHZsmn0cu5S0GWrd0xuN1fr58MUKCi2bdDb4VxECw+RQFe1ircMXV4n696kCBIaKU7bK4iCD7rdtgYzmJkS76eRwFbC9VpiCPFdPH3+lyQklM1qXru2+LvSZp9p76hEMrjgFOj6Eo4TFLUbgFgbo5IHbU62KAVGaxxyxHFAnZocAj4piwoVL9i3fhOQa20bdO9gFeqzdksOHfWQ8KLejlOf+qzzhM18DuQ DvR+85nv 4Wa74DM/rWprAd01fHqOAMlrcGLMFGAUVi3/mMLhxZvr495vEF5a0DV0rjyJvYl81kqg4 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Instead of storing stack records in stack depot pools one right after another, use fixed-sized slots. Add a new Kconfig option STACKDEPOT_MAX_FRAMES that allows to select the size of the slot in frames. Use 64 as the default value, which is the maximum stack trace size both KASAN and KMSAN use right now. Also add descriptions for other stack depot Kconfig options. This is preparatory patch for implementing the eviction of stack records from the stack depot. Signed-off-by: Andrey Konovalov Reviewed-by: Oscar Salvador --- Changes v2->v3: - Keep previously existing Kconfig options not configurable by users. Changes v1->v2: - Add and use STACKDEPOT_MAX_FRAMES Kconfig option. --- lib/Kconfig | 10 ++++++++++ lib/stackdepot.c | 13 +++++++++---- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/lib/Kconfig b/lib/Kconfig index 3ea1c830efab..5ddda7c2ed9b 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -713,10 +713,20 @@ config ARCH_STACKWALK config STACKDEPOT bool select STACKTRACE + help + Stack depot: stack trace storage that avoids duplication config STACKDEPOT_ALWAYS_INIT bool select STACKDEPOT + help + Always initialize stack depot during early boot + +config STACKDEPOT_MAX_FRAMES + int "Maximum number of frames in trace saved in stack depot" + range 1 256 + default 64 + depends on STACKDEPOT config REF_TRACKER bool diff --git a/lib/stackdepot.c b/lib/stackdepot.c index e41713983cac..682497dbe081 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; @@ -264,9 +267,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)) { @@ -301,6 +302,10 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) if (stack_pools[pool_index] == NULL) return NULL; + /* Limit number of saved frames to CONFIG_STACKDEPOT_MAX_FRAMES. */ + if (size > CONFIG_STACKDEPOT_MAX_FRAMES) + size = CONFIG_STACKDEPOT_MAX_FRAMES; + /* Save the stack trace. */ stack = stack_pools[pool_index] + pool_offset; stack->hash = hash; From patchwork Mon Nov 20 17:47:05 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: 13461821 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 AECD2C197A0 for ; Mon, 20 Nov 2023 17:48:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4FE656B034B; Mon, 20 Nov 2023 12:48:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 44D716B0355; Mon, 20 Nov 2023 12:48:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 202DD6B034E; Mon, 20 Nov 2023 12:48:37 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 03CF26B034D for ; Mon, 20 Nov 2023 12:48:37 -0500 (EST) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id CDADE1CB5ED for ; Mon, 20 Nov 2023 17:48:36 +0000 (UTC) X-FDA: 81479067432.26.467B728 Received: from out-172.mta0.migadu.com (out-172.mta0.migadu.com [91.218.175.172]) by imf02.hostedemail.com (Postfix) with ESMTP id E3ECC80028 for ; Mon, 20 Nov 2023 17:48:34 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ukcrau9r; spf=pass (imf02.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.172 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=1700502515; 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=ul/nKw7kj0oOeSlF9NlnEwH7YAqJQYzQqOqaJ7LWVgY=; b=hrDYqnSRPMjvCEjSP+ddBm+tMFBU0YF297xfmsshzOoUueORi5mW9Mi4phGSIFljxVMO+8 i7jreWpq5qRIsbSb4sdbAvCw802nHU1Q8evoKZVXpVFRPNqbjMwgQPFRg8ysY1piX0yqIP JJPwcCGqfbGd9Io0SPYaL59Y5AtBbhU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502515; a=rsa-sha256; cv=none; b=eMkJcCBI0ONozLtizbNdfdxVHXu7Oxvm93l+gDkZNQgqyIlRWR0k7GguJVW7Obg9JJ2rtU V3Lg0zYEZyLZSEeEeHKzIbdWxHt//1yvu6OtUmcQwuDwsSRaJDP6aPl2AhsCeLEvj8w3wV 62oY+41CkQKNNU87KMK2P7NiTSMW6Js= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=ukcrau9r; spf=pass (imf02.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.172 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=1700502513; 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=ul/nKw7kj0oOeSlF9NlnEwH7YAqJQYzQqOqaJ7LWVgY=; b=ukcrau9rBSSL43tgTPUpFjw/XmDDuuV7b5Rf6ugJdDIx3eaVAx8V/9PFiPuIgxA8HOVqDI FOv6Ks7qLmHGEak24eJMUo0HQta3weJK4BNrKPY6LbsCGCWMiUh2CgVaukTzBdLG8nj0JF 0fqdYiwEAxqEdM4YGbvK075MNLH3F5M= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 07/22] lib/stackdepot: fix and clean-up atomic annotations Date: Mon, 20 Nov 2023 18:47:05 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: E3ECC80028 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: zuop9r4ahk6mjjybrka546a7zgayk7a6 X-HE-Tag: 1700502514-52927 X-HE-Meta: U2FsdGVkX1+20BBYnqw3mtoFN8iU3mDRqv1xyMbyQmEnWcOdeoFPgSbvdcexP/RbAmYiXelN7gTzq7v2jz48OtNX2SuFDJD6qX/s3J2WIrPHIMggbBXHHKkHQgv6+/JnjoYu0epD/qjL3OAYtPg3WPQLtyDfLoqIXV5NmXexfnd/LKzdxTFKqn1zgy3D31+21hMTinErHdbTgPQfyGwdajAmlQt0Jujq+Cb1UwGFId0Xh9ik0prVmIDXvurvqnTAHMvzreIS+O9jJJX8e3pfsusDm96hrv2T24XDKdNdidRZefNUoBBC6dL9JkjqlUjUbFsf+2OLVFzVj88ZS4kwQ+b6DyONQ9l13NFbbqRe2stBEHjIpi18HVQORlz/wKZRRRM2yN37NlzYetTqsUJPxgkh0+X9sFPLJ/hp557y4UQU34qvmD8u4BxhRvNI5FjTTZCPgcaqg3vF1eVIHLcMJxy8vP0i9jyQvE3lj65GyQGjpBONs4QVTesDKcGxzvlK2jQwMT/hNGWGl+VzyAbcpVdwFFZpE7xdoJH30Pc9bidizS+Ump5NhPOHG+dv0wF1q7VknA42NR/ekzgaPXbXL4rCyEKlOCUnv41g09xVRc1P85YWmNBjwDD6sZVguS+wLMT3toMAcjARdmzVUoSXRS/d+ZpzawayL5P1aekpjt5r//aIEUv8kQx3dWXkpqAslXfR9WAQSRxGwBKdZlQ4Z6v0UW93oAL4AQyQGghhoVrFSKj/Dy23g+BzfW8+CyIaLb+1tUXG50YyLIjvUTgtirEjqsCqSiWniPIYJC/zMYK4j+G5ine+ZqOdMlPkcAQjWrKvI9Y7pkNAtDIZmnFHIufYQ4HtNZc284IMwVDZpKKzwqyOLK8q1xqUdTst/IjF5/HE0P3XtYrNXEWTL8yrTficPmC2TwqPknvQv2xgcz04WajkTYZyj2MFc0zAcq5IMshPz2pV31x3MO15TPn rDuEDO8m w8lyvwbyXAeL2Ss/u3UY6CgcSPHUXauPNVUBBRZvm66tbEOwYu2QH+c5uRnrjgQ58jGOv X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Drop smp_load_acquire from next_pool_required in depot_init_pool, as both depot_init_pool and the all smp_store_release's to this variable are executed under the stack depot lock. Also simplify and clean up comments accompanying the use of atomic accesses in the stack depot code. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- This patch is not strictly required, as the atomic accesses are fully removed in one of the latter patches. However, I decided to keep the patch just in case we end up needing these atomics in the following iterations of this series. Changes v2->v3: - Keep parentheses when referring to functions in comments. - Add comment that explains why depot_init_pool reads next_pool_required non-atomically. Changes v1->v2: - Minor comment fix as suggested by Marco. - Drop READ_ONCE marking for next_pool_required. --- lib/stackdepot.c | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 682497dbe081..cfa3c6c7cc2e 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -231,10 +231,10 @@ static void depot_init_pool(void **prealloc) /* * If the next pool is already initialized or the maximum number of * pools is reached, do not use the preallocated memory. - * smp_load_acquire() here pairs with smp_store_release() below and - * in depot_alloc_stack(). + * Access next_pool_required non-atomically, as there are no concurrent + * write accesses to this variable. */ - if (!smp_load_acquire(&next_pool_required)) + if (!next_pool_required) return; /* Check if the current pool is not yet allocated. */ @@ -255,8 +255,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); } @@ -279,7 +279,7 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) /* * Move on to the next pool. - * WRITE_ONCE pairs with potential concurrent read in + * WRITE_ONCE() pairs with potential concurrent read in * stack_depot_fetch(). */ WRITE_ONCE(pool_index, pool_index + 1); @@ -287,8 +287,8 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) /* * If the maximum number of pools is not reached, take note * that the next pool needs to initialized. - * smp_store_release() here pairs with smp_load_acquire() in - * stack_depot_save() and depot_init_pool(). + * smp_store_release() pairs with smp_load_acquire() in + * stack_depot_save(). */ if (pool_index + 1 < DEPOT_MAX_POOLS) smp_store_release(&next_pool_required, 1); @@ -329,7 +329,7 @@ static struct stack_record *depot_fetch_stack(depot_stack_handle_t handle) { union handle_parts parts = { .handle = handle }; /* - * READ_ONCE pairs with potential concurrent write in + * READ_ONCE() pairs with potential concurrent write in * depot_alloc_stack(). */ int pool_index_cached = READ_ONCE(pool_index); @@ -419,8 +419,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) @@ -430,8 +429,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))) { /* @@ -457,8 +456,8 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, if (new) { new->next = *bucket; /* - * This smp_store_release() pairs with - * smp_load_acquire() from |bucket| above. + * smp_store_release() pairs with smp_load_acquire() + * from |bucket| above. */ smp_store_release(bucket, new); found = new; From patchwork Mon Nov 20 17:47:06 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: 13461822 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 CF3D2C5AD4C for ; Mon, 20 Nov 2023 17:48:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 97F5B6B034E; Mon, 20 Nov 2023 12:48:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 907326B0351; Mon, 20 Nov 2023 12:48:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6ED6D6B034F; Mon, 20 Nov 2023 12:48:37 -0500 (EST) 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 404BE6B0351 for ; Mon, 20 Nov 2023 12:48:37 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 1E7E4140319 for ; Mon, 20 Nov 2023 17:48:37 +0000 (UTC) X-FDA: 81479067474.18.3E91E0C Received: from out-185.mta0.migadu.com (out-185.mta0.migadu.com [91.218.175.185]) by imf14.hostedemail.com (Postfix) with ESMTP id 5DCD5100009 for ; Mon, 20 Nov 2023 17:48:35 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Kt5p6zSc; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf14.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.185 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=1700502515; 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=9IYHxZGEq/qB2Qlddv235LQK4ccHh5XyxORYKM2z5Wc=; b=FAaoSEIZVbq/hl6ieJZeAtDgCJnnf9aIsFJU9yKFLW2RpkAfwL5EvBDadv4/izipNv6dVk +3GcqY4mugor+FG03vLty8VNmkpQgg21YpekZCsyi9/Bg/bXKZCoI1IS1ZXIbwq7CapmDm Rm4At615DX9cJPeEkKG/s6B+W1HI+4c= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Kt5p6zSc; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf14.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.185 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502515; a=rsa-sha256; cv=none; b=FN/QoGU4V3jZeKHPbrWW/YmoIkfVSi4wVQbUIkVjMFn/CxCHdjz8pqchMYejbN2kwTb/KQ gmt19W3PhQ7vWwArDNk6QdloG/clP3dlq2YJU6vFpd+4wMSKxLMzbkDLjKl7sz/KDY35i7 vgyi9XONE2yOq34/Abc7u4RMYyumRv4= 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=1700502513; 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=9IYHxZGEq/qB2Qlddv235LQK4ccHh5XyxORYKM2z5Wc=; b=Kt5p6zScvUKaXsHar2QlUefWEFtJ6Uws7kbkfGgrB5cxpVMGSn2WXhKYFJ5MhB50RhntDQ K7N0E2IbssHoPp6AeWgnF1sPLT3aDEF/nGgRuJ546AoCXVl6clQWGAmNHZLEBxlZNtF5g9 xiIqlvFkZnYs+J6Hza0Jtp/FsRG4z5s= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 08/22] lib/stackdepot: rework helpers for depot_alloc_stack Date: Mon, 20 Nov 2023 18:47:06 +0100 Message-Id: <71fb144d42b701fcb46708d7f4be6801a4a8270e.1700502145.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Stat-Signature: o3ne7cbxqnyrbba1a4jiz1u3ggbcz8rw X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 5DCD5100009 X-HE-Tag: 1700502515-442004 X-HE-Meta: U2FsdGVkX18X6H8I1+eDmZzc5h9MeV040gjvCgd9kLlFSKjIfGlaju/Xh4RZtrRKC5S7NjdtjU4Pt6GsJ8+I1dD7jmgttoSABUkmH2ELxQR7ih+Ttjs9f9NBoDTbdyliKbJMBiU5QcXRjNpAcL40aoxcMZqS/qpqhbgNY2q4fq9gMDY/tKNcNm9EgnNOcm8cQofBGpLr4P8oeEL3qrNyiNnCtOu1ON7YGAnoSND76TNuZELlcazYCxaRPeww20VLFu+CnqHTVkMxwFVmzP1vGhgq9CfGbm05FLKa/VPStCdivIOp/Og4wyg/NoI6SIGJNTeijI1Kg4tysyT+ybg/1ATTc7rU5lsJOZ1JcnqJ5Z+3bUz6GZXMwwmHtRhdHpRgsCFHMZ3l1VO63BXxarpRbcvX4+dMHYY+xPDRlEbJeAhfm2J44mYWKZf6HVQP6gKKhq87nH98bPCpblDsponHJ4lQErtdmUBurK6fAl57E14u0+sR7Q9r/Keu7pFdSu/ofmYlBh+V0bMbdafvVmaWgVAl3fAU0BCO8U+QprmoCegEyD2euhaYvF4FUetLzRXsR/AqKikL/V78Ls/6+wq3W7HW491KHaV/+CDaD6r+a7aGNASCbBWCHhyWwKOE79eqCV6SxdonraRYQYSeTt26CJc1CP8jiubb+vJiMhCbJfPI44ZkxvWbYjU9t5DtYaBd1cegyprqVKnVaSECgCG+auWVzhlVPw0t4q2ndIAEHCXQfYcVBgjnYGYMr1PijzUf4WEJ20stZNr+y7YFQaMUaqkmIN5yjYlbfzGO8+q+WtZthEq8x79NOEPyisirWk+TBVV7Of6MqUtQcNyMJXk6pHlqkVUlffe4yIKK0eif6qPqvreM9tCEz+SxtbhrDS/gOFIKocAOA64XU6XJ/Ysdcu+OUZ/jpRBhY8a4WNVSPlUeEl7QEsDk13OagrWjTbEzffDURDz5VwdFseCjQzj A3J3tPTZ EpsObRF5rtpUMA85swVkyipe1PNPf/sR912cEkXLT4snjn6SJolLqAc+NExG4wuFR8jrYqUB0o+kBwX/r8sdgZQwREb1Hcn2XvOzYeQ/vbPG6RTUWOT1DsTup3Q== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Split code in depot_alloc_stack and depot_init_pool into 3 functions: 1. depot_keep_next_pool that keeps preallocated memory for the next pool if required. 2. depot_update_pools that moves on to the next pool if there's no space left in the current pool, uses preallocated memory for the new current pool if required, and calls depot_keep_next_pool otherwise. 3. depot_alloc_stack that calls depot_update_pools and then allocates a stack record as before. This makes it somewhat easier to follow the logic of depot_alloc_stack and also serves as a preparation for implementing the eviction of stack records from the stack depot. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov Reviewed-by: Oscar Salvador --- Changes v2->v3: - Add parentheses when referring to function calls in comments. --- lib/stackdepot.c | 86 +++++++++++++++++++++++++++--------------------- 1 file changed, 49 insertions(+), 37 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index cfa3c6c7cc2e..b3af868627f4 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -225,11 +225,11 @@ int stack_depot_init(void) } EXPORT_SYMBOL_GPL(stack_depot_init); -/* Uses preallocated memory to initialize a new stack depot pool. */ -static void depot_init_pool(void **prealloc) +/* Keeps the preallocated memory to be used for the next stack depot pool. */ +static void depot_keep_next_pool(void **prealloc) { /* - * If the next pool is already initialized or the maximum number of + * If the next pool is already saved or the maximum number of * pools is reached, do not use the preallocated memory. * Access next_pool_required non-atomically, as there are no concurrent * write accesses to this variable. @@ -237,44 +237,34 @@ static void depot_init_pool(void **prealloc) if (!next_pool_required) return; - /* Check if the current pool is not yet allocated. */ - if (stack_pools[pool_index] == NULL) { - /* Use the preallocated memory for the current pool. */ - stack_pools[pool_index] = *prealloc; + /* + * Use the preallocated memory for the next pool + * as long as we do not exceed the maximum number of pools. + */ + if (pool_index + 1 < DEPOT_MAX_POOLS) { + stack_pools[pool_index + 1] = *prealloc; *prealloc = NULL; - } else { - /* - * Otherwise, use the preallocated memory for the next pool - * as long as we do not exceed the maximum number of pools. - */ - if (pool_index + 1 < DEPOT_MAX_POOLS) { - stack_pools[pool_index + 1] = *prealloc; - *prealloc = NULL; - } - /* - * At this point, either the next pool is initialized or the - * maximum number of pools is reached. In either case, take - * note that initializing another pool is not required. - * smp_store_release() pairs with smp_load_acquire() in - * stack_depot_save(). - */ - smp_store_release(&next_pool_required, 0); } + + /* + * At this point, either the next pool is kept or the maximum + * number of pools is reached. In either case, take note that + * keeping another pool is not required. + * smp_store_release() pairs with smp_load_acquire() in + * stack_depot_save(). + */ + smp_store_release(&next_pool_required, 0); } -/* Allocates a new stack in a stack depot pool. */ -static struct stack_record * -depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) +/* Updates references 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; } /* @@ -284,9 +274,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(). */ @@ -294,9 +285,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) @@ -330,7 +342,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; @@ -430,7 +442,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, * the memory now - we won't be able to do that under the lock. * * smp_load_acquire() pairs with smp_store_release() in - * depot_alloc_stack() and depot_init_pool(). + * depot_update_pools() and depot_keep_next_pool(). */ if (unlikely(can_alloc && smp_load_acquire(&next_pool_required))) { /* @@ -467,7 +479,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, * Stack depot already contains this stack trace, but let's * keep the preallocated memory for the future. */ - depot_init_pool(&prealloc); + depot_keep_next_pool(&prealloc); } raw_spin_unlock_irqrestore(&pool_lock, flags); From patchwork Mon Nov 20 17:47:07 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: 13461823 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 2B62EC197A0 for ; Mon, 20 Nov 2023 17:48:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 16A216B034F; Mon, 20 Nov 2023 12:48:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 07E026B0351; Mon, 20 Nov 2023 12:48:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C8A106B0355; Mon, 20 Nov 2023 12:48:37 -0500 (EST) 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 9EA476B034F for ; Mon, 20 Nov 2023 12:48:37 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 8430D40978 for ; Mon, 20 Nov 2023 17:48:37 +0000 (UTC) X-FDA: 81479067474.13.78C2654 Received: from out-180.mta0.migadu.com (out-180.mta0.migadu.com [91.218.175.180]) by imf19.hostedemail.com (Postfix) with ESMTP id C25F81A000C for ; Mon, 20 Nov 2023 17:48:35 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=tZ0IiLou; spf=pass (imf19.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.180 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=1700502515; 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=hhwH9Rj+7SlwckI0c6mMcIkIslfacxpde7X6neQ7UWA=; b=XZPGX2mtsvQl61/YHpV/SNAEGEEIx5tws5AI6Y3lVYEOdkSrpAURUSX8GpShqhV1aV/fg2 pyi255Q3/pEsqPTNE0FfJFm7kLbt9KyRo+JmVv9fXM55QmacQA6bhx9vIlSVp/h+4C21L7 BzM1oo85WEf8jU+9zu9Yhk4HcTRdfTs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502515; a=rsa-sha256; cv=none; b=8iOFgGbA8rciNziQVFGRdoCv5igt2bNmpA2v/+JUR0fgp6mGme9ncyS0cjduOZLl/lqK72 TLdXU6AZaZi1yywY+AZZD6dQsc/pl93TDlx3mxdQXvls0R+TJ9lD0oW+9eaoMCqP0wBuq9 y7wCecIYIYWe5v57AnbL3v1l/LbDS0g= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=tZ0IiLou; spf=pass (imf19.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.180 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=1700502514; 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=hhwH9Rj+7SlwckI0c6mMcIkIslfacxpde7X6neQ7UWA=; b=tZ0IiLouwcQL+2MAJN48oud52EurhrFZ0EVdLm9w8bTgwIXFM6wmInvK6PvSCrc9NfsCH+ mZJSfVlEyctgDWkqrhpIIt5iHEnorbr/fJGIrTZlLY3MGFvfhwPt5aeKYygAHMY4xY+vIV lxaS4PU+oIsToSR3HDnVIiG5H7/UWHw= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 09/22] lib/stackdepot: rename next_pool_required to new_pool_required Date: Mon, 20 Nov 2023 18:47:07 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Stat-Signature: yjka4itz7twdz494gpe54uwfspzrifnk X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: C25F81A000C X-Rspam-User: X-HE-Tag: 1700502515-737656 X-HE-Meta: U2FsdGVkX1/MA1G8N+oksGT0/wBBFIAoueIzsj0w4UYujBDSPHGf9suxVwTrDaBPIyP9vEYicdydp5VLbfWmvD62W6a2YinYG0AZAAWPS3q2ln9I+Qwdg92SSjh8k8sV3sQRG10YcU0aLbrnoG8JcyZIbCKP/6pyDUa2gH/OEqgkrNvkA5MFafxKHxXI4TxeG2WitxZhynSnZ9dYGXEAz0rJxLBK3GaArvqg9ZTGMwYOXGyvmB0/m9qR60QUx52h6clyQGTmMhxtV/kHgaPmSKKL7JjnYsdox25XlIlaxt9Srdt/iERJsb7nE4n+cCLXgtvq9UL8iOl6jz1NHBhx5CT34bebqpHnP8PZosWTjYj3ufdjGlISXhkH8aHWFlBYXWzBem5cKhbLBLk/EalNaZ+psjZtDsTM46MPSYnxRnbrGFEW9g7CX89VCRAOFZqx4LGQRDxNZqcxXF6/OyiH8WY4Du1gP1eGT+uR35CJ1MNAvkhGk0Tx5usYI0NVNVPDMNVPDs6xODRxxByc6spNUj/4k8AK7KFDGq4KRd9Fq+KvOKqkoiu5xyZE05L8K50oYAulVMKGw7LAwIZb3z6cf9Im5bREFq9o3z/VbJMHzwC1sEJDb/7o0h2Sfw2mzf8PKKC4BVmWUhb3+wwPiXk5UWYZaaB5kuCtWYWgs+LNHqfq9bc3gQ4lqNaceGB1GrtTprEI9Ch1FvUyZP5OSmp09877V5ipEt8UA+mxUytAYQAD4PH4/6vJCUPIc5l3GXO84zQhCOYzq5bYmelqW1/9LwXioG0mncIG3c6S+gw5k5mgUitoYJ0PTfckEstRFD8rHTmg6oWCtsSTJ59fQ9kLPUM7r0qu6alCn38kwCxGSOmK63cBo1wORRsW8VLw2lDDxoV6zTZupKWM9y++zj+z51hmTzGCZcoZselgo7AVjZEnX72BZduNpj2/AqJVPUcKv+TcsX4qWfgPm8hVgga 9qqdS5fs Xg/fvrwngXVWnzJ642o6gLF+k1+Ji9IXODNwXsH4kVZTgIuy9tZVqxaZn4jGS3KQLPp80iedA13Y32VxyCQS8dIl5HMrt7hxTn+8dLqAabP+m8bk= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Rename next_pool_required to new_pool_required. This a purely code readability change: the following patch will change stack depot to store the pointer to the new pool in a separate variable, and "new" seems like a more logical name. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov Reviewed-by: Oscar Salvador --- lib/stackdepot.c | 49 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index b3af868627f4..a38661beab97 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) { @@ -225,20 +224,20 @@ int stack_depot_init(void) } EXPORT_SYMBOL_GPL(stack_depot_init); -/* Keeps the preallocated memory to be used for the next stack depot pool. */ -static void depot_keep_next_pool(void **prealloc) +/* Keeps the preallocated memory to be used for a new stack depot pool. */ +static void depot_keep_new_pool(void **prealloc) { /* - * If the next pool is already saved or the maximum number of + * If a new pool is already saved or the maximum number of * pools is reached, do not use the preallocated memory. - * Access next_pool_required non-atomically, as there are no concurrent + * Access new_pool_required non-atomically, as there are no concurrent * write accesses to this variable. */ - if (!next_pool_required) + if (!new_pool_required) return; /* - * Use the preallocated memory for the next pool + * Use the preallocated memory for the new pool * as long as we do not exceed the maximum number of pools. */ if (pool_index + 1 < DEPOT_MAX_POOLS) { @@ -247,13 +246,13 @@ static void depot_keep_next_pool(void **prealloc) } /* - * At this point, either the next pool is kept or the maximum + * At this point, either a new pool is kept or the maximum * number of pools is reached. In either case, take note that * keeping another pool is not required. * smp_store_release() pairs with smp_load_acquire() in * stack_depot_save(). */ - smp_store_release(&next_pool_required, 0); + smp_store_release(&new_pool_required, 0); } /* Updates references to the current and the next stack depot pools. */ @@ -268,7 +267,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(). */ @@ -277,12 +276,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. */ @@ -293,9 +292,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; } @@ -306,7 +305,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; @@ -438,13 +437,13 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, goto exit; /* - * Check if another stack pool needs to be initialized. If so, allocate - * the memory now - we won't be able to do that under the lock. + * Check if another stack pool needs to be allocated. If so, allocate + * the memory now: we won't be able to do that under the lock. * * smp_load_acquire() pairs with smp_store_release() in - * depot_update_pools() and depot_keep_next_pool(). + * depot_update_pools() and depot_keep_new_pool(). */ - if (unlikely(can_alloc && smp_load_acquire(&next_pool_required))) { + if (unlikely(can_alloc && smp_load_acquire(&new_pool_required))) { /* * Zero out zone modifiers, as we don't have specific zone * requirements. Keep the flags related to allocation in atomic @@ -477,9 +476,9 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, } else if (prealloc) { /* * Stack depot already contains this stack trace, but let's - * keep the preallocated memory for the future. + * keep the preallocated memory for future. */ - depot_keep_next_pool(&prealloc); + depot_keep_new_pool(&prealloc); } raw_spin_unlock_irqrestore(&pool_lock, flags); From patchwork Mon Nov 20 17:47:08 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: 13461824 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 9078EC5AE5B for ; Mon, 20 Nov 2023 17:48:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B7B2E6B0351; Mon, 20 Nov 2023 12:48:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B05406B0355; Mon, 20 Nov 2023 12:48:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 92F856B0357; Mon, 20 Nov 2023 12:48:38 -0500 (EST) 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 757426B0351 for ; Mon, 20 Nov 2023 12:48:38 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 4A7CC1201BE for ; Mon, 20 Nov 2023 17:48:38 +0000 (UTC) X-FDA: 81479067516.06.7767EFA Received: from out-181.mta0.migadu.com (out-181.mta0.migadu.com [91.218.175.181]) by imf12.hostedemail.com (Postfix) with ESMTP id 75D8E40024 for ; Mon, 20 Nov 2023 17:48:36 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=mVlXzudg; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf12.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.181 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=1700502516; 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=Wo0lOk4QF0V4/CSryrBtBcTGMr2NPIzDQs2c2iiME6Q=; b=edhz44Ja+Qju4gJD0ILhFfujkGhMTv7H1bQXPLOj4xk1W8CVns7OMleM1r1hHbXtxr3g89 BAVGmsXE+b++9MEJDlC3yzkyvN23J6opXoY0ujqyXzI2KeTs91MLp8N+pC89/AaGcOmjIm kSfRDmJl+Vps+NHooYXGYneNtPHj1BE= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=mVlXzudg; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf12.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.181 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502516; a=rsa-sha256; cv=none; b=QO3tgZeoqtlEGHZNbkSsVA1MneiujxwoNmPrJV5P6xIMOZyisfbpd76+P+PrqwIrXAGrPU c+SXZkI+qLCIuTHk8r8uwcSIx20x+QdfAHXc+4+Su2kfMA/9uE/s9cEnVVuAEOKxfWUsrK qxbiFE4+zGBVp5/wReU6LJI0e1owcU4= 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=1700502515; 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=Wo0lOk4QF0V4/CSryrBtBcTGMr2NPIzDQs2c2iiME6Q=; b=mVlXzudgWPREwFPbPjEzWKl0pkuPrGrBfXgV/o3hu2wbrexzrKyl7c+k+ldkpF3cqIoiwI u8DO0T3MhAJP+5xhUN4eko0SgcoGz172qfdNSCMVSIN1yVWwfrjX7hnhKKWOfY2ZNwgi4e RtBwt9lIOYU3HUK95YFtEZQHsBHOLqY= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 10/22] lib/stackdepot: store next pool pointer in new_pool Date: Mon, 20 Nov 2023 18:47:08 +0100 Message-Id: <448bc18296c16bef95cb3167697be6583dcc8ce3.1700502145.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 75D8E40024 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: j1znxzugyd8gqry1e5h95isqjk7rbwfk X-HE-Tag: 1700502516-867121 X-HE-Meta: U2FsdGVkX1/RSoNXode3MkOpIAPl2ufJxZI+gKk5MrEO40jRHcQTJ1qC3RcTwng+epfi+vnFMcpIcrllFNmal1XcLJ88rQkObqRgjHebo6Ha3aip+dnIqYRX4E9wiPV1haKuwx7lflSUlv/woU+mTT9MN6l1X2TUnWePV4QoPJvWSwp+vr34XbASOf10dVZR2H0FpCuAYQIn0490X1zem/TRszZ6+d9GAV7TIghsXBHa4O+0J4A5JzcLZW2OlOkQpzUSf530zt46OWRl+1vicDHhW3/s4pBdGiDZtvgSDaHwtNI+C+VQ+agzU5YdHey6UNYHTQ4rz0/DGfR2hoH6bkgTwXnLO68U46NSRUcHufzM0BEMPUSz8fFC140naD5FmZa4PKWZf+9nbjJ1nCVqI6uGukLfh3pA8xS6YWTvo9ro4ZmsDJl9mtNFx7KvYiKkxr2G4J0VIcsib5bB5CawuOir0ctyuj1Ry8rfIB3DzYegybilPPzSgQ/0EoR4sjLFp3EELYgPm6dekehCL72DW2LnVBaZv8rOQ+vzEVaAzH8y0wf798FKDIOMwM0Dg5Ah/+3npXpyYd6F7OBL9krI7A9Y0oAePyTaUrMqKHkssvAEr4AK7UqdczaGXz8A6nHwE3WOjrSVP+r6J9qD2eSsbyVumDjCfIu55o2h5cQs8Jc1HQfxOmrHXmf8AGwhYuzunxAYgPz8eWWbzKLeAVeml9HLIzVfxRZw3wWG9uXI9dpdOHheAQqzhMhRgdn+w//06Vlg6EahZCMCMsxpl9ZYFuMPdBil6tpCQEINNujN99hseBQCFKNoQ5wHv3AbhF5feAM3Ad5n1fcQOuz0hTStWmkO4vSZHpfMkDccTJsXTlbT3PSbDgmZJZkP486ooN8p36n0u9Ehdwt5IFnkNStwjVKVz5RnS+uYn32Onx2v7gmQ3n+cduEba9cXXjuLeqKqbn+pemuUnrs5jqSTtqA 4QJqVTyx Z1lBlT/t/rdTx0J6fDYyW7qBRUNfR4Zew/4NY92vf/8GoNCcoPc0tTigML68+Khf+OMO5W9hBQZBuNQAeLTbbet635Xa9raAw/ajm6kwIQUv520I= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Instead of using the last pointer in stack_pools for storing the pointer to a new pool (which does not yet store any stack records), use a new new_pool variable. This a purely code readability change: it seems more logical to store the pointer to a pool with a special meaning in a dedicated variable. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov Reviewed-by: Oscar Salvador --- lib/stackdepot.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index a38661beab97..68c1ac9aa916 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. */ @@ -241,7 +243,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; } @@ -272,6 +274,8 @@ static bool depot_update_pools(size_t required_size, void **prealloc) * stack_depot_fetch(). */ WRITE_ONCE(pool_index, pool_index + 1); + stack_pools[pool_index] = new_pool; + new_pool = NULL; pool_offset = 0; /* From patchwork Mon Nov 20 17:47:09 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: 13461825 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 046C8C197A0 for ; Mon, 20 Nov 2023 17:48:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3B3406B0355; Mon, 20 Nov 2023 12:48:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2EEC36B0357; Mon, 20 Nov 2023 12:48:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0F2326B0358; Mon, 20 Nov 2023 12:48:38 -0500 (EST) 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 D3A556B0357 for ; Mon, 20 Nov 2023 12:48:38 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id AE133B5A50 for ; Mon, 20 Nov 2023 17:48:38 +0000 (UTC) X-FDA: 81479067516.28.4F89E64 Received: from out-180.mta0.migadu.com (out-180.mta0.migadu.com [91.218.175.180]) by imf17.hostedemail.com (Postfix) with ESMTP id EAA3940015 for ; Mon, 20 Nov 2023 17:48:36 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="RxqnWJ/W"; spf=pass (imf17.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.180 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=1700502517; 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=+N1Q7uFgicZ/Pf0RFILDijpppowVMY0ExiNLLtkdgaQ=; b=T+o/kNngRW1GTEfmK+W76C3j2w7TzW0uWQr2aigpP/csabmqMtj/86KItgONbvjz+WN2qo TziWe7IeKpg+miI0H9EJJ8It/JKXsqFzR/Bm1G6gZBPuX07e/bT+9Vd02J6bIXfutNtj3L vjSO1MKHLjesxghjL1ZQ0Hg3CKUyKXQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502517; a=rsa-sha256; cv=none; b=CNnFppRNKSF7/nGQJlPWciyWIFWbjDfiwyB0XxiJVtmChiqiS4HTUmPBExEL47YWephqwB TOCBj561It1WqU/5zn6HtlNyh8vyAa6LGrXhsMGJ2SnRctg3qsAvDFH0/rSr9Itb3JGwS8 hn/rNNru23sUmvB8ctf4+64hjeoVxdQ= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="RxqnWJ/W"; spf=pass (imf17.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.180 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=1700502515; 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=+N1Q7uFgicZ/Pf0RFILDijpppowVMY0ExiNLLtkdgaQ=; b=RxqnWJ/WbNov6GNDVKKl98NdJigJDFyuUx5XrfpgY0Y/FIFkxZ8cb2F/DaXDPkvN4oYnyI w7lMzmrfXQRH2KCXJzK07x0GGzxp0h1cjOV+z4h6XLtv4ZtoYRFtRsvvF70WP3/hsS5GYv gtRARyq+ePtSURhjvSTg/rB+k7qi5sM= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 11/22] lib/stackdepot: store free stack records in a freelist Date: Mon, 20 Nov 2023 18:47:09 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: EAA3940015 X-Rspam-User: X-Stat-Signature: u71647ge3fx7p8yrrh7rahenbymseshk X-Rspamd-Server: rspam03 X-HE-Tag: 1700502516-586184 X-HE-Meta: U2FsdGVkX18SgyZZ7SMvukn8V3tJ+3VW2alEgWBRQwGLUDrd/5FMglvVHyGsiCTzHFJv5cZNJqJ07fKJdECYFO5vlm2exqFDAq0MaaYia2+yzkKDbcYS+U3ez/08+ctQUo/e2KNn9mlqX8QCYLwg3Ks2eww+Z8rBs1Nw/NcrKwQEYmRLz9+qrvbxNoScJjR7YF4QkiZMP6n5Aaryzp0PzOHT/qllUKGyuQM500LEhwNH7Npsx5w0s5D164d6pMbcgTE9jidMOlA4hEKeKxolzYMJr0PaRMrV4r0btsg0w52VLnFRLLk+yCrXophcVd2vJVW98s7Ruf8n/ca4GV3mnzat5eoCE9u5GPW6q8ABK3nORo8q1sKrexTB7MYn5V8RSymOsTPZlFlCK3YLJ90ds5wS+SxTwjaHeJLnrXU26twF7UkGvf2lqusWdRcP7bj1gXh2vfcZo8xHyWvNjGEKzfzlbVoJTUZ9lR9FavvEavvP35Ba2pkVwar1cHaMMjQ75TnvISI4o985gful0H5PS1o7rKRRkbEsSuYMU1Tpvyv8KYH9fMe2Y6aySl0NITDgzgaurliPqSTJJRVJCBbXvBjqr2j5UF0t7M7gG8DASn3zGj6shKOktKV2loD/fFpCWs5uNJxFKcwzUF10KsD9Lcdea9fjIGbjAsne6YjFTtn8RzsDscZ4HF0JUxpstbTV9B9vF9UA43BLgtIr0jr09Z30pvXDifS2iF183dbomcyYN4lFFod41Ck4QJvZGB6PXerZh4fYXLHGnRPk9Lv48rv2DffFV43a7bLt9dYD0J8YyUkE1ce4ogX/KGsIUhGZKPBSg6M5RPRPQ+qgZLN37c2VqTBSYbZ/BxglaYO/IO+gKKUFzlGePsO1z0CMA8wqgsVDoX72NtHAl1Zpb4R90+2c71+5TdE1Vg6Vv+x7njlnBfhoYkqrUsmyTfKU/46gi6rs4AD9nuIAHI4KI87 UtrfU7fn DoDOhKEI5uqF9OLRwBGROs45cr1YWiD2QVpRgL3lAeKU9tr6Jx42Wi7ibsw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Instead of using the global pool_offset variable to find a free slot when storing a new stack record, mainlain a freelist of free slots within the allocated stack pools. A global next_stack variable is used as the head of the freelist, and the next field in the stack_record struct is reused as freelist link (when the record is not in the freelist, this field is used as a link in the hash table). This is preparatory patch for implementing the eviction of stack records from the stack depot. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- Changes v2->v3: - Add parentheses when referring to function calls in comments. Changes v1->v2: - Fix out-of-bounds when initializing a pool. --- lib/stackdepot.c | 131 +++++++++++++++++++++++++++++------------------ 1 file changed, 82 insertions(+), 49 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 68c1ac9aa916..a5eff165c0d5 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); /* @@ -226,6 +226,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) { @@ -242,7 +278,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; } @@ -258,45 +294,42 @@ static void depot_keep_new_pool(void **prealloc) } /* Updates references 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; @@ -307,35 +340,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; } @@ -345,16 +378,16 @@ static struct stack_record *depot_fetch_stack(depot_stack_handle_t handle) union handle_parts parts = { .handle = handle }; /* * READ_ONCE() pairs with potential concurrent write in - * depot_update_pools(). + * depot_init_pool(). */ - int pool_index_cached = READ_ONCE(pool_index); + int pools_num_cached = READ_ONCE(pools_num); void *pool; size_t offset = parts.offset << DEPOT_STACK_ALIGN; struct stack_record *stack; - if (parts.pool_index > pool_index_cached) { + if (parts.pool_index > pools_num_cached) { WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n", - parts.pool_index, pool_index_cached, handle); + parts.pool_index, pools_num_cached, handle); return NULL; } From patchwork Mon Nov 20 17:47:10 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: 13461826 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 DA90DC5AD4C for ; Mon, 20 Nov 2023 17:49:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 75D116B0361; Mon, 20 Nov 2023 12:49:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 6E5F96B0362; Mon, 20 Nov 2023 12:49:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5618D6B0363; Mon, 20 Nov 2023 12:49:41 -0500 (EST) 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 410706B0361 for ; Mon, 20 Nov 2023 12:49:41 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 1D1E81CAAF2 for ; Mon, 20 Nov 2023 17:49:41 +0000 (UTC) X-FDA: 81479070162.07.C337EAF Received: from out-177.mta0.migadu.com (out-177.mta0.migadu.com [91.218.175.177]) by imf23.hostedemail.com (Postfix) with ESMTP id 3303914001F for ; Mon, 20 Nov 2023 17:49:38 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=IaUsjEmo; spf=pass (imf23.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.177 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=1700502579; 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=K4xViYAWVaV6VQ5HtL4BYhsbr4MB9DPwoOHOaowHM8k=; b=SDMbCNcmD8Wpm2Ojo1/ERA/LQcxZPZAwKhPN6nLglN0PaC5w9Bkt+zGK0nNRkutiSbL1Bi 3VOFuGLvvio5VEJjnVPVONACr3p3DA0ZKDDAWEmH0bZ3zVOsAqyVOBG0OakP3CIvLIcxWa i58+ahiZ3Ia8cW0PeqGNVu1y4LZ+8IU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502579; a=rsa-sha256; cv=none; b=IaR0Y9Es9Vb/ZHfUtkG00hkX6+Bnrq7DJVxqHdMHnD5lDk1HHzUXypItkko8FfSMIi3qyB AoclhLKhkLpVXGA/nhDkcjgWcaA/7DMr6vEofNc9WGh51X+mX20+z6NJ8A1fbAHGv3PAUf sL+UqYmPs2Sk7iIytFRkusHk4bl1FR0= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=IaUsjEmo; spf=pass (imf23.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.177 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=1700502577; 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=K4xViYAWVaV6VQ5HtL4BYhsbr4MB9DPwoOHOaowHM8k=; b=IaUsjEmoPRLDB8+q+N1/FFx4BQ3dNpy2/8UK1Gk7f8jxcPQzpMGtk0vCNWAd3ep43VVaLY YSfB7RGeC4krK+GbjgpVSoqyBiDp1Yja1RT2FgG6vH1HpRdUzo+USs4eje1zfRoqc8P41c 5glC0pVcsGIq8X4efr6ATYrkrC3nrgc= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 12/22] lib/stackdepot: use read/write lock Date: Mon, 20 Nov 2023 18:47:10 +0100 Message-Id: <9f81ffcc4bb422ebb6326a65a770bf1918634cbb.1700502145.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Stat-Signature: t38ktehgtcwrn5bdink98ui6g8km44g5 X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 3303914001F X-Rspam-User: X-HE-Tag: 1700502578-239586 X-HE-Meta: U2FsdGVkX188e94fo4K/BdVpw5PWXMMV/LblRZmhfQoBvKgvP5+72ejhjNyv4s+/BeRSdSj/Cg/WK7HVSf371QTgZ0xt8yBJyCUQ4fzAQQ22uR6eArVA9pGVrF10Cv7njKSm3IcCZ0AG+qNaqKiJAtHT5SuNmmrXi6Av2YzR6nC+2PNzZqnO73c8+QeY/d7rbOXnBnVArmzPfslr9/wLvY1dDc64MBMbUKOAawyFcNeaxdMnxMidZx4gYUYLCwjcyv8uqP4dPXxBGcTE21UDLVBqVYgw/FedlYS9Y/TU6+WkRD6XbwzaHcSYug/TclwLQ2f/Yz1uIpmauGGnxOIhAuVCP1zquf7Ec4/Z+RgUgwpaaV5n8wsgMA0OASWwqEoyyhG2Qh8Whawyl7JmalyPPTae0e4t/KrW26SV4J4rCAI3Kl3qtjS/KTAfxyrQiM3LRL804EfkoqN3nRMzG0tZaqeYitLAq+dXz3amb3IujtttfzFJeoe3Ef3KruUMMmhDenuYZrqyaTd7lqbIE/tuue/C3Un4xrFW65oBDGY0NxJcRzGhE0jH93TV9/vYKYU9pTuvau+Jnzx1IyQkai4j6TW7PWAsz6mee/LvHlLVEN8h/z8gax8z7rzivsmqGy//h9pZPRLt+o8dIv44HBRNfyTKDQyw+3eJmQkTbz1m4KNgS/ioYZx2z4U9pZS1bfoVrEMobZ0kK00+3fuiNVokShw3kQGcpD/rXntekcfBxcz2h7c1U59BwZp/LDl/Yb7769WYlRdgGneHVjh0G+pFhSeBZ3UTw0jmi6Y3BgE0m57uMMF+Kt8VCadN+/9PqiI+fSIVwQ/IcRmtyhfQ27YbnrYT19jqxvMXR3qOAQ8hORs2SfFPqrqLX1dLpMlik6l88BuU3yx7e6UGp5Z7cGCey4/y3gE4SYckToETdB/ZjZw2FBrJtUm0xZgZprOvsprB529IdxlQQQq7gYDqZpX KHTxVKP6 FsXmZXUf0cQJvQXveO+DvXxtBteKhEEGXV6bnffyLBLUrjDzilD+7320FmTTpeVFKF2jY X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Currently, stack depot uses the following locking scheme: 1. Lock-free accesses when looking up a stack record, which allows to have multiple users to look up records in parallel; 2. Spinlock for protecting the stack depot pools and the hash table when adding a new record. For implementing the eviction of stack traces from stack depot, the lock-free approach is not going to work anymore, as we will need to be able to also remove records from the hash table. Convert the spinlock into a read/write lock, and drop the atomic accesses, as they are no longer required. Looking up stack traces is now protected by the read lock and adding new records - by the write lock. One of the following patches will add a new function for evicting stack records, which will be protected by the write lock as well. With this change, multiple users can still look up records in parallel. This is preparatory patch for implementing the eviction of stack records from the stack depot. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov Reviewed-by: Oscar Salvador Signed-off-by: Marco Elver Signed-off-by: Marco Elver --- Changed v2->v3: - Use lockdep_assert_held_read annotation in depot_fetch_stack. Changes v1->v2: - Add lockdep_assert annotations. --- lib/stackdepot.c | 87 +++++++++++++++++++++++++----------------------- 1 file changed, 46 insertions(+), 41 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index a5eff165c0d5..8378b32b5310 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) { @@ -232,6 +233,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; @@ -254,22 +257,17 @@ static void depot_init_pool(void *pool) /* Save reference to the pool to be used by depot_fetch_stack(). */ stack_pools[pools_num] = pool; - - /* - * WRITE_ONCE() pairs with potential concurrent read in - * depot_fetch_stack(). - */ - WRITE_ONCE(pools_num, pools_num + 1); + pools_num++; } /* Keeps the preallocated memory to be used for a new stack depot pool. */ static void depot_keep_new_pool(void **prealloc) { + lockdep_assert_held_write(&pool_rwlock); + /* * If a new pool is already saved or the maximum number of * pools is reached, do not use the preallocated memory. - * Access new_pool_required non-atomically, as there are no concurrent - * write accesses to this variable. */ if (!new_pool_required) return; @@ -287,15 +285,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 references 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; @@ -307,7 +305,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; @@ -341,6 +339,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; @@ -376,18 +376,15 @@ depot_alloc_stack(unsigned long *entries, int size, u32 hash, void **prealloc) static struct stack_record *depot_fetch_stack(depot_stack_handle_t handle) { union handle_parts parts = { .handle = handle }; - /* - * READ_ONCE() pairs with potential concurrent write in - * depot_init_pool(). - */ - int pools_num_cached = READ_ONCE(pools_num); void *pool; size_t offset = parts.offset << DEPOT_STACK_ALIGN; struct stack_record *stack; - if (parts.pool_index > pools_num_cached) { + lockdep_assert_held_read(&pool_rwlock); + + if (parts.pool_index > pools_num) { WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n", - parts.pool_index, pools_num_cached, handle); + parts.pool_index, pools_num, handle); return NULL; } @@ -429,6 +426,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 && @@ -446,6 +445,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; @@ -465,22 +465,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 @@ -494,7 +498,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) { @@ -503,11 +507,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) { @@ -518,7 +518,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. */ @@ -542,6 +542,7 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, unsigned long **entries) { struct stack_record *stack; + unsigned long flags; *entries = NULL; /* @@ -553,8 +554,12 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, if (!handle || stack_depot_disabled) return 0; + read_lock_irqsave(&pool_rwlock, flags); + stack = depot_fetch_stack(handle); + read_unlock_irqrestore(&pool_rwlock, flags); + *entries = stack->entries; return stack->size; } From patchwork Mon Nov 20 17:47:11 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: 13461827 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 D412DC2BB3F for ; Mon, 20 Nov 2023 17:49:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 080576B0362; Mon, 20 Nov 2023 12:49:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0083D6B0363; Mon, 20 Nov 2023 12:49:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DC4046B0365; Mon, 20 Nov 2023 12:49:41 -0500 (EST) 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 C65F06B0362 for ; Mon, 20 Nov 2023 12:49:41 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 9267916090C for ; Mon, 20 Nov 2023 17:49:41 +0000 (UTC) X-FDA: 81479070162.28.23962C1 Received: from out-176.mta0.migadu.com (out-176.mta0.migadu.com [91.218.175.176]) by imf08.hostedemail.com (Postfix) with ESMTP id C051D160011 for ; Mon, 20 Nov 2023 17:49:39 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Ru67pJEW; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf08.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.176 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=1700502579; 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=JJPmOktxCpdhFxDsq8sCddRT3Qn7jxR2DhkERqO3NMk=; b=MxgjFzzH1x9QygRBksjjRbVNHtEuon2KAcgsJ3IHilqliiYTQPjB1L19y2DveqCtpa6Mg+ 1doO4jJZosLChNOsK06h0zLj0mc+6tpV7sc62UxQi5LoA3086NQkstfYn1zQVAXAm2Zr9C 7R/gEN1jmVOtAdURo0HSGIe1Dgr36iE= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=Ru67pJEW; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf08.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.176 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502579; a=rsa-sha256; cv=none; b=bIIy/w+EWg1UXUdnc1BUXUimalCq7UcEtqVSmacKU0uI2UvWtd+8E1NYObCwVUp57eoRZC B9Vk8dYWSJ3Nzw0ALRthBd0dcqswqIg7peWYyH0NDhCLTtM3BodoVtJj6Kv06clTp/8A6Q GGtzgOwo31BnZe9KCL8ItcA2wWNU+wM= 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=1700502578; 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=JJPmOktxCpdhFxDsq8sCddRT3Qn7jxR2DhkERqO3NMk=; b=Ru67pJEW7nzqb9rpO9yuZYxURTiycM1daQ55M4avqe1G86/dIuZUOw3dlKb408oXGg6x5O JLJSygolUyzo1EhZYq8z5/W03AxWgm/1aMH2K0UPV7/E3PTSKcGiP2gUzwgoDhjawwRIqO osUPMvkhvTwmI4K9sg+oiHZAmIKPW1E= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 13/22] lib/stackdepot: use list_head for stack record links Date: Mon, 20 Nov 2023 18:47:11 +0100 Message-Id: <4787d9a584cd33433d9ee1846b17fa3d3e1987ad.1700502145.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: C051D160011 X-Stat-Signature: amtr18pgurg7y51hrw7ayxnyatuktekj X-HE-Tag: 1700502579-26770 X-HE-Meta: U2FsdGVkX181DPf7fgc+K3TteGmB5XSUmcRjQOLOogM6o8qr5SvhCv2gdgFDfJJB4y7p7pQFsBHp0SYdtxwague6hJxyGiPgSqtn9FEQ16/xfsnY6ocpibD7+12xdd9mqfDkZbwsXaPp/6tsswWgp2LgHxo5an2Lbxr4UDWieDnkxXSE3zRdwg9qKyjw/nq2nGfChP3TatuftYfxPfVFg101z4gI41w6Shb2TIeiOJcK9TAR1T3I/i3Hm7/ophH+9Zl0hrfJ2QhqiDm0sOeRRekGsca1/KuKaNNxodFl7jksERF+1x3F0VSP/TpwKQfjSPBnZsJiQ7nCTm5Dzd0nobJ2jF3EXQ8n5WqMYd+pz3Ui1oYRPWTTar/u3lR7HX9By7jbgMcmUdFRag7ZID2Zr98bzMm2E3gay4u4Vqr10fiSvtr8gJKlm06URpO8RIjbbxymReAgK9gUvd3ESaofPXQHxsdSloG+Storgp2fv2OqX2mrVXLjaOErYJmUyA3wdyzU5xICEs23dYIH5/m+dsM86svPulCXeRerUrdBwAfc3NUCuJ407tOyvRU/iUcPh4m36Hp7NT/Lg6U7L6mBZNHv1rk1WHuy7+RSOKcYk7HQzi9O7V5pxzHIDHe55TVwtdvkZafLER5JnHiyQy0clkEoI7HGcTRGfANRRe0luYmYu2z0Ax6ZPEfUoc1ZGWql99NdJuZd/XkOKyV4CFjeDiIThwoxa+ZEQSpN2I1VSqpLsvnrhPGAQbB3ccGob5UJmQtxVU6n5lFHCM2DqULu5WPDJP8281PauYkBS0imrgaNiquvKU8urt3fXOWAorC+W4v6ORAUkZpSIEyoolHPAubasLZ8yzt4oMmxDPUdBRDt3GVWtKKmVCa2xMR+TsrK9aATSAcZFdYIr6qFXIlCSqyX0fx5JLW0beZjVWe/dATTgQQDAlL7Fz4GRl4QQLpKeGlzpoEhiChXEnE5q01 9cYbL3dR wAM9i7XlwFKATtMCGHzsOgxkIxIu8AF1T+G5E8npsOX5G8qhkkjE/LLqKnPes7SJwx0JdPLViNS5riECNECoUlqHEz7fCNECx9k3oyb8INiIhUMSgxRD9S8rtmg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Switch stack_record to use list_head for links in the hash table and in the freelist. This will allow removing entries from the hash table buckets. This is preparatory patch for implementing the eviction of stack records from the stack depot. Signed-off-by: Andrey Konovalov --- Changes v2->v3: - Use the proper number of entries for initializing the stack table when alloc_large_system_hash() auto-calculates the number. Changes v1->v2: - Use list_head instead of open-coding backward links. --- lib/stackdepot.c | 87 ++++++++++++++++++++++++++++-------------------- 1 file changed, 50 insertions(+), 37 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 8378b32b5310..4bb0af423f82 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 @@ -116,6 +117,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,16 +162,16 @@ int __init stack_depot_early_init(void) /* * If stack_bucket_number_order is not set, leave entries as 0 to rely - * on the automatic calculations performed by alloc_large_system_hash. + * on the automatic calculations performed by alloc_large_system_hash(). */ if (stack_bucket_number_order) entries = 1UL << stack_bucket_number_order; pr_info("allocating hash table via alloc_large_system_hash\n"); stack_table = alloc_large_system_hash("stackdepot", - sizeof(struct stack_record *), + sizeof(struct list_head), entries, STACK_HASH_TABLE_SCALE, - HASH_EARLY | HASH_ZERO, + HASH_EARLY, NULL, &stack_hash_mask, 1UL << STACK_BUCKET_NUMBER_ORDER_MIN, @@ -171,6 +181,14 @@ int __init stack_depot_early_init(void) stack_depot_disabled = true; return -ENOMEM; } + if (!entries) { + /* + * Obtain the number of entries that was calculated by + * alloc_large_system_hash(). + */ + entries = stack_hash_mask + 1; + } + init_stack_table(entries); return 0; } @@ -211,7 +229,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; @@ -219,6 +237,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); @@ -230,31 +249,24 @@ EXPORT_SYMBOL_GPL(stack_depot_init); /* Initializes a stack depol pool. */ static void depot_init_pool(void *pool) { - const int records_in_pool = DEPOT_POOL_SIZE / DEPOT_STACK_RECORD_SIZE; - int i, offset; + int offset; lockdep_assert_held_write(&pool_rwlock); - /* Initialize handles and link stack records to each other. */ - for (i = 0, offset = 0; - offset <= DEPOT_POOL_SIZE - DEPOT_STACK_RECORD_SIZE; - i++, offset += DEPOT_STACK_RECORD_SIZE) { + WARN_ON(!list_empty(&free_stacks)); + + /* Initialize handles and link stack records into the freelist. */ + for (offset = 0; offset <= DEPOT_POOL_SIZE - DEPOT_STACK_RECORD_SIZE; + offset += DEPOT_STACK_RECORD_SIZE) { struct stack_record *stack = pool + offset; stack->handle.pool_index = pools_num; stack->handle.offset = offset >> DEPOT_STACK_ALIGN; stack->handle.extra = 0; - if (i < records_in_pool - 1) - stack->next = (void *)stack + DEPOT_STACK_RECORD_SIZE; - else - stack->next = NULL; + list_add(&stack->list, &free_stacks); } - /* Link stack records into the freelist. */ - WARN_ON(next_stack); - next_stack = pool; - /* Save reference to the pool to be used by depot_fetch_stack(). */ stack_pools[pools_num] = pool; pools_num++; @@ -295,7 +307,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. */ @@ -346,19 +358,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(). */ @@ -420,15 +431,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)) @@ -441,7 +454,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; @@ -468,7 +482,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; @@ -500,14 +514,13 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, write_lock_irqsave(&pool_rwlock, flags); - found = find_stack(*bucket, entries, nr_entries, hash); + found = find_stack(bucket, entries, nr_entries, hash); if (!found) { struct stack_record *new = depot_alloc_stack(entries, nr_entries, hash, &prealloc); if (new) { - new->next = *bucket; - *bucket = new; + list_add(&new->list, bucket); found = new; } } else if (prealloc) { From patchwork Mon Nov 20 17:47:12 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: 13461828 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 C1DC4C5AD4C for ; Mon, 20 Nov 2023 17:49:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8706D6B0363; Mon, 20 Nov 2023 12:49:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7D0636B0365; Mon, 20 Nov 2023 12:49:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 622A46B0368; Mon, 20 Nov 2023 12:49:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 4E1B46B0363 for ; Mon, 20 Nov 2023 12:49:42 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 1FD4840899 for ; Mon, 20 Nov 2023 17:49:42 +0000 (UTC) X-FDA: 81479070204.23.817BC4E Received: from out-188.mta0.migadu.com (out-188.mta0.migadu.com [91.218.175.188]) by imf21.hostedemail.com (Postfix) with ESMTP id 509DE1C0007 for ; Mon, 20 Nov 2023 17:49:40 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=b2OIEP3B; spf=pass (imf21.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.188 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=1700502580; a=rsa-sha256; cv=none; b=0tzACFiwETj+bxEhDaIVfEqZV2G9+Zn/gRlNSdsSaBwXfyybDfZwvdJLM0MuLJoPig7lPA OhkSySNupy6C81kif1wCn2iTsLvdj/9bKaiMZjX+/FNkLef+/hEp8F+1+BaXKOKkaEaiuF R8x/sLAdnV/qDVNXbst/RXTopjwBgTA= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=b2OIEP3B; spf=pass (imf21.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.188 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=1700502580; 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=AmubiAZbFJ0d/i3HqBc+VhVD2T8fru9oX5DWKdU3rlA=; b=F2MRa3ji8Ro1Cm4q1qC4mEu0FT9SOAmR46cZ+x4T3HyWZw/AKR6BTdakANP4e2Jt+/OpHw UcjH0+JDaLm4PfkwBMXiMj3gc/A0J3MQ/0cGBDx/KukUuk63cYDBXLi8/eCD3rxau1iYpI bssev72YFQelrgVQiZlBMSDZIFcMCVE= 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=1700502578; 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=AmubiAZbFJ0d/i3HqBc+VhVD2T8fru9oX5DWKdU3rlA=; b=b2OIEP3BJYVimjqmep3gX4pnHU69uhP3rz4BbIU0wDtcOHhahRQdj/mDShPRiA2HDFVZhX ObfN7eBTcN91SgmffyLxHsXabiv3RbporAg2HdC9O1DTTM3xtlUsATOXQ2XYASwgncbBwR 7K4umBHx7gSfcQI4eOh8RKYS2iCumFg= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 14/22] kmsan: use stack_depot_save instead of __stack_depot_save Date: Mon, 20 Nov 2023 18:47:12 +0100 Message-Id: <18092240699efdc6acd78b51e41ea782953e6c8d.1700502145.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 509DE1C0007 X-Stat-Signature: t366iifjugnia7fojzmkhpa1op8g3ik3 X-Rspam-User: X-HE-Tag: 1700502580-883497 X-HE-Meta: U2FsdGVkX1+YGFUy6bn6FADHq2QHZVL6MbE5mlYORhAdqVABiogKbAXTtqdgvaduWkU7Xl2fze5gwyRPhQvqEkgCLdotzl9ZFv56smcClJw6YqobYtQBQZ9sDb/z8GyHocjgfiMB89E+RKFdVxphafAyEOPzKzK8U0Xjlp3mv/mKbnCcD7akGnuYr9jr6gBJC9WETN5qnvLo712+BvKqwrnC0kbr0CRTKtJ6Igtlzwu2/1D2J08ejPofmfqRk0YxuqsQJMYv5IJbwcGlVMtV+0XrkKXt95idRwEepbJjml7TWZH6lfyfApF6j6ai1psgupLCr1vR+VGrVt7e/WKgGtTiaTjGXVXKmgZwpoekqktODSauUdlfedq82T8mDWwMmzv4YiEGDcv6CzqNv+vq3mCoU1LA1SrzmYTKMG3Qzcuz03FaUHw6FY8QZXjFEZ7rC0PytXFsqJMf4MnVfY7SbulG9ANSSRRplP/W2iArmtgKegWMeafT1Z8shpIP8YKW+OLaThyXJguui9v78hq2c6En/39nglR6mwAsa0GpeQvfNmk4GBIl4wPJKDsmaB2BcQ/eqNzx0tw8bmaAn/8g5NkAOarKi3pmfgYGGfPONk7ad/PGTKtMTsCUiJJcGQDiNI4hTzBQ1X2oBPPw3iPaBD+hUwzJmC8wO2wQ3vYszp+lI9iHPE8oVbUdPt6aU0xEf+MmCgh0M0WS7/cNdZBY3dDzkSPKvS4BOdf336L8ZzwraP42IOlA2VCKXpxuKKb1R5KeYul192XrTmki1RRYdRU1mvTAKjT5aRNw7RSRefqyyxcS22R/dbyWltwlnqk4M8viwcrY0+KfhNT/yjeQS6CUP+6CPnunbS5GyNzHxgcphAPOR4exC5zVEeBFiaWQbc8fYiDppYDRlduLg0y62lL27jnq2BpVAKOuc/R5G6bCcgF1jgpTft81O5evV8fZGjyfeogmcQNe+Ct215D v2yooeiH mR159ggWQ/kRhygYjJJEPZ4XD7YBiOWg+VQgo/AEee44LrAU1+LGdToBEUK6SyC1gD9MzdUztZHoAE3cLi8VDUhirQI3AF/v0odYnf8olzqfJBi4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Make KMSAN use stack_depot_save instead of __stack_depot_save, as it always passes true to __stack_depot_save as the last argument. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- Changes v1->v2: - This is a new patch. --- mm/kmsan/core.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/mm/kmsan/core.c b/mm/kmsan/core.c index c19f47af0424..cf2d70e9c9a5 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); } @@ -185,11 +185,10 @@ depot_stack_handle_t kmsan_internal_chain_origin(depot_stack_handle_t id) /* * @entries is a local var in non-instrumented code, so KMSAN does not * know it is initialized. Explicitly unpoison it to avoid false - * positives when __stack_depot_save() passes it to instrumented code. + * positives when stack_depot_save() passes it to instrumented code. */ kmsan_internal_unpoison_memory(entries, sizeof(entries), false); - handle = __stack_depot_save(entries, ARRAY_SIZE(entries), __GFP_HIGH, - true); + handle = stack_depot_save(entries, ARRAY_SIZE(entries), __GFP_HIGH); return stack_depot_set_extra_bits(handle, extra_bits); } From patchwork Mon Nov 20 17:47:13 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: 13461829 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 6E018C5AE5B for ; Mon, 20 Nov 2023 17:49:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8BE9F6B0368; Mon, 20 Nov 2023 12:49:43 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 820FE6B0370; Mon, 20 Nov 2023 12:49:43 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 561D66B036B; Mon, 20 Nov 2023 12:49:43 -0500 (EST) 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 397A36B0365 for ; Mon, 20 Nov 2023 12:49:43 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 04F45C027C for ; Mon, 20 Nov 2023 17:49:42 +0000 (UTC) X-FDA: 81479070246.12.BAB31F5 Received: from out-186.mta0.migadu.com (out-186.mta0.migadu.com [91.218.175.186]) by imf24.hostedemail.com (Postfix) with ESMTP id 1A559180021 for ; Mon, 20 Nov 2023 17:49:40 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=HqbyNSHD; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf24.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.186 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502581; a=rsa-sha256; cv=none; b=tNVFM/MvlMnpxbstwyDrHFrKgkyImTMTUrECocH78vlSDnzTznMClCLPGVxLWrnaRPJaFn BTHtFuYXQc+ixFwGGPq1Jcm8t127E402r7vXkNGFmGgkolwPIilHTve9IcengejSd3p+Se g89fNL+17FlYgMKxJ/hZBWwYRTCjVVw= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=HqbyNSHD; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf24.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.186 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=1700502581; 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=aXTF/ovgO7tSDGaC/rtVyVWMqky+xGAg2LPsWIiIw38=; b=lMMxYlDn0DJRiuO6AstULmX+k4n+iZv9rPzWk960RyBpBARUOxEBFcjkAPH871/tW81HzV 2ou0cTazGACq2SmK+qNgPhYZXyeq2lryeCr23OA133C/03I1JlBQL2oD+UD7wLwpZ3U2iL bNPLHHdve45CUGNAb/ew0zVDaObJbX4= 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=1700502579; 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=aXTF/ovgO7tSDGaC/rtVyVWMqky+xGAg2LPsWIiIw38=; b=HqbyNSHD4r9vYA816UIOJaXgIqcjElz6erC2e41iS8jUsO6pNaIMU40yawD+G8CERk8/mB qdv7+Ypo8jZ0ZcLL7hUCGbmu0MDX0b6jRsskfd3hDjPvU8Dgx5Nmli1NgQFC70MO8T/FHR /N3gcwtihjoZvi89Omg8LoW5wxj9aNk= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 15/22] lib/stackdepot, kasan: add flags to __stack_depot_save and rename Date: Mon, 20 Nov 2023 18:47:13 +0100 Message-Id: <645fa15239621eebbd3a10331e5864b718839512.1700502145.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 1A559180021 X-Stat-Signature: xphxtzs3kcpsdo53ewgmou4bsih4mpu8 X-HE-Tag: 1700502580-99775 X-HE-Meta: U2FsdGVkX19cvRuZV9YWKbnxlFfCdpkEhWOuxLSB0p/v4JRcVlQz7WuOPDWRuD2/87vVhB6wZklAxx82PdlrE/18zA6sH9CNdC15Y/0Vd1aj9g18RKxQJdTf9ZYgvSLd1A9/jER+Xsg6zuSJ0wrQGa8rM37/+1gX937dg3y5GUFeu4T0V2aEnJ3DvjbzuclVH+l2bZz4FpEuBs6oRsMG89G+CvLVQdOAffs4odrPquT33DdRpsqkqCGDOEfc86BLdn5vJbw/fu+kOqG07lc1IfZKjVbv2ed2ldXY6qFgqqyllW0l/sbvKtl8z7uYC2o93tLxM9WV2B/s3cc+O2hW0BquP9BOji5h++GpnpgF8fhA3DY9eOAiR+OUWdgruo4MU1G93aJ16LvvTL7IgVvsVHQv5w46geKO2s6XsFi5FU4rz5tD0yJa6KVKyX5pMxsjMqADlHEIRVMjZd1GPRUWuRvsM6wVGc6gSc+pJ7OKGl/Ql52FmCJhKdk8rSxZ5OdkQFwn2khMxcEqSx5PXg4f5UCrXWyLXcM2Nv2h9zlXYrP/z8S/pWj2M33BYG+Z1v7ATual3VYvQS/TVTqsgM9npAuRgC7krGzFO8Ka2OvZXz/7qFXs9R/J1mAIRtaRBzJsKWi/1YmHA0hUlY7+RzYLKjdsag7DVw0pIFCizx7Ee8NXpfbHjUkZ/PsJtkKccMW7ftp5DpCKT/omA48mPLWgH18fxTAWwjmHKEuEbPS/P6xLRSybwGahpvBWmcgLEnUyrMu8ux9HnH1YFk93ogx8g92vQkUYkHF5wqP5Smo1mwR6cT8hth/wTWM4mlLooMEmKVXctkgXlenRlAHn2oia+pgcrxxYSHs8/X/Gyb5T135k3+uJu2aaovQ9bmbL/+WY7swvp8yBV9jOcVeqvoENGJZ0sLMAFCcofybh8sV4g5xQ6xFPlcYNvTOckpVzAu4wZ5/6epF/vABqXc1ccwL OuD0nMEL ywFpeclEK7ZnKh0fKYHgUypNOKvr04m25KDm9qKlmKHM01dtxUoRjWy0zqQrPEXdapcpUXz29Jypun440bGjbGw3cynE+LmWAz87A0MRpcShUJtY= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Change the bool can_alloc argument of __stack_depot_save to a u32 argument that accepts a set of flags. The following patch will add another flag to stack_depot_save_flags besides the existing STACK_DEPOT_FLAG_CAN_ALLOC. Also rename the function to stack_depot_save_flags, as __stack_depot_save is a cryptic name, Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- Changes v2->v3: - WARN_ON invalid flags in stack_depot_save_flags. Changes v1->v2: - This is a new patch. --- include/linux/stackdepot.h | 36 +++++++++++++++++++++++++----------- lib/stackdepot.c | 16 +++++++++++----- mm/kasan/common.c | 7 ++++--- mm/kasan/generic.c | 9 +++++---- mm/kasan/kasan.h | 2 +- mm/kasan/tags.c | 3 ++- 6 files changed, 48 insertions(+), 25 deletions(-) diff --git a/include/linux/stackdepot.h b/include/linux/stackdepot.h index e58306783d8e..0b262e14144e 100644 --- a/include/linux/stackdepot.h +++ b/include/linux/stackdepot.h @@ -32,6 +32,17 @@ typedef u32 depot_stack_handle_t; */ #define STACK_DEPOT_EXTRA_BITS 5 +typedef u32 depot_flags_t; + +/* + * Flags that can be passed to stack_depot_save_flags(); see the comment next + * to its declaration for more details. + */ +#define STACK_DEPOT_FLAG_CAN_ALLOC ((depot_flags_t)0x0001) + +#define STACK_DEPOT_FLAGS_NUM 1 +#define STACK_DEPOT_FLAGS_MASK ((depot_flags_t)((1 << STACK_DEPOT_FLAGS_NUM) - 1)) + /* * Using stack depot requires its initialization, which can be done in 3 ways: * @@ -69,31 +80,34 @@ static inline int stack_depot_early_init(void) { return 0; } #endif /** - * __stack_depot_save - Save a stack trace to stack depot + * stack_depot_save_flags - Save a stack trace to stack depot * * @entries: Pointer to the stack trace * @nr_entries: Number of frames in the stack * @alloc_flags: Allocation GFP flags - * @can_alloc: Allocate stack pools (increased chance of failure if false) + * @depot_flags: Stack depot flags + * + * Saves a stack trace from @entries array of size @nr_entries. * - * Saves a stack trace from @entries array of size @nr_entries. If @can_alloc is - * %true, stack depot can replenish the stack pools in case no space is left - * (allocates using GFP flags of @alloc_flags). If @can_alloc is %false, avoids - * any allocations and fails if no space is left to store the stack trace. + * If STACK_DEPOT_FLAG_CAN_ALLOC is set in @depot_flags, stack depot can + * replenish the stack pools in case no space is left (allocates using GFP + * flags of @alloc_flags). Otherwise, stack depot avoids any allocations and + * fails if no space is left to store the stack trace. * * If the provided stack trace comes from the interrupt context, only the part * up to the interrupt entry is saved. * - * Context: Any context, but setting @can_alloc to %false is required if + * Context: Any context, but setting STACK_DEPOT_FLAG_CAN_ALLOC is required if * alloc_pages() cannot be used from the current context. Currently * this is the case for contexts where neither %GFP_ATOMIC nor * %GFP_NOWAIT can be used (NMI, raw_spin_lock). * * Return: Handle of the stack struct stored in depot, 0 on failure */ -depot_stack_handle_t __stack_depot_save(unsigned long *entries, - unsigned int nr_entries, - gfp_t gfp_flags, bool can_alloc); +depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, + unsigned int nr_entries, + gfp_t gfp_flags, + depot_flags_t depot_flags); /** * stack_depot_save - Save a stack trace to stack depot @@ -103,7 +117,7 @@ depot_stack_handle_t __stack_depot_save(unsigned long *entries, * @alloc_flags: Allocation GFP flags * * Context: Contexts where allocations via alloc_pages() are allowed. - * See __stack_depot_save() for more details. + * See stack_depot_save_flags() for more details. * * Return: Handle of the stack trace stored in depot, 0 on failure */ diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 4bb0af423f82..59d61d5c09a7 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -450,19 +450,24 @@ static inline struct stack_record *find_stack(struct list_head *bucket, return NULL; } -depot_stack_handle_t __stack_depot_save(unsigned long *entries, - unsigned int nr_entries, - gfp_t alloc_flags, bool can_alloc) +depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, + unsigned int nr_entries, + gfp_t alloc_flags, + depot_flags_t depot_flags) { struct list_head *bucket; struct stack_record *found = NULL; depot_stack_handle_t handle = 0; struct page *page = NULL; void *prealloc = NULL; + bool can_alloc = depot_flags & STACK_DEPOT_FLAG_CAN_ALLOC; bool need_alloc = false; unsigned long flags; u32 hash; + if (WARN_ON(depot_flags & ~STACK_DEPOT_FLAGS_MASK)) + return 0; + /* * If this stack trace is from an interrupt, including anything before * interrupt entry usually leads to unbounded stack depot growth. @@ -541,13 +546,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 8b06bab5c406..b29d46b83d1f 100644 --- a/mm/kasan/kasan.h +++ b/mm/kasan/kasan.h @@ -368,7 +368,7 @@ static inline void kasan_init_cache_meta(struct kmem_cache *cache, unsigned int static inline void kasan_init_object_meta(struct kmem_cache *cache, const void *object) { } #endif -depot_stack_handle_t kasan_save_stack(gfp_t flags, bool can_alloc); +depot_stack_handle_t kasan_save_stack(gfp_t flags, depot_flags_t depot_flags); void kasan_set_track(struct kasan_track *track, gfp_t flags); void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags); void kasan_save_free_info(struct kmem_cache *cache, void *object); diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index 7dcfe341d48e..4fd32121b0fd 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -101,7 +102,7 @@ static void save_stack_info(struct kmem_cache *cache, void *object, struct kasan_stack_ring_entry *entry; void *old_ptr; - stack = kasan_save_stack(gfp_flags, true); + stack = kasan_save_stack(gfp_flags, STACK_DEPOT_FLAG_CAN_ALLOC); /* * Prevent save_stack_info() from modifying stack ring From patchwork Mon Nov 20 17:47:14 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: 13461830 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 93999C197A0 for ; Mon, 20 Nov 2023 17:49:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1B89A6B036B; Mon, 20 Nov 2023 12:49:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 11A8B6B0370; Mon, 20 Nov 2023 12:49:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DC47A6B0374; Mon, 20 Nov 2023 12:49:43 -0500 (EST) 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 C59D76B036B for ; Mon, 20 Nov 2023 12:49:43 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 95D9B1CADE7 for ; Mon, 20 Nov 2023 17:49:43 +0000 (UTC) X-FDA: 81479070246.06.AC9E8E6 Received: from out-173.mta0.migadu.com (out-173.mta0.migadu.com [91.218.175.173]) by imf04.hostedemail.com (Postfix) with ESMTP id 9902B40027 for ; Mon, 20 Nov 2023 17:49:41 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="rZ/C2Hcs"; spf=pass (imf04.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.173 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=1700502581; 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=91QxaYvk4RHHEJwI4Sc7Oo7z2rlFVPgOoXOer3eAY+g=; b=59NqEnXIxqmDsvqgo//ebEPhfTWVpBP6RaYiI+NvmqedhyF9fZw0n+VwVCM/3jHkU7Qaly zsbnleyzIdQQHe3TnSgW0xe7VrqYHc9J2JmFcYZUmrK6nvZ9m8BbJOiTUNgTkBkbqef+2o gsicMMLgmA+NPE6SVwMD8/fvQvuE3eU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502581; a=rsa-sha256; cv=none; b=ZJ/GN85JjtvuqAMPytg8XrEL3REGUitYSzW5vh4fmSnnxKojSD03LIfbA760nDcVU6UAqf g9OMgrQUJmMore8FfQIn7vmCvNHll6v2YV3lFmbJigZ9JZilpkw6Q08scG+RV/WDhieE6s YuzCYnv2q7DNwpJI4V2MG8U+jREojNY= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="rZ/C2Hcs"; spf=pass (imf04.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.173 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=1700502580; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=91QxaYvk4RHHEJwI4Sc7Oo7z2rlFVPgOoXOer3eAY+g=; b=rZ/C2HcsNzvMwpX5O9VOx1lNSC2GHR0kSTsgEzMvFKq6y+hKBKp3/9Ua+zCoBCJmYeieov wMbZyfFLCbQY6lOshDlPQuKUW5//srMlo9hBj2CFZiAjPs39TBjkS5qlASK6p+8fCOmgfm q8MQh7TtmnvvOGnA2P/0s6mRGFe8vjU= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 16/22] lib/stackdepot: add refcount for records Date: Mon, 20 Nov 2023 18:47:14 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 9902B40027 X-Rspam-User: X-Stat-Signature: jirhg1azoyzpfrq8rupa18sde78br1sf X-Rspamd-Server: rspam03 X-HE-Tag: 1700502581-631065 X-HE-Meta: U2FsdGVkX1+bS/x+3cRLuADBlCCIFX1ULQR7lNm8Uv24vmbs/ka/Iqe5KByHsBZGUldCfdI5YvZXukw6k69Gyck5LPNBlWgrwPhGpUZcsN/GPNNSNXVCzKfDcNNi1BkJ+9vj+epRx5PckJjl6+G5O37FRSreqFfiP6O23tw4e4yB9yRYqoDwwSbyzHRAqUBe+DK2WeQgigI9fwGCi4DP1o86oXCRQqqEJzI27usih7zwAUTmXVkKpjaxxq0EkcWZJNVt+eVJNKuwGWNDaZKur9WhN0wV72UJdkgEvmsmxYhylXrw5lP3/tu5cb12f4df17vwPQwN4vcx3qOVviK51ZBqdtL7LyaD/aSq2s8WAslz5WdzIKJ0jc1KRZE+5VhpnLTfrr4/KjmFsMWx069ur0KWd4VC6yHeO0Da58w2fL7H5qfCgiSUnKXpiaf680AJt2U85UbZe6NffHAnlnnZNJN09E4RaPzDJTvydCMXC0h9HoSTzv0VzR5mujDor0unw1ptCrn758kYMlKxZI/DjV+EXT5ivvA95nLWNEak0AmVeyXrL8Qsc1XxXoS95QfGjlwumJYG3+88CrC1si+2MzI1eQG9PdLK87ihrD07MrGuxFUeBPAZNFLApg/gbIRf4pv8USMDrP96f+pO28Hmo08wepgvTTF278ZE3NRYoB+Pv2GAytqevpTrmBcASYQbJBrvqn7oZG6Jqwl3wU5XuT2NGK7WUxlrRelWPe5J4BjPR1TMnanu6iODTcoq0xDhY6fC91EtvgVbKBzMvV6hA1LXbhhoOMQ5cYIQVZwEaufWt9ublSxX4hY9XS9s8RLvgZJ+72d6X7q3jHD9LlGlevo0OkGY82VfE+GHzfEobfEP3sS5RGCCVFePYb/alDZgRvFhogcUIdcBIc/obbb9VtQAQV+yLsLkk9xNYTzWCGp+yz55puKUaOvDJMjL1S15KepM54dZdEh2vTtfG9V /6G/9ibV 0/E6nE8yW7xYW0J7bMqFqHGI1Fi3b2AVnrSg25l2noIRb+RjcPdvtzH2VLw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Add a reference counter for how many times a stack records has been added to stack depot. Add a new STACK_DEPOT_FLAG_GET flag to stack_depot_save_flags that instructs the stack depot to increment the refcount. Do not yet decrement the refcount; this is implemented in one of the following patches. Do not yet enable any users to use the flag to avoid overflowing the refcount. This is preparatory patch for implementing the eviction of stack records from the stack depot. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- Changes v1->v2: - Add forgotten refcount_inc() under write lock. - Add STACK_DEPOT_FLAG_GET flag for stack_depot_save_flags. --- include/linux/stackdepot.h | 13 ++++++++++--- lib/stackdepot.c | 12 ++++++++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/include/linux/stackdepot.h b/include/linux/stackdepot.h index 0b262e14144e..611716702d73 100644 --- a/include/linux/stackdepot.h +++ b/include/linux/stackdepot.h @@ -39,8 +39,9 @@ typedef u32 depot_flags_t; * to its declaration for more details. */ #define STACK_DEPOT_FLAG_CAN_ALLOC ((depot_flags_t)0x0001) +#define STACK_DEPOT_FLAG_GET ((depot_flags_t)0x0002) -#define STACK_DEPOT_FLAGS_NUM 1 +#define STACK_DEPOT_FLAGS_NUM 2 #define STACK_DEPOT_FLAGS_MASK ((depot_flags_t)((1 << STACK_DEPOT_FLAGS_NUM) - 1)) /* @@ -94,6 +95,9 @@ static inline int stack_depot_early_init(void) { return 0; } * flags of @alloc_flags). Otherwise, stack depot avoids any allocations and * fails if no space is left to store the stack trace. * + * If STACK_DEPOT_FLAG_GET is set in @depot_flags, stack depot will increment + * the refcount on the saved stack trace if it already exists in stack depot. + * * If the provided stack trace comes from the interrupt context, only the part * up to the interrupt entry is saved. * @@ -116,8 +120,11 @@ depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, * @nr_entries: Number of frames in the stack * @alloc_flags: Allocation GFP flags * - * Context: Contexts where allocations via alloc_pages() are allowed. - * See stack_depot_save_flags() for more details. + * Does not increment the refcount on the saved stack trace; see + * stack_depot_save_flags() for more details. + * + * Context: Contexts where allocations via alloc_pages() are allowed; + * see stack_depot_save_flags() for more details. * * Return: Handle of the stack trace stored in depot, 0 on failure */ diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 59d61d5c09a7..911dee11bf39 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 */ }; @@ -373,6 +375,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)); /* @@ -489,6 +492,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; } @@ -528,12 +533,15 @@ depot_stack_handle_t stack_depot_save_flags(unsigned long *entries, list_add(&new->list, bucket); found = new; } - } else if (prealloc) { + } else { + if (depot_flags & STACK_DEPOT_FLAG_GET) + refcount_inc(&found->count); /* * Stack depot already contains this stack trace, but let's * keep the preallocated memory for future. */ - depot_keep_new_pool(&prealloc); + if (prealloc) + depot_keep_new_pool(&prealloc); } write_unlock_irqrestore(&pool_rwlock, flags); From patchwork Mon Nov 20 17:47:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 13461831 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C1188C5AE5C for ; Mon, 20 Nov 2023 17:49:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8BDAA6B0370; Mon, 20 Nov 2023 12:49:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 846326B0374; Mon, 20 Nov 2023 12:49:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 673046B0377; Mon, 20 Nov 2023 12:49:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 4E93F6B0370 for ; Mon, 20 Nov 2023 12:49:44 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 1B488B501E for ; Mon, 20 Nov 2023 17:49:44 +0000 (UTC) X-FDA: 81479070288.28.96DE346 Received: from out-174.mta0.migadu.com (out-174.mta0.migadu.com [91.218.175.174]) by imf21.hostedemail.com (Postfix) with ESMTP id 2D3791C0003 for ; Mon, 20 Nov 2023 17:49:41 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=DLuotPQZ; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf21.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.174 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700502582; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=taQhm6+sqq7C99vuNmST7zKoEFBrlbi2ArfSWbY49ok=; b=h7nhDBgXQXaQWkkyWXP3gsDgBlCl0+JBTh1r1/NoM7pm2IXrHMNOd26VGIiqh1XheCHNiT igUAYuJtCfaOX7wYpOiqjKnb5sP3bGIBHLPX2XmKCRuMfaikS0RnLRwDv6jwhV0V5hP/iW wELZn/uod1XJv87X6Wb7MzF1c/THvK8= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=DLuotPQZ; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf21.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.174 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502582; a=rsa-sha256; cv=none; b=uZeKzud/hIeJRwagy4APXnD+G5E2Xozl9psxEVExf0unsDHNti4a4XtBtuSUKnoSdBzpGC dkgheQwIHH1+VHh79JhjHb+4RJ1vBsuCyVxIaOrLBnsoIUj4ksEpUVhQwGSYcOVJlO+wrH dtZnR5dahLrEnJUdJYoMF0m1JenoKwU= X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1700502580; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=taQhm6+sqq7C99vuNmST7zKoEFBrlbi2ArfSWbY49ok=; b=DLuotPQZ2ya2ze/Uk9qdks6Velp5g/ufNOpAnZ3F0CTG4bBuTmqBJZX+8HrA4Dqfb+h/XH cXG4e43rc7TxqEGmM94O/sylDTL5wKtysxQVD2NH08lAvJQHMJZz91DKB754x+wRKfj68K mQIu1A/BqCWkTls4JakGGQmnxTCxoCg= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 17/22] lib/stackdepot: allow users to evict stack traces Date: Mon, 20 Nov 2023 18:47:15 +0100 Message-Id: <1d1ad5692ee43d4fc2b3fd9d221331d30b36123f.1700502145.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 2D3791C0003 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: 56j9jkke6x946sf1cc7eexgw6gbzb6yj X-HE-Tag: 1700502581-372242 X-HE-Meta: U2FsdGVkX19ejo45ZklWFySp0+XyAZDeFgMrbSEsrOFY2+MMGliMO+lFZpB1VCyK9EWOrZI0SaFZkWiwqoqmkTFlFBhRTFOKKCCPoa1as4VSH/yjUPDrSGBWRZ07l0s0Zds70Z+/RgwrYwW/wY9YnFLqs2+aJVtWaZL4ky8hEQFt+ZvWh80BtbGBMLu+GgBApeDm8NsaGeZaxbPXr5Ai8nveBo+GJJBLL4DMjyCJ9xXWPwCNdfPqCH+QVqHUC9OMKuV+fUAVYzGktGAXL1hgOndmMwpN6DEyOzq6fkkIuHXM6RUk2FHogzOmuP2YyoS1zIOrDQG4Nlg42y/Ed6yJy7Gl8ToMzYq3npIdwxa3mLUo1ASwZejL9/sVVgtUv1sb0RDPMeB7x1dJ2qB1oJ3a28EKbv+EgtT+7eTMgKEU92jG5Y7S0fkhlCR40bxPNVunnA/oHKS0zTj0OSmdypoinSTtttAyB+POa8gCG6L62bGN6eWKbLtghpsADAqS+zKjx9vFrq3hPBaDNp1wILP8tMyPctwZoDRWC6sXQYKOL2esx/zJx3O8CL10+jYMaP7ADKCE++6WGJUpNstmVSZ11ohDL7N0uqaHA/zfxvyXxpyjzAaiaY6Nmw1tt5VJ2RWjr3uYjGA22UwDG5PNey2iksUULVCJXiNdwZQ0pwqksQmZpNeskfyfz6OuN96pzZky3CHc1q5DPPxcJj4rETGprwl1KK5HMjzuyWlaBgNqVQhPydGODlBwPDwd+n8fvxIIDxjHu4BUtxYVXb6tn0I8XEEpeWVwk1/MOPnmJTnV3V2g2RgeTNVE30XJUvJOj4AlI+5CmFxoaVX8k9jBuoCAa9rJGNN+07DWugKO83ndgDSX9fWsvm2TegKGH9M48I/gtk3Yv45C3eVA2aWhb9nroe3wTP0mH7ZjlyblT9OmzxHnU1qhtBDJtUY5E7g8xcrDWL5e2sx2prh1COy2Syo r2ltwQUR fUGBY/T/6KlHI9Njf8BrcnQdxhfW8tbSgnt0rxlI9vHjhB2aeIp27MvZprObTb4sh1xZ4 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Add stack_depot_put, a function that decrements the reference counter on a stack record and removes it from the stack depot once the counter reaches 0. Internally, when removing a stack record, the function unlinks it from the hash table bucket and returns to the freelist. With this change, the users of stack depot can call stack_depot_put when keeping a stack trace in the stack depot is not needed anymore. This allows avoiding polluting the stack depot with irrelevant stack traces and thus have more space to store the relevant ones before the stack depot reaches its capacity. Signed-off-by: Andrey Konovalov --- Changes v1->v2: - Comments fixes as suggested by Marco. - Add lockdep_assert annotation. - Adapt to using list_head's. - Rename stack_depot_evict to stack_depot_put. --- include/linux/stackdepot.h | 14 ++++++++++++++ lib/stackdepot.c | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/include/linux/stackdepot.h b/include/linux/stackdepot.h index 611716702d73..a6796f178913 100644 --- a/include/linux/stackdepot.h +++ b/include/linux/stackdepot.h @@ -97,6 +97,8 @@ static inline int stack_depot_early_init(void) { return 0; } * * If STACK_DEPOT_FLAG_GET is set in @depot_flags, stack depot will increment * the refcount on the saved stack trace if it already exists in stack depot. + * Users of this flag must also call stack_depot_put() when keeping the stack + * trace is no longer required to avoid overflowing the refcount. * * If the provided stack trace comes from the interrupt context, only the part * up to the interrupt entry is saved. @@ -162,6 +164,18 @@ void stack_depot_print(depot_stack_handle_t stack); int stack_depot_snprint(depot_stack_handle_t handle, char *buf, size_t size, int spaces); +/** + * stack_depot_put - Drop a reference to a stack trace from stack depot + * + * @handle: Stack depot handle returned from stack_depot_save() + * + * The stack trace is evicted from stack depot once all references to it have + * been dropped (once the number of stack_depot_evict() calls matches the + * number of stack_depot_save_flags() calls with STACK_DEPOT_FLAG_GET set for + * this stack trace). + */ +void stack_depot_put(depot_stack_handle_t handle); + /** * stack_depot_set_extra_bits - Set extra bits in a stack depot handle * diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 911dee11bf39..c1b31160f4b4 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -394,7 +394,7 @@ static struct stack_record *depot_fetch_stack(depot_stack_handle_t handle) size_t offset = parts.offset << DEPOT_STACK_ALIGN; struct stack_record *stack; - lockdep_assert_held_read(&pool_rwlock); + lockdep_assert_held(&pool_rwlock); if (parts.pool_index > pools_num) { WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n", @@ -410,6 +410,14 @@ static struct stack_record *depot_fetch_stack(depot_stack_handle_t handle) return stack; } +/* Links stack into the freelist. */ +static void depot_free_stack(struct stack_record *stack) +{ + lockdep_assert_held_write(&pool_rwlock); + + list_add(&stack->list, &free_stacks); +} + /* Calculates the hash for a stack. */ static inline u32 hash_stack(unsigned long *entries, unsigned int size) { @@ -592,6 +600,33 @@ unsigned int stack_depot_fetch(depot_stack_handle_t handle, } EXPORT_SYMBOL_GPL(stack_depot_fetch); +void stack_depot_put(depot_stack_handle_t handle) +{ + struct stack_record *stack; + unsigned long flags; + + if (!handle || stack_depot_disabled) + return; + + write_lock_irqsave(&pool_rwlock, flags); + + stack = depot_fetch_stack(handle); + if (WARN_ON(!stack)) + goto out; + + if (refcount_dec_and_test(&stack->count)) { + /* Unlink stack from the hash table. */ + list_del(&stack->list); + + /* Free stack. */ + depot_free_stack(stack); + } + +out: + write_unlock_irqrestore(&pool_rwlock, flags); +} +EXPORT_SYMBOL_GPL(stack_depot_put); + void stack_depot_print(depot_stack_handle_t stack) { unsigned long *entries; From patchwork Mon Nov 20 17:47:16 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: 13461832 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 CDF55C197A0 for ; Mon, 20 Nov 2023 17:50:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6C1C26B0382; Mon, 20 Nov 2023 12:50:46 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 64B436B0384; Mon, 20 Nov 2023 12:50:46 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 512FA6B0386; Mon, 20 Nov 2023 12:50:46 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 3FA416B0382 for ; Mon, 20 Nov 2023 12:50:46 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 01DD6160947 for ; Mon, 20 Nov 2023 17:50:45 +0000 (UTC) X-FDA: 81479072892.20.44AFAF0 Received: from out-175.mta1.migadu.com (out-175.mta1.migadu.com [95.215.58.175]) by imf29.hostedemail.com (Postfix) with ESMTP id 431D612001B for ; Mon, 20 Nov 2023 17:50:44 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="altZ3Y/X"; spf=pass (imf29.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.175 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=1700502644; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=h9lSbnFVvjj2GZw0LrViVCVgXWgHHPxOTEIrM79fNvQ=; b=KczJU125yMZjT/uHw3wn0b625c59tl8ppbJQ730J7hBFahqJ0ekhZ5FnCjVSsXfSf+mCC4 fiY7+0HKyM4+/KdyPlt0rbvTJPOxQD69agOc2+rbtzb8E9lNXcOGOiJr7leB0ZerToggU1 F7YuvMV4sINfQ9HqTopTItBCNl7EqL8= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="altZ3Y/X"; spf=pass (imf29.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.175 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=1700502644; a=rsa-sha256; cv=none; b=e6OENMddHPsslc9dD9kZFypvXfyE1X7Edd9lQ9GpC4mG2M5VTHpG2fYKaSjpI+eSvLHMVv NqKjgrE/2i2iz2lmw5OjSnbVDKA8RMys6kfaYQZEKv67XJSUJEvQCPUuFBS32E+FJK++9s AYJnY/NzTsu15DFaAALF0V3nD00USFs= 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=1700502642; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=h9lSbnFVvjj2GZw0LrViVCVgXWgHHPxOTEIrM79fNvQ=; b=altZ3Y/XI4JhIu5hrnCRXA2Lh0EqKL+D8A/MAbLwDHf+SQx3XdzFWHdK500Z2nrgBAdsMJ CIl8i/FRs6CobGUwDDY8obdvF4axa9Jsyq1WLFLLkkvZ3UoyLWEgpJBac7eWWb07xxbTHj /N4UEr5XJVfvRHr9tfyWkhDs5sCw/8o= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 18/22] kasan: remove atomic accesses to stack ring entries Date: Mon, 20 Nov 2023 18:47:16 +0100 Message-Id: <29f59126d9845c5257b6c29cd7ad113b16f19f47.1700502145.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 431D612001B X-Rspam-User: X-Stat-Signature: yrtd83o561jdydqtzsizu48ue7u99dna X-Rspamd-Server: rspam01 X-HE-Tag: 1700502644-727315 X-HE-Meta: U2FsdGVkX189jYyQmdT89i/M+lVreY7SQAyqdppBoBg25pWJ2dULsjuT3cUlLolUtYQfpktBtdlaI1l+QY1CZmGwKn0QDirwcngfpXRn3ir8vTcsmrjYfCM4S6rhbbzrnxmrQbWmfsUROEO62ocmCLdzOEA2STzMd7XUy+iYHP3ba5ENM6biazJ7miAKE7K5IjpSXaPZU3BlZtdyHfdU+1+ol6LYRppd2eMd6Rr8KVe3bLPL8E9jD2l1nYPga8RLVX2/We9Y+PdCQM5WBCUQnSg8uEOblr1Fsr4vly+wKVSWAg7La8sMpEBG7ptbgZlCTM+CuIqz74k9I/Mx0XykcjtzK/oIAL3znX8KivcL+tNV9XrbKpU6yMAQCS1FWJpbna/+vxOzst8O8AiqUGm0fkWxsryHdWy82f5oYkpXL/FJwa5xwQWgRZQ89Qf4ulyx9rMY0NvM8s8LQyuif418RB++F3bRR7rBynVTC1YhoHvD4dOFJ7VdNE/Q9RHMfQHaxrq0Y6nhxZSpyVtdV+21XVTQCZj6/DUgi+fh6yRQL5NfG4k05fai4D2L/BakXuz9xnTKxvB8sV/QT6/k5Qak6vePho3kxlWNDqnER1jexjUnMQsm1KLtVY1nxakB9JsVgdjefSVHcL0jnQgV+tPoZiGQM5CTRyMW4HKzQJUoKyCzT6wYT7uuBECUQRwf66jWRNJxwXZw9M2IA1JptAfmA2ITGaUVsNStTYIrZMz0fbgJPqmHy4YEJXJoOHOAGw0HDYky1P+tqLjy8cYLSKwZt8h3NEfzMLwypMaj/Rs+Rgxm4NO4mY0ezBtqsnem7+n7wiFM5sWOJJytRK9mSioF1ZNstHxKVFxvi/1Ut8NfNOOeFpYTwcNk+NbCYbroys2BBPzuu0NZouRpz3oLd/93hi21mj4eNsosy9NzPbITv8k81lWgh7tX+MpYxO0hSCm9vBUGBsgaC/ftCSYcHI0 7+sjFsCf yYQisLZKIyUnKz5GZ/of6CzG8FoRXt29XYBlwGv98f+m3Qu4Zk1toFSpaD9oTEyK212u4 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Remove the atomic accesses to entry fields in save_stack_info and kasan_complete_mode_report_info for tag-based KASAN modes. These atomics are not required, as the read/write lock prevents the entries from being read (in kasan_complete_mode_report_info) while being written (in save_stack_info) and the try_cmpxchg prevents the same entry from being rewritten (in save_stack_info) in the unlikely case of wrapping during writing. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- Changes v1->v2: - This is a new patch. --- mm/kasan/report_tags.c | 25 +++++++------------------ mm/kasan/tags.c | 13 +++++-------- 2 files changed, 12 insertions(+), 26 deletions(-) diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c index 8b8bfdb3cfdb..78abdcde5da9 100644 --- a/mm/kasan/report_tags.c +++ b/mm/kasan/report_tags.c @@ -31,10 +31,6 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) unsigned long flags; u64 pos; struct kasan_stack_ring_entry *entry; - void *ptr; - u32 pid; - depot_stack_handle_t stack; - bool is_free; bool alloc_found = false, free_found = false; if ((!info->cache || !info->object) && !info->bug_type) { @@ -61,18 +57,11 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) entry = &stack_ring.entries[i % stack_ring.size]; - /* Paired with smp_store_release() in save_stack_info(). */ - ptr = (void *)smp_load_acquire(&entry->ptr); - - if (kasan_reset_tag(ptr) != info->object || - get_tag(ptr) != get_tag(info->access_addr)) + if (kasan_reset_tag(entry->ptr) != info->object || + get_tag(entry->ptr) != get_tag(info->access_addr)) continue; - pid = READ_ONCE(entry->pid); - stack = READ_ONCE(entry->stack); - is_free = READ_ONCE(entry->is_free); - - if (is_free) { + if (entry->is_free) { /* * Second free of the same object. * Give up on trying to find the alloc entry. @@ -80,8 +69,8 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) if (free_found) break; - info->free_track.pid = pid; - info->free_track.stack = stack; + info->free_track.pid = entry->pid; + info->free_track.stack = entry->stack; free_found = true; /* @@ -95,8 +84,8 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) if (alloc_found) break; - info->alloc_track.pid = pid; - info->alloc_track.stack = stack; + info->alloc_track.pid = entry->pid; + info->alloc_track.stack = entry->stack; alloc_found = true; /* diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index 4fd32121b0fd..b6c017e670d8 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -121,15 +121,12 @@ static void save_stack_info(struct kmem_cache *cache, void *object, if (!try_cmpxchg(&entry->ptr, &old_ptr, STACK_RING_BUSY_PTR)) goto next; /* Busy slot. */ - WRITE_ONCE(entry->size, cache->object_size); - WRITE_ONCE(entry->pid, current->pid); - WRITE_ONCE(entry->stack, stack); - WRITE_ONCE(entry->is_free, is_free); + entry->size = cache->object_size; + entry->pid = current->pid; + entry->stack = stack; + entry->is_free = is_free; - /* - * Paired with smp_load_acquire() in kasan_complete_mode_report_info(). - */ - smp_store_release(&entry->ptr, (s64)object); + entry->ptr = object; read_unlock_irqrestore(&stack_ring.lock, flags); } From patchwork Mon Nov 20 17:47:17 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: 13461833 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 C2212C2BB3F for ; Mon, 20 Nov 2023 17:50:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5B0D76B0384; Mon, 20 Nov 2023 12:50:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 513D76B0386; Mon, 20 Nov 2023 12:50:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 254716B0388; Mon, 20 Nov 2023 12:50:47 -0500 (EST) 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 086D56B0384 for ; Mon, 20 Nov 2023 12:50:47 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id CE5F91CAEAF for ; Mon, 20 Nov 2023 17:50:46 +0000 (UTC) X-FDA: 81479072892.09.D5FA36B Received: from out-174.mta1.migadu.com (out-174.mta1.migadu.com [95.215.58.174]) by imf17.hostedemail.com (Postfix) with ESMTP id 1C34B4001F for ; Mon, 20 Nov 2023 17:50:44 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=lATFUJLN; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf17.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.174 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700502645; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=uhMrVBrzgAoT1gwViC8KxLkH9OMZhix/uhvoEc70W98=; b=RMWEKcj6s/0VSN2juwyup7FFFhOwiUHGtBmePB/+CEpJ/yafg311vA0NnNZ+bbx2lpNk2y ynonv7RWcFyXuBXBNlBiKfYw7nttSj9uAXPIoa1aJd+BD2F/5j3m8rQ4bOuL680tPsWO+E gYXGoSO9eb5dajMDHW3G8UU/8O0E618= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=lATFUJLN; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf17.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.174 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502645; a=rsa-sha256; cv=none; b=eds7kq8eGCcWGLS0fkVbsCxvFxGvq2XH6CESg5mXLnbUGB2tL1ZZd7wBjflL5BQa0sjyg3 Pm6dvvuSSQ1JDlkHEyp58gaTo4HisJDM2XQC3RfGK2851ItSn8K5JO2Zf9d+Wthy/+MohA vVuFoxjj9cI1+o9EQwBaIp9GHNk1MSk= 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=1700502643; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uhMrVBrzgAoT1gwViC8KxLkH9OMZhix/uhvoEc70W98=; b=lATFUJLNYsof+QZ6G2sBXsnC9Q+wIKDyc8lFVJDjx9RK2nGA9FoeQs6kQO+6hjM8HBefIP eURADnY2Rf+gcAsebe+mW3WuHxDxbtNbYaSjhKQqLHNAk7mVow4Kb9CTZdG5zssygETF+r BwY3IBxnt51rdnlRs7VhwlO0TNcSVR8= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 19/22] kasan: check object_size in kasan_complete_mode_report_info Date: Mon, 20 Nov 2023 18:47:17 +0100 Message-Id: <68c6948175aadd7e7e7deea61725103d64a4528f.1700502145.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 1C34B4001F X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 45zsud6kppy1u749nfep7mdonygpdhyr X-HE-Tag: 1700502644-319821 X-HE-Meta: U2FsdGVkX19Mg/g8zgucnyidCEUYw4UDUaYivh9ax/8R7oQmS6fsnPTkN8ASKt5Qm0SCB1QZ5fPTPZk+Yh/jlUYeOqK1OPsMXV8FDj5A2NFb2oVvjD8hqGc9KfulDPm5bFQXNRXILlJUMJ0zO1Pze++2A/QlvyVridZIthUJ8xjTT32TaOOVs8kpP7U4ziH5VjBx53motOvoMtfbzIidTdyjQh6fyZiYPZ7f46XR+DRT3NTIRYXzfDixcB012pHo0YlJwXwFtVYt9n3k4o45gaIa6lyKXHg+k8YpC14DXOV+3lTjdgklkCTH15bw4TASIxD6IoKsH/ZiO2A1CiSOT3Wn7QMYnJedlxVhViNHoHQZ2nn5xpdJisFskm5I8LsYOG/aDD1B0aVGEQH5wILT/1fyUDwDMOMV7qUeEg/7FX1TqStKJIx/zciqP3NieOGass+QFLpGjj3XRiKVGQTECcVQF7jJ9oow0RynKbATadDeykV8gn3N5X+StGrmOHEbQklPSZYrQ0OEKI3ou1fh8xJlP2FWFdQK5tFwqdm+Oh+Xr0gQIj3pACNrZF2n074uqi0bcfuDkfx3IGiLaL6+CBiTucUtpW84uqT5qpEY4ADRj/D7UyiqWWa/pyqWDjZ/GEv3eI/Ke2NXBmcaCB58YzJoPuepgQPlOMJ6Rlxe5xWF0u6G0zrb//yKDHrC2eLL4PqoupKleyz/Jw3OcLymLrJ14C8CoxbQkt4jkHixnvBf6YRg8pibXvQ2aE7VC2CjOsoAjvKoL99JQk4zgUWPPgXVXmSnmgMFEjtfklDH7/xvY8G6dnPWCIFEFxuzIYaMDSzlt+zzEpKUxzk+mb7IKXkrAClDMmbQ8/m66uFOlG1GUla4T1o+gxYLaSNdZvDR+sQAtMS1WMLer6foDgKeFmnFD3162g7FCsGqoHvyb+0HSx0vBgky+DJpJ+K40mbCs+gLx1UGWZxS0pZThdM CsyhEjV3 0SI/ug98CEwxnmqLfjpdmhvwHwu3wkqzCMjq04eznwCpLULhx0aN8G5Kh45FOaVcTz0pv2kH8VhCEWtNOGDUNGEFi2u3eCne4Nv2+JofaFIUEB/vql64KXF7WsBpIPXRxRZcPDW02QTQSo5o= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Check the object size when looking up entries in the stack ring. If the size of the object for which a report is being printed does not match the size of the object for which a stack trace has been saved in the stack ring, the saved stack trace is irrelevant. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- Changes v2->v3: - Added missing "../slab.h" include for accessing a kmem_cache field. Changes v1->v2: - This is a new patch. --- mm/kasan/report_tags.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mm/kasan/report_tags.c b/mm/kasan/report_tags.c index 78abdcde5da9..55154743f915 100644 --- a/mm/kasan/report_tags.c +++ b/mm/kasan/report_tags.c @@ -7,6 +7,7 @@ #include #include "kasan.h" +#include "../slab.h" extern struct kasan_stack_ring stack_ring; @@ -58,7 +59,8 @@ void kasan_complete_mode_report_info(struct kasan_report_info *info) entry = &stack_ring.entries[i % stack_ring.size]; if (kasan_reset_tag(entry->ptr) != info->object || - get_tag(entry->ptr) != get_tag(info->access_addr)) + get_tag(entry->ptr) != get_tag(info->access_addr) || + info->cache->object_size != entry->size) continue; if (entry->is_free) { From patchwork Mon Nov 20 17:47:18 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: 13461834 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 C38C3C5AD4C for ; Mon, 20 Nov 2023 17:50:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B07FF6B0386; Mon, 20 Nov 2023 12:50:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A42EF6B0388; Mon, 20 Nov 2023 12:50:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 86E366B038C; Mon, 20 Nov 2023 12:50:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 62CFD6B0388 for ; Mon, 20 Nov 2023 12:50:47 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 42F63A095B for ; Mon, 20 Nov 2023 17:50:47 +0000 (UTC) X-FDA: 81479072934.06.304D4E1 Received: from out-182.mta1.migadu.com (out-182.mta1.migadu.com [95.215.58.182]) by imf04.hostedemail.com (Postfix) with ESMTP id 5EE7C40007 for ; Mon, 20 Nov 2023 17:50:45 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=SjlBdcCR; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.182 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=1700502645; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Qc2DDhk7eSddbt2Q3kjJLSbCNGbLZWOl9JsGZF81sG4=; b=w8vt3EAh3AJwU0IeQs45B2J2tAvYG0I4nC0Eq34unOYgYKNot6jFPaicT7opDYykZpD+VB btg4Pgm7oBSj1x++h3samy5zf5z9iNiXyZ51Wv9BafFs5wc80GWqiyn15k9nnD6HjWu1IR ptIeP5X5ILZ9Gu/3Z16NSiZXYdZQa7A= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=SjlBdcCR; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf04.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.182 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502645; a=rsa-sha256; cv=none; b=yCRN3gUspESFTGS41TuThKR6Sex7kButfp8FFcm2kgYDIQI1lX0mtakZDGo8gDnPpO3hkt l6aM2d/YU2rD86c2v5VOOEjvNPBEetLFu+3AqhNfeawoyWrl5P5oRe0muLOSxZRrdJL1ip fMqwSzVAVgh7JJre5y8QC/0ix1EN6mU= 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=1700502644; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=Qc2DDhk7eSddbt2Q3kjJLSbCNGbLZWOl9JsGZF81sG4=; b=SjlBdcCRByh5PrG2MOhc8aFxhKI3iaDpNaxTHUEIaJyLo5jT4ZNFXt6Bt3sd9u50OfOKSg CaLcy9JTN+VHkT3VWRZiUR5iYfkV9u6uxcxdsNTqWTVW/b0iVcXorrCGAHL4XcSBww8rJU PD/hyomJcMBAWfjY4j+0VCD4sMN1M4g= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 20/22] kasan: use stack_depot_put for tag-based modes Date: Mon, 20 Nov 2023 18:47:18 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Stat-Signature: xtxyfmpuxur3bn5pcfndc3s63weszqc3 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 5EE7C40007 X-HE-Tag: 1700502645-286687 X-HE-Meta: U2FsdGVkX1/6Mqw0a/YKSyMhOGBUOpEddE14+/E4tGysTxlQPxuDynaS/l43BKmX4z81scp2MngJTL69aW+fwkp9dkY0TscYAfWrAE2cKl+gLL5ZZkz3NIJPWk6Aj1V+P319DIu9ha53akaRgBHeCSxyJtxnP5+TAxgDAilv9zB2hC0Xz1VYtteOgsHoSkPpHtHm5C/b+7BaeWmZl1vWv9rvIjtfm8G4oV5he84vKTWmN6SYVcfPr3+6nrd2Ut86OgqSy/J6F0R0m2/9zUTRQMfklFOW0pAeO4iqeBKNAVijGK6ytZTHfPvfjLeHoY8g9eflffKtsB7FxoU1J9owmE9y//hI4fYhtJK3jVDAibS8Kny8gdwNB1rVbXe+A2165aNFJlD1vg17shrKirzq6SSZ65cGnAGFjQV4vKBSot1fQ0mOylGxRXhCZMYsvChmHyziRN7rWFBoSCePAWHSIr0k7TYsPXCTIrKO7C8cr1N8P7rkvZi1bug9WsOs4Tt0GuRSK8zN0+kQGcI1T2t45sUUPJXORCYktYjZWs2X4EsozwKzkmAh0uXs6lSZM7GgviZAWJ51PXZWElG15tHQa1JHR2Rki/n9rA/PBhXRYYp0YXWIrni/x3u5S+00euQmsZmGT/VOsgwY9WzuxiucQZHfK2w+mWQ9M2JkQOLvUfyk3zzJ2aQrWVaiKj//vrxA9n3jZTYDu2iKpFwVUzygK7r5OnD7IXbLbtnTdUl1UpCCnknTm20nk7XxTWzAflncxUZ2GV+GUzVHgSi1lUxFlW/STktuk474smIKc3i+BdPntFLWF4Ng9zbYjlz7I/EdotP1gWK7hFwNkfrD1Jl83wZE9Ea49f3KLHq2yYeLufuo8cX12uJy0AtCQP1Veh0OvrIvfaVV8TVG7120+HFaAQjr+rNOmEHcR0tcw7e9MpgONg/e54PArZyCM8842Cqqs3qpAN8p/2f2JduIUTX agips3D3 VB5z3hfRRnIDyDdkR+v2C2WD4OAg2cj0fEzGZGrGFQ7ZSIn3W9RitarIdanF+xvq7vysRLCJWTHBCqHF8aBVmlkKXtbUrFr9tFBkl X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Make tag-based KASAN modes evict stack traces from the stack depot once they are evicted from the stack ring. Internally, pass STACK_DEPOT_FLAG_GET to stack_depot_save_flags (via kasan_save_stack) to increment the refcount when saving a new entry to stack ring and call stack_depot_put when removing an entry from stack ring. Reviewed-by: Alexander Potapenko Signed-off-by: Andrey Konovalov --- Changes v1->v2: - Adapt to the stack depot API change. - Drop READ_ONCE when reading entry->stack. --- mm/kasan/tags.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c index b6c017e670d8..739ae997463d 100644 --- a/mm/kasan/tags.c +++ b/mm/kasan/tags.c @@ -97,12 +97,13 @@ static void save_stack_info(struct kmem_cache *cache, void *object, gfp_t gfp_flags, bool is_free) { unsigned long flags; - depot_stack_handle_t stack; + depot_stack_handle_t stack, old_stack; u64 pos; struct kasan_stack_ring_entry *entry; void *old_ptr; - stack = kasan_save_stack(gfp_flags, STACK_DEPOT_FLAG_CAN_ALLOC); + stack = kasan_save_stack(gfp_flags, + STACK_DEPOT_FLAG_CAN_ALLOC | STACK_DEPOT_FLAG_GET); /* * Prevent save_stack_info() from modifying stack ring @@ -121,6 +122,8 @@ static void save_stack_info(struct kmem_cache *cache, void *object, if (!try_cmpxchg(&entry->ptr, &old_ptr, STACK_RING_BUSY_PTR)) goto next; /* Busy slot. */ + old_stack = entry->stack; + entry->size = cache->object_size; entry->pid = current->pid; entry->stack = stack; @@ -129,6 +132,9 @@ static void save_stack_info(struct kmem_cache *cache, void *object, entry->ptr = object; read_unlock_irqrestore(&stack_ring.lock, flags); + + if (old_stack) + stack_depot_put(old_stack); } void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) From patchwork Mon Nov 20 17:47:19 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: 13461835 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 64C76C197A0 for ; Mon, 20 Nov 2023 17:50:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 823AA6B0388; Mon, 20 Nov 2023 12:50:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 760396B038C; Mon, 20 Nov 2023 12:50:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 588EE6B038D; Mon, 20 Nov 2023 12:50:48 -0500 (EST) 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 3E53D6B0388 for ; Mon, 20 Nov 2023 12:50:48 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 166B0C0977 for ; Mon, 20 Nov 2023 17:50:48 +0000 (UTC) X-FDA: 81479072976.25.5B999B1 Received: from out-178.mta1.migadu.com (out-178.mta1.migadu.com [95.215.58.178]) by imf14.hostedemail.com (Postfix) with ESMTP id 2213F10001E for ; Mon, 20 Nov 2023 17:50:45 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=DNgy9uYy; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf14.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.178 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=1700502646; 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=RvoTVlNH/9QJKGjG7ts3ES/7xJIaYyT0JIUdEPtk5OM=; b=lfrcZs/Y4I7oZVgDBUPd/dGAuSSqv3H/24ziVQShfY1sc+WCQFFkMjMXzKPoS1ttsonoxS NX5fv7II8xwiwfIfg/GnNEHF6TFdJ8Zs9fNG5UXlhxOUdxAu5IhlAEoa5ALiinq7pqIXlX NP49Fhawow8ad7dhGskBBrzbMqgWcas= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=DNgy9uYy; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf14.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.178 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502646; a=rsa-sha256; cv=none; b=PHHbPjx54HNVEpgGRzSKKkiR5tRa2u2Tu6XtLIe8MDO/0lK+IWOiu35RDdEJAVOQS+4mX8 mzk2YwQ9txBtMnBodYITsM2b23Q8BI7PlcDoblDZ0unu4NPVlwYvBvIiu7nLS3hJ2XKdUp 4xfSSRs0iy6bucGWbrfrBEZjlxTcxEw= 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=1700502644; 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=RvoTVlNH/9QJKGjG7ts3ES/7xJIaYyT0JIUdEPtk5OM=; b=DNgy9uYyYrfck71UIDDMmeBlP2Bik/eXdRS59wwnt2UBuC9bsjPHLcrxPmB7E98KHzUlfS OnFiyTSIdvZJFlR6bvb819vsoHa3zDRv09EFK0vHzurXdv2QeiuJeuOu21i9cfXyy/okM9 r0Wi2UF4LVMIbr0H8VW9kEyPJyGdVBE= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 21/22] kasan: use stack_depot_put for Generic mode Date: Mon, 20 Nov 2023 18:47:19 +0100 Message-Id: <5cef104d9b842899489b4054fe8d1339a71acee0.1700502145.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: 2213F10001E X-Stat-Signature: 3kp3mypr13cb5oqrc1ndy7c5jwbgegp7 X-Rspam-User: X-HE-Tag: 1700502645-759361 X-HE-Meta: U2FsdGVkX19swcpYf0H7BfUvAZF0tEN/dfKGjMukAbIEKCDvZa5xm10HkTzjVU+QlvUkdmCDY2WudoB2WwtLVcByaQR7F/TT/PlXw9aJpz9O+cbg/88I8yFGRoQD5VTI1iQBDPhAcmPBVPuY+0qYKepBbTF2MkRyn6KMLC+dalW5gni2eXXhTVcXKLv2yKy8MvPHwZHfyJCv/YABnFlfVbxfmfy7sX4RiNhPsME7jXScCwZ/ZML1DC3JsFLZnd5QTnk27RoOqeNRgyVWzTwd5qi528xPVqwQuLTAKW3iDzoWYk5qli7Y9e4wm5iMI26t+cXV9OJY4j77DhJzYwT5UF03RPU3AUZgUVQEmjNwrDvSRSDF1FZLPlBRdu9/bQtv/pV73tlHFAzibBHRpRcfU1NsZ6vFhb0U5RT5gyZeB+cJvoorSAJCY3XX12oW+OSVffQF39ac1VPE0pnIU7RJrbdRL3n+kLJJyXfvcuSvuov4It0Y5p7Ts2zBN5Xldvqo4ttO6dTsZbyrTJV0bAfta2fYMvrHKPJTpzfa8UsPbUhPvslMOdBci0TmQBXMKgdTNbmsStSzQRsNYhinqNvjua8D6lSKoR6E5n44Ak/LHP1jVhkwBnVkqltBMMFB5A5GXPxszfputltBubSxOGNiU6E+kNDSiOO4p0WoAR2xZkqGMncudvgj9Vg4kvr/IZx+Cfg2ht7xouSAyde7k7zv6oNDAzchRD46Mo3HS8n/rujPQZOo0pEHb027WTsxOf/3HDvTTSpDEkI/19eP23ExtmpB3uNjVllRxkuZS9jtI4RGBb3QcuoSpvSv3umWM4KWsvC830fmTOg1pdXzb9oiZbuNVMHBshCoM+PJ/iBQYogcBb/Emutk8KeNDcubLGISQjTlEwyHSlYh2MBgTMsp0juiYY5n0JoEYx2NKjaTKArXfLLASLxQwzMOI3txZwOayuwnw9PDs1AiiUQBJgs Bu+aRsEJ 5TsCnR69WuaPdDoVkwrkLKb8JJwIxugf9hOOqGWJRFw7Im+3XD39tuyHUcRrRql2A5BdEt7XJzLIfgIQdmW4eeDCITMXR4lX/08MC8mjSGEZEPfE= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Evict alloc/free stack traces from the stack depot for Generic KASAN once they are evicted from the quaratine. For auxiliary stack traces, evict the oldest stack trace once a new one is saved (KASAN only keeps references to the last two). Also evict all saved stack traces on krealloc. To avoid double-evicting and mis-evicting stack traces (in case KASAN's metadata was corrupted), reset KASAN's per-object metadata that stores stack depot handles when the object is initialized and when it's evicted from the quarantine. Note that stack_depot_put is no-op if the handle is 0. Reviewed-by: Marco Elver Signed-off-by: Andrey Konovalov --- mm/kasan/common.c | 3 ++- mm/kasan/generic.c | 22 ++++++++++++++++++---- mm/kasan/quarantine.c | 26 ++++++++++++++++++++------ 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 825a0240ec02..b5d8bd26fced 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -50,7 +50,8 @@ 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) { track->pid = current->pid; - track->stack = kasan_save_stack(flags, STACK_DEPOT_FLAG_CAN_ALLOC); + track->stack = kasan_save_stack(flags, + STACK_DEPOT_FLAG_CAN_ALLOC | STACK_DEPOT_FLAG_GET); } #if defined(CONFIG_KASAN_GENERIC) || defined(CONFIG_KASAN_SW_TAGS) diff --git a/mm/kasan/generic.c b/mm/kasan/generic.c index 5d168c9afb32..50cc519e23f4 100644 --- a/mm/kasan/generic.c +++ b/mm/kasan/generic.c @@ -449,10 +449,14 @@ struct kasan_free_meta *kasan_get_free_meta(struct kmem_cache *cache, void kasan_init_object_meta(struct kmem_cache *cache, const void *object) { struct kasan_alloc_meta *alloc_meta; + struct kasan_free_meta *free_meta; alloc_meta = kasan_get_alloc_meta(cache, object); if (alloc_meta) __memset(alloc_meta, 0, sizeof(*alloc_meta)); + free_meta = kasan_get_free_meta(cache, object); + if (free_meta) + __memset(free_meta, 0, sizeof(*free_meta)); } size_t kasan_metadata_size(struct kmem_cache *cache, bool in_object) @@ -489,18 +493,20 @@ static void __kasan_record_aux_stack(void *addr, depot_flags_t depot_flags) if (!alloc_meta) return; + stack_depot_put(alloc_meta->aux_stack[1]); alloc_meta->aux_stack[1] = alloc_meta->aux_stack[0]; alloc_meta->aux_stack[0] = kasan_save_stack(0, depot_flags); } void kasan_record_aux_stack(void *addr) { - return __kasan_record_aux_stack(addr, STACK_DEPOT_FLAG_CAN_ALLOC); + return __kasan_record_aux_stack(addr, + STACK_DEPOT_FLAG_CAN_ALLOC | STACK_DEPOT_FLAG_GET); } void kasan_record_aux_stack_noalloc(void *addr) { - return __kasan_record_aux_stack(addr, 0); + return __kasan_record_aux_stack(addr, STACK_DEPOT_FLAG_GET); } void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) @@ -508,8 +514,16 @@ void kasan_save_alloc_info(struct kmem_cache *cache, void *object, gfp_t flags) struct kasan_alloc_meta *alloc_meta; alloc_meta = kasan_get_alloc_meta(cache, object); - if (alloc_meta) - kasan_set_track(&alloc_meta->alloc_track, flags); + if (!alloc_meta) + return; + + /* Evict previous stack traces (might exist for krealloc). */ + stack_depot_put(alloc_meta->alloc_track.stack); + stack_depot_put(alloc_meta->aux_stack[0]); + stack_depot_put(alloc_meta->aux_stack[1]); + __memset(alloc_meta, 0, sizeof(*alloc_meta)); + + kasan_set_track(&alloc_meta->alloc_track, flags); } void kasan_save_free_info(struct kmem_cache *cache, void *object) diff --git a/mm/kasan/quarantine.c b/mm/kasan/quarantine.c index ca4529156735..265ca2bbe2dd 100644 --- a/mm/kasan/quarantine.c +++ b/mm/kasan/quarantine.c @@ -143,11 +143,22 @@ static void *qlink_to_object(struct qlist_node *qlink, struct kmem_cache *cache) static void qlink_free(struct qlist_node *qlink, struct kmem_cache *cache) { void *object = qlink_to_object(qlink, cache); - struct kasan_free_meta *meta = kasan_get_free_meta(cache, object); + struct kasan_alloc_meta *alloc_meta = kasan_get_alloc_meta(cache, object); + struct kasan_free_meta *free_meta = kasan_get_free_meta(cache, object); unsigned long flags; - if (IS_ENABLED(CONFIG_SLAB)) - local_irq_save(flags); + if (alloc_meta) { + stack_depot_put(alloc_meta->alloc_track.stack); + stack_depot_put(alloc_meta->aux_stack[0]); + stack_depot_put(alloc_meta->aux_stack[1]); + __memset(alloc_meta, 0, sizeof(*alloc_meta)); + } + + if (free_meta && + *(u8 *)kasan_mem_to_shadow(object) == KASAN_SLAB_FREETRACK) { + stack_depot_put(free_meta->free_track.stack); + free_meta->free_track.stack = 0; + } /* * If init_on_free is enabled and KASAN's free metadata is stored in @@ -157,14 +168,17 @@ static void qlink_free(struct qlist_node *qlink, struct kmem_cache *cache) */ if (slab_want_init_on_free(cache) && cache->kasan_info.free_meta_offset == 0) - memzero_explicit(meta, sizeof(*meta)); + memzero_explicit(free_meta, sizeof(*free_meta)); /* - * As the object now gets freed from the quarantine, assume that its - * free track is no longer valid. + * As the object now gets freed from the quarantine, + * take note that its free track is no longer exists. */ *(u8 *)kasan_mem_to_shadow(object) = KASAN_SLAB_FREE; + if (IS_ENABLED(CONFIG_SLAB)) + local_irq_save(flags); + ___cache_free(cache, object, _THIS_IP_); if (IS_ENABLED(CONFIG_SLAB)) From patchwork Mon Nov 20 17:47:20 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: 13461836 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 58172C2BB3F for ; Mon, 20 Nov 2023 17:50:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D78B76B038C; Mon, 20 Nov 2023 12:50:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D02D56B0391; Mon, 20 Nov 2023 12:50:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B2D8A6B0390; Mon, 20 Nov 2023 12:50:48 -0500 (EST) 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 7EA5F6B038D for ; Mon, 20 Nov 2023 12:50:48 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 6176B16093A for ; Mon, 20 Nov 2023 17:50:48 +0000 (UTC) X-FDA: 81479072976.20.338D882 Received: from out-174.mta1.migadu.com (out-174.mta1.migadu.com [95.215.58.174]) by imf03.hostedemail.com (Postfix) with ESMTP id B282120016 for ; Mon, 20 Nov 2023 17:50:46 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=WmK6Qhek; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf03.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.174 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700502646; 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=mWJyEm6VyaOWhREL8AAhhvtbKxcDTX0iHOdq2I7biaE=; b=RBSTdDKgI8MfRIb5+1881pMdSdSIrxTJ1TWXD5Y87kODowI3jPMajRtj+gkqg3i4zXENNc aJ3jFA8Rm0MB8UkS5kgK2N/q6S8YaIeLBmSi2ANKcEUhEJ8Tk6L12o84GcSQK+tTrqcWvz tHEFBxgK5ufMuNIARSn2W3MIE+Ihiew= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=WmK6Qhek; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf03.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.174 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700502646; a=rsa-sha256; cv=none; b=8Vwn6Lyp5SRLMmKaFUqR3NwsaCiXusVwO/hurluiWG2qvL6MLibVqIJOcrL4spQHIhr4SV v0rot5RaVeyufGauHG6gcr5KGfw4mRKApcUGPZi1IEoJuvkT6X6N4OLi2+fRWBE5EU56MK u7VreEwjMv3XikGyVBWTIijtFmeh5w4= 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=1700502645; 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=mWJyEm6VyaOWhREL8AAhhvtbKxcDTX0iHOdq2I7biaE=; b=WmK6QhekX7FInJyHV2uIR20pRmvpYax2GIBAaZMlPJyA0fa28yyqkU2wHc9EZkcNKhaGVw rpPS5IdkJL5crjQMbxbFNxa8MnpB9QR4IgbAId2GDpPOic2y+TwrC0P2SZxzr/nb1ApKtj eY3o0dSnWGXhfqV2/RcUKsx+woR/BOo= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH v4 22/22] lib/stackdepot: adjust DEPOT_POOLS_CAP for KMSAN Date: Mon, 20 Nov 2023 18:47:20 +0100 Message-Id: <301a115cf7ce8ddb42ef6de9151c2bb76ba728fc.1700502145.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: B282120016 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: 9bt6yhsormzcfea9hztejso4yp3916it X-HE-Tag: 1700502646-962587 X-HE-Meta: U2FsdGVkX19L9dM3YHXWugfm4pcuAacDEKi7uYqw/qaSRYPHWiR1W9r5nL1gd27kPEYB+KfLKNPj8IV1UnK62l7eVx0vCPYaPeBqZF4pMPw1x4LlaJKVogSKXxNoHBOX11faDitT+VBLd2RT3RqKNXcRLDWo4qecWXy1bIQWLmQoyJ2B+dbVD5cT1Fv81ztGG3odBX0htzkFU3ZwoVOCEO60j6fvFq+GQ03UysTRAc5ryjOdIdwfvv+Emb3ZboXEy1JoN1HzBznJ+T9090WNZoO5WCFMVU2ZwRQFisnLDprpY9YCp7129U/RkZXcbKvRs+kSr05lhty3+15+pKIwTzRDqEYLRB2T7Bew/G4hHwP8G9ADl+8lPR85T5uvr/7/uNvweppttx/i4uxNeGgXiDlrjkb5+6Jj5CKpT8EV6/nGebnRUaZOiqWc7ZGTjNpEcTjFHwsuisyWMHeTsIBGoWxaY2cwqyT8igFx0F/GAH++Lm89K6M74jP5eg9qgrGXwBM+0YMKT4E4q7cG+cQrMKtwsiE2AT87Ik+rmf4KvWfFltxIjFit+m+d9FYfT4QnH1BACyuoqco6HCLTUqkqkl0S6Zn58jKSoAPgYrEYb4BX/Z4HzuPddHHun3vwdZS1P5v3WXTimrGTRANMfrCta8wlqKIQiF+eGH5wrF49pCNoXPa4JIJ0YVx4Jep1jvgmnym5jIqNu9qkUMgXV9SVfrtnf/PgaP54ltFTv3DF/dwnIUmFZUJdgz6Xc59pQ4+PT46ohoutdfk6rkXmbhu+o2CTrxrW8pS6exJNuxj4dIaXsOPfVOshuWHZxeMxnS7fKRcSW7ksQv5aGA3mHRZBY75sgadqVanXiOB0iFwHBNasiZb/vFVOti3qZs2vArhU0YbSvOHL/+VjwZrxAEx7PQlha/t6A1ehPVZPM+ukCL2JurT2Xu5CTq1f2AkfZmA56akRBQoqVfN0+aNWEyv kdI351JH q9nuwUbj58BX4R/XtGKep70cTnGhzjwFODk0Dz/w+wUezlelCdSHDePQk+Q3u5HS8H+HIwEIrdmgER6Fu+kD2L+3IyS00ICE86hrds2leoT0Img4= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov KMSAN is frequently used in fuzzing scenarios and thus saves a lot of stack traces. As KMSAN does not support evicting stack traces from the stack depot, the stack depot capacity might be reached quickly with large stack records. Adjust the maximum number of stack depot pools for this case. The average size of a stack trace saved into the stack depot is ~16 frames. Thus, adjust the maximum pools number accordingly to keep the maximum number of stack traces that can be saved into the stack depot similar to the one that was allowed before the stack trace eviction changes. Signed-off-by: Andrey Konovalov --- lib/stackdepot.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index c1b31160f4b4..870cce2f4cbd 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -41,7 +41,17 @@ #define DEPOT_OFFSET_BITS (DEPOT_POOL_ORDER + PAGE_SHIFT - DEPOT_STACK_ALIGN) #define DEPOT_POOL_INDEX_BITS (DEPOT_HANDLE_BITS - DEPOT_OFFSET_BITS - \ STACK_DEPOT_EXTRA_BITS) +#if IS_ENABLED(CONFIG_KMSAN) && CONFIG_STACKDEPOT_MAX_FRAMES >= 32 +/* + * KMSAN is frequently used in fuzzing scenarios and thus saves a lot of stack + * traces. As KMSAN does not support evicting stack traces from the stack + * depot, the stack depot capacity might be reached quickly with large stack + * records. Adjust the maximum number of stack depot pools for this case. + */ +#define DEPOT_POOLS_CAP (8192 * (CONFIG_STACKDEPOT_MAX_FRAMES / 16)) +#else #define DEPOT_POOLS_CAP 8192 +#endif #define DEPOT_MAX_POOLS \ (((1LL << (DEPOT_POOL_INDEX_BITS)) < DEPOT_POOLS_CAP) ? \ (1LL << (DEPOT_POOL_INDEX_BITS)) : DEPOT_POOLS_CAP)