From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9073019EEBD; Thu, 15 Aug 2024 13:37:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723729071; cv=none; b=c57WKBEsj5patZORc0V3Am9WVCYLR8V4NEKQwRi79ubH2lsd3jQvzb5NVwM/QRh0rQ6UynAHfFnotyKsea+GWuvvJ/r2bCBe9qRTMCkjORnQSRYKQJa5rV42dElkk9eXn06/fIhl73xzixR1CmIFHbkcHnlnddQ7fUns/RN9zuk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723729071; c=relaxed/simple; bh=5NDKDeQxFglbNAlFB3+llv98d0O3zbMQY8E+BGzllPQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=n1srE5K/Y1Jp2a1TihJk0nbmUfNkKJao+tgNXT0fV1q2cjMBqFOqbpYc/v8KMCpGekL/0qBnvRvYnRxTy9DhgZumBCbCJx5+Usa6UkxQcYAkG3fJEQuDeuFC4SNHcbUSNaTlINAZ34YTrFj6d1zKA2jNVfh+K6o0/QQJwi2TR6Y= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=g//JBcCU; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="g//JBcCU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 061C1C32786; Thu, 15 Aug 2024 13:37:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1723729071; bh=5NDKDeQxFglbNAlFB3+llv98d0O3zbMQY8E+BGzllPQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=g//JBcCUvHI3HdNRAQg6LUrOokMALOpDIzd7ymaGBhx2eHVtc08+ZAOAnARs6Z/Wi 1VHty6W61srqdJwbWbbuak+7zYDx/sQti63z9XkC/YO/waV/ufTHhGQKaygR7j3/qY dPLazk1hCOD0A7ZB+v2YxsWtvD9pS3DBuHQziKvE= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Masami Hiramatsu , Cheng-Jui Wang , Tze-nan Wu , "Steven Rostedt (Google)" Subject: [PATCH 4.19 184/196] tracing: Fix overflow in get_free_elt() Date: Thu, 15 Aug 2024 15:25:01 +0200 Message-ID: <20240815131859.110116554@linuxfoundation.org> X-Mailer: git-send-email 2.46.0 In-Reply-To: <20240815131852.063866671@linuxfoundation.org> References: <20240815131852.063866671@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Tze-nan Wu commit bcf86c01ca4676316557dd482c8416ece8c2e143 upstream. "tracing_map->next_elt" in get_free_elt() is at risk of overflowing. Once it overflows, new elements can still be inserted into the tracing_map even though the maximum number of elements (`max_elts`) has been reached. Continuing to insert elements after the overflow could result in the tracing_map containing "tracing_map->max_size" elements, leaving no empty entries. If any attempt is made to insert an element into a full tracing_map using `__tracing_map_insert()`, it will cause an infinite loop with preemption disabled, leading to a CPU hang problem. Fix this by preventing any further increments to "tracing_map->next_elt" once it reaches "tracing_map->max_elt". Cc: stable@vger.kernel.org Cc: Masami Hiramatsu Fixes: 08d43a5fa063e ("tracing: Add lock-free tracing_map") Co-developed-by: Cheng-Jui Wang Link: https://lore.kernel.org/20240805055922.6277-1-Tze-nan.Wu@mediatek.com Signed-off-by: Cheng-Jui Wang Signed-off-by: Tze-nan Wu Signed-off-by: Steven Rostedt (Google) Signed-off-by: Greg Kroah-Hartman --- kernel/trace/tracing_map.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) --- a/kernel/trace/tracing_map.c +++ b/kernel/trace/tracing_map.c @@ -454,7 +454,7 @@ static struct tracing_map_elt *get_free_ struct tracing_map_elt *elt = NULL; int idx; - idx = atomic_inc_return(&map->next_elt); + idx = atomic_fetch_add_unless(&map->next_elt, 1, map->max_elts); if (idx < map->max_elts) { elt = *(TRACING_MAP_ELT(map->elts, idx)); if (map->ops && map->ops->elt_init) @@ -699,7 +699,7 @@ void tracing_map_clear(struct tracing_ma { unsigned int i; - atomic_set(&map->next_elt, -1); + atomic_set(&map->next_elt, 0); atomic64_set(&map->hits, 0); atomic64_set(&map->drops, 0); @@ -783,7 +783,7 @@ struct tracing_map *tracing_map_create(u map->map_bits = map_bits; map->max_elts = (1 << map_bits); - atomic_set(&map->next_elt, -1); + atomic_set(&map->next_elt, 0); map->map_size = (1 << (map_bits + 1)); map->ops = ops;