From patchwork Thu Feb 15 21:59:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Oscar Salvador X-Patchwork-Id: 13559170 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 453EEC48BC4 for ; Thu, 15 Feb 2024 21:58:07 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 866F38D0014; Thu, 15 Feb 2024 16:58:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7EF6A8D0001; Thu, 15 Feb 2024 16:58:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5F31D8D0014; Thu, 15 Feb 2024 16:58:06 -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 49AF68D0001 for ; Thu, 15 Feb 2024 16:58:06 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D3D4B140852 for ; Thu, 15 Feb 2024 21:58:05 +0000 (UTC) X-FDA: 81795401730.01.A1AF831 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) by imf21.hostedemail.com (Postfix) with ESMTP id 1EF811C0019 for ; Thu, 15 Feb 2024 21:58:01 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=BIeS8ey8; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=jDZk1p8j; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=BIeS8ey8; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=jDZk1p8j; dmarc=pass (policy=none) header.from=suse.de; spf=pass (imf21.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.130 as permitted sender) smtp.mailfrom=osalvador@suse.de ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708034282; a=rsa-sha256; cv=none; b=xiYVTBXW47xuuz2yTURvXbRzJQ7bl5CjQYvwLywvBknUiyMNe2w55fkfL9x4CykHdVvvty IaDP60XCcbi6UijBKOPlOPOA7YH2fsJEFjbSEJjyawX4mp+vmkUN1YzOfzTV3fDD4tt8zk y7qivviBIt2DUMd+yS7D88D9ilE57jM= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=BIeS8ey8; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=jDZk1p8j; dkim=pass header.d=suse.de header.s=susede2_rsa header.b=BIeS8ey8; dkim=pass header.d=suse.de header.s=susede2_ed25519 header.b=jDZk1p8j; dmarc=pass (policy=none) header.from=suse.de; spf=pass (imf21.hostedemail.com: domain of osalvador@suse.de designates 195.135.223.130 as permitted sender) smtp.mailfrom=osalvador@suse.de ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708034282; 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=dokaDBSPVaBXieZJW0HGUzN4swvmh9yT+sqR2X4jBMw=; b=l35V+Yixu/dM2BSi3dMtY2yhRxTuBAIQTx4I65w5p8A2PxbWG5sod9WeOxZbiDlrQO29op RNK5LKbaVVtuBptL+oEJc88hofCPXfvdLRgpOTQdNaY7p5Ddf06bUPiB/aXT6X6peGHdy7 2I88mAXPbCnhBB5isrVpI9JyoaHepMk= Received: from imap2.dmz-prg2.suse.org (imap2.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104: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 355F722216; Thu, 15 Feb 2024 21:58:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1708034280; 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=dokaDBSPVaBXieZJW0HGUzN4swvmh9yT+sqR2X4jBMw=; b=BIeS8ey8s0J4V2b54NhN6Z6gNxsesWqq8anGuqv6gfTwgfcfgP/eMqYcCFr+t9fqdbO7KL /2TRlCl9AMjAXSgahUTzydYxT/XfehM+xzfaVWAZaKJ4uDWTp9LUsY1SlfqL3f4uzxf0tD MmReXnT6el1g2Sxq4mzhHsya7W36coY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1708034280; 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=dokaDBSPVaBXieZJW0HGUzN4swvmh9yT+sqR2X4jBMw=; b=jDZk1p8juHpSTbEvbUl6/ZP2gzkG1u3u2N6mL6NXnzKG+UePPbn7Eo+Mu8OREiCJ8vopUl wWEtkgywZoevpnBg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1708034280; 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=dokaDBSPVaBXieZJW0HGUzN4swvmh9yT+sqR2X4jBMw=; b=BIeS8ey8s0J4V2b54NhN6Z6gNxsesWqq8anGuqv6gfTwgfcfgP/eMqYcCFr+t9fqdbO7KL /2TRlCl9AMjAXSgahUTzydYxT/XfehM+xzfaVWAZaKJ4uDWTp9LUsY1SlfqL3f4uzxf0tD MmReXnT6el1g2Sxq4mzhHsya7W36coY= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1708034280; 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=dokaDBSPVaBXieZJW0HGUzN4swvmh9yT+sqR2X4jBMw=; b=jDZk1p8juHpSTbEvbUl6/ZP2gzkG1u3u2N6mL6NXnzKG+UePPbn7Eo+Mu8OREiCJ8vopUl wWEtkgywZoevpnBg== 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 A5CFB1346A; Thu, 15 Feb 2024 21:57:59 +0000 (UTC) Received: from dovecot-director2.suse.de ([10.150.64.162]) by imap2.dmz-prg2.suse.org with ESMTPSA id IEDFJeeIzmXPAQAAn2gu4w (envelope-from ); Thu, 15 Feb 2024 21:57:59 +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 v10 1/7] lib/stackdepot: Fix first entry having a 0-handle Date: Thu, 15 Feb 2024 22:59:01 +0100 Message-ID: <20240215215907.20121-2-osalvador@suse.de> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240215215907.20121-1-osalvador@suse.de> References: <20240215215907.20121-1-osalvador@suse.de> MIME-Version: 1.0 X-Spamd-Bar: / X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 1EF811C0019 X-Stat-Signature: mricorj5uj7ow7ramc4t9gsiuutauuse X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x3ZaD4Q6WvyL0PGEFnUsQZztKBkjPK5FDa6lQy2DUxfzBP9UU93/uz0rdykChxhjWgytHhOcONgU5q77Vf2+qxqfNduZoSga72zPFOyRCGWWkbDT/+2rEXVx5hXl9sknW9x0b6w83uu42ke91KIEUSLe9/9aimZ5LEkh+s+j/bYJYCzuWq9t8dGapLuDvt27RZbhEn5f4ViL0BZfcO 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 Reviewed-by: Andrey Konovalov --- 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;