From patchwork Wed Feb 14 17:01:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 13556789 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 0E074C48BEC for ; Wed, 14 Feb 2024 17:01:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 970A36B00A9; Wed, 14 Feb 2024 12:00:59 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 91F936B00A7; Wed, 14 Feb 2024 12:00:59 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 798DF6B00A9; Wed, 14 Feb 2024 12:00:59 -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 639976B00A6 for ; Wed, 14 Feb 2024 12:00:59 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 2C90CA0D3F for ; Wed, 14 Feb 2024 17:00:59 +0000 (UTC) X-FDA: 81791024238.27.45715D3 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf28.hostedemail.com (Postfix) with ESMTP id 3A1EEC0009 for ; Wed, 14 Feb 2024 17:00:55 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=WgbFMw8U; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=3NIO6dV5; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=WgbFMw8U; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=3NIO6dV5; spf=pass (imf28.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.130 as permitted sender) smtp.mailfrom=osalvador@suse.de; dmarc=pass (policy=none) header.from=suse.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707930056; a=rsa-sha256; cv=none; b=2kOrLFSWiCWqxLChcD+mUmOSZqu9gTdrQJbJrq5s4KlbPoj5J2GK6Gu4k6mlO9eyudxv4a lprNaOzc+v8HS3e7rVZvR4ZSq5FZBmMh+1EtjtTh2iW73inpm13xOB06wfqPyXubxhp/0F K1qaxrbYgbzKqIBGsQBNO5rXXRh8sKA= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=WgbFMw8U; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=3NIO6dV5; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=WgbFMw8U; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=3NIO6dV5; spf=pass (imf28.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.130 as permitted sender) smtp.mailfrom=osalvador@suse.de; dmarc=pass (policy=none) header.from=suse.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707930056; 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=E2WS2tmMx6vkdBbq/BMMJO3YZaJfxmYDmOZxjs2QKsg=; b=mFu7W6/yQYvvuOt+evww/dxphn6iuBpxS8gaBl+AaRrKz5zaC8X1BpY4mZgJnOmZuSPUV1 EXe4FUtJjVbfqFEtnR5fbZYlDxwYoBakc6nxU3D2Um04qliaygF0SQ0OeVWL6TzK9EtXwt 2u83vXQ+6mDUMNbsfyaCSMJgIPodsVA= Received: from imap2.dmz-prg2.suse.org (imap2.dmz-prg2.suse.org [10.150.64.98]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id AD36021DB7; Wed, 14 Feb 2024 17:00:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707930054; h=from:from:reply-to: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=E2WS2tmMx6vkdBbq/BMMJO3YZaJfxmYDmOZxjs2QKsg=; b=WgbFMw8UsrxLruEg33YDAuX/poRzXS7nr/9vF3IZg0N8OqlEAqb7r2DCmE0N9JdTTknD88 DhsNvOUBQ8CBs44LnciNfutYPQvJF13zuDOSlEDicZfn+5sGByZKxDnrkfmyYpO3nCt9Dr 38A7zFmce29vz6ugxWdMRaJYObdjThc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707930054; h=from:from:reply-to: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=E2WS2tmMx6vkdBbq/BMMJO3YZaJfxmYDmOZxjs2QKsg=; b=3NIO6dV59Z+blB81iT3AmWsJbD2s5OZgbH4nP7yaUkeuxuYpCyB++tHVB7ivqpi8wrlcf8 kaTsY4hiqApWLVAg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1707930054; h=from:from:reply-to: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=E2WS2tmMx6vkdBbq/BMMJO3YZaJfxmYDmOZxjs2QKsg=; b=WgbFMw8UsrxLruEg33YDAuX/poRzXS7nr/9vF3IZg0N8OqlEAqb7r2DCmE0N9JdTTknD88 DhsNvOUBQ8CBs44LnciNfutYPQvJF13zuDOSlEDicZfn+5sGByZKxDnrkfmyYpO3nCt9Dr 38A7zFmce29vz6ugxWdMRaJYObdjThc= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1707930054; h=from:from:reply-to: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=E2WS2tmMx6vkdBbq/BMMJO3YZaJfxmYDmOZxjs2QKsg=; b=3NIO6dV59Z+blB81iT3AmWsJbD2s5OZgbH4nP7yaUkeuxuYpCyB++tHVB7ivqpi8wrlcf8 kaTsY4hiqApWLVAg== Received: from imap2.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap2.dmz-prg2.suse.org (Postfix) with ESMTPS id 2C77413A0B; Wed, 14 Feb 2024 17:00:54 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap2.dmz-prg2.suse.org with ESMTPSA id yAQrCMbxzGUfMwAAn2gu4w (envelope-from ); Wed, 14 Feb 2024 17:00:54 +0000 From: Oscar Salvador To: Andrew Morton Cc: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Michal Hocko , Vlastimil Babka , Marco Elver , Andrey Konovalov , Alexander Potapenko , Oscar Salvador Subject: [PATCH v9 1/7] lib/stackdepot: Fix first entry having a 0-handle Date: Wed, 14 Feb 2024 18:01:51 +0100 Message-ID: <20240214170157.17530-2-osalvador@suse.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240214170157.17530-1-osalvador@suse.de> References: <20240214170157.17530-1-osalvador@suse.de> MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 3A1EEC0009 X-Stat-Signature: ciqdx7i9k8a6siq9axp5eywx1hwtgxsq X-Rspam-User: X-HE-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rro3y/MTIR26/EkxFk7Nm6BTRhX08s2UX8ou/JptEwFVJnvJ/t4S/wdzjq6D5FFvUHbYPnYYDGSOZcxBQYbc7WfPZARr/0G0mT5GQax3FijkPXjSr9L4bQtHTcrSpFuUDjv50zIiudV6zSdS4RPdK 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: The very first entry of stack_record gets a handle of 0, but this is wrong because stackdepot treats a 0-handle as a non-valid one. E.g: See the check in stack_depot_fetch() Fix this by adding and offset of 1. This bug has been lurking since the very beginning of stackdepot, but no one really cared as it seems. Because of that I am not adding a Fixes tag. Co-developed-by: Marco Elver Signed-off-by: Marco Elver Signed-off-by: Oscar Salvador Acked-by: Vlastimil Babka --- lib/stackdepot.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/lib/stackdepot.c b/lib/stackdepot.c index 4a7055a63d9f..c043a4186bc5 100644 --- a/lib/stackdepot.c +++ b/lib/stackdepot.c @@ -45,15 +45,16 @@ #define DEPOT_POOL_INDEX_BITS (DEPOT_HANDLE_BITS - DEPOT_OFFSET_BITS - \ STACK_DEPOT_EXTRA_BITS) #define DEPOT_POOLS_CAP 8192 +/* The pool_index is offset by 1 so the first record does not have a 0 handle. */ #define DEPOT_MAX_POOLS \ - (((1LL << (DEPOT_POOL_INDEX_BITS)) < DEPOT_POOLS_CAP) ? \ - (1LL << (DEPOT_POOL_INDEX_BITS)) : DEPOT_POOLS_CAP) + (((1LL << (DEPOT_POOL_INDEX_BITS)) - 1 < DEPOT_POOLS_CAP) ? \ + (1LL << (DEPOT_POOL_INDEX_BITS)) - 1 : DEPOT_POOLS_CAP) /* Compact structure that stores a reference to a stack. */ union handle_parts { depot_stack_handle_t handle; struct { - u32 pool_index : DEPOT_POOL_INDEX_BITS; + u32 pool_index : DEPOT_POOL_INDEX_BITS; /* pool_index is offset by 1 */ u32 offset : DEPOT_OFFSET_BITS; u32 extra : STACK_DEPOT_EXTRA_BITS; }; @@ -372,7 +373,7 @@ static struct stack_record *depot_pop_free_pool(void **prealloc, size_t size) stack = current_pool + pool_offset; /* Pre-initialize handle once. */ - stack->handle.pool_index = pool_index; + stack->handle.pool_index = pool_index + 1; stack->handle.offset = pool_offset >> DEPOT_STACK_ALIGN; stack->handle.extra = 0; INIT_LIST_HEAD(&stack->hash_list); @@ -483,18 +484,19 @@ static struct stack_record *depot_fetch_stack(depot_stack_handle_t handle) const int pools_num_cached = READ_ONCE(pools_num); union handle_parts parts = { .handle = handle }; void *pool; + u32 pool_index = parts.pool_index - 1; size_t offset = parts.offset << DEPOT_STACK_ALIGN; struct stack_record *stack; lockdep_assert_not_held(&pool_lock); - if (parts.pool_index > pools_num_cached) { + if (pool_index > pools_num_cached) { WARN(1, "pool index %d out of bounds (%d) for stack id %08x\n", - parts.pool_index, pools_num_cached, handle); + pool_index, pools_num_cached, handle); return NULL; } - pool = stack_pools[parts.pool_index]; + pool = stack_pools[pool_index]; if (WARN_ON(!pool)) return NULL;