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;