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 D2F8E15820C; Tue, 11 Mar 2025 15:28:35 +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=1741706915; cv=none; b=GaojohSvOJW2UVU2jvgiyxhmxLzrvdJ/gt6VDiwiFvifW+Ho8u8ZqvSUMXCrv8gK35u4Dqu8iSYNN69F60iVCJ8RxKe0U0ia37Seumf9blFItmwd3d7sVu8Ees54xSRTV42mJbF02k53xJS/ZsSe7w9xHUC9wqTFiY3ntbFx0AM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741706915; c=relaxed/simple; bh=NG5zL50oIKiu5IbpQsiVBgbzVe0AnJgHrfLySnRzdDk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=O9U/KhePpvkKW8rDCjdZx53WrObvNjY0UY3JI/7tU90f/g/hPAcb59pzdhAMVC3naCd14vTeJ/BeRcvXPFQazpRVAQTSgENEctz9PMlxoHzCc1knTbLptIsnFN+tC11ntivQVdS67qIHLtJMz5eX+Pvxvz3NzQHwtfaQB+UVdqc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=yafZu/ay; 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="yafZu/ay" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5909EC4CEE9; Tue, 11 Mar 2025 15:28:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1741706915; bh=NG5zL50oIKiu5IbpQsiVBgbzVe0AnJgHrfLySnRzdDk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=yafZu/ay88L3Pr4OP4ewpuHiE7yVxchYaWA72KvqFpx4jZY2gqtXBTK/68XZjIwbY kS9uumfd8hYMdrhfWFg8J6/UnHIXQxVFQhGxVzIj0MNWZdfpWpqbAn4uJEClznu9qL CGlcItXE59+RsG4ZXEcE8Jo4zHyEdzLfdtkiCez8= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Kuan-Wei Chiu , James Clark , Namhyung Kim Subject: [PATCH 5.10 180/462] perf bench: Fix undefined behavior in cmpworker() Date: Tue, 11 Mar 2025 15:57:26 +0100 Message-ID: <20250311145805.466468329@linuxfoundation.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250311145758.343076290@linuxfoundation.org> References: <20250311145758.343076290@linuxfoundation.org> User-Agent: quilt/0.68 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 5.10-stable review patch. If anyone has any objections, please let me know. ------------------ From: Kuan-Wei Chiu commit 62892e77b8a64b9dc0e1da75980aa145347b6820 upstream. The comparison function cmpworker() violates the C standard's requirements for qsort() comparison functions, which mandate symmetry and transitivity: Symmetry: If x < y, then y > x. Transitivity: If x < y and y < z, then x < z. In its current implementation, cmpworker() incorrectly returns 0 when w1->tid < w2->tid, which breaks both symmetry and transitivity. This violation causes undefined behavior, potentially leading to issues such as memory corruption in glibc [1]. Fix the issue by returning -1 when w1->tid < w2->tid, ensuring compliance with the C standard and preventing undefined behavior. Link: https://www.qualys.com/2024/01/30/qsort.txt [1] Fixes: 121dd9ea0116 ("perf bench: Add epoll parallel epoll_wait benchmark") Cc: stable@vger.kernel.org Signed-off-by: Kuan-Wei Chiu Reviewed-by: James Clark Link: https://lore.kernel.org/r/20250116110842.4087530-1-visitorckw@gmail.com Signed-off-by: Namhyung Kim Signed-off-by: Greg Kroah-Hartman --- tools/perf/bench/epoll-wait.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) --- a/tools/perf/bench/epoll-wait.c +++ b/tools/perf/bench/epoll-wait.c @@ -407,7 +407,12 @@ static int cmpworker(const void *p1, con struct worker *w1 = (struct worker *) p1; struct worker *w2 = (struct worker *) p2; - return w1->tid > w2->tid; + + if (w1->tid > w2->tid) + return 1; + if (w1->tid < w2->tid) + return -1; + return 0; } int bench_epoll_wait(int argc, const char **argv)