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 A0D2125D908; Tue, 11 Mar 2025 15:08:31 +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=1741705711; cv=none; b=Q/ki09Bz4paSw1RxyPe4kd467K7iFUYcS/tQ4tW3qN+R4fp/XXkP4DjI3mU9LDsZO/9tNSFWQyWfjhkg3KcnZOoB/TqnB7xf8sqkNXe/heBqGHh6QBhxYzfdCva7Fa1NhygUufrrxwIPs3EpjmMkGVsYe1fbhj8NPatKfhPPLS4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741705711; c=relaxed/simple; bh=yc1lXOzDquYEBV5dxgoI8iFq3VW0JNgJxKUtEhjWGcI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KTxVIS+X6mzuqjVW6Sl/Il1uQ9VRsdFI4Nx7tTtiZqbFbzWypClVIDJKuzpatKjJz8XnEc5Nonq4W1TQV7RHopdY5OqYY4PIfTcd2NJJOvIUTv2c2D971cw0++VW0Nyc7bpPJtT7eET32SojZUvDB3WRkeyyu0Nx82dHuADElUI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=HpXcpQOE; 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="HpXcpQOE" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1EFDEC4CEE9; Tue, 11 Mar 2025 15:08:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1741705711; bh=yc1lXOzDquYEBV5dxgoI8iFq3VW0JNgJxKUtEhjWGcI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HpXcpQOEb8ZfPEcnzgezRFgOwaHHfNA47TdD8J1F4cWCqhBxrb93USC91lMCi6nFs TYgzj65Jhbf+KiqwueSH+nPA0H0ThKaaLs+LbIRj14awzXO39bB/608V/VxRnPYtc1 RZCKChmZxjUnDJiVGrmKSSLe5wTIGq9YpmrDhm7U= 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.4 129/328] perf bench: Fix undefined behavior in cmpworker() Date: Tue, 11 Mar 2025 15:58:19 +0100 Message-ID: <20250311145720.030837150@linuxfoundation.org> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250311145714.865727435@linuxfoundation.org> References: <20250311145714.865727435@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.4-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)