From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B839F24A7C0 for ; Thu, 16 Jan 2025 10:40:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737024050; cv=none; b=LBYLZQDpClTCDnGei4oDLzdXdju0Q1daewHkWEKLRqiA7rM8qAahduMdpjO+wstZ3ERD6RqDVMxNRTUZi+QD4jkt+g919jiYY1PCRxK099jEjHNh6UP5VTO/kwZixagz213c8RN1H+9ouBNe4Bv4D25CdN6VFlx0ay8voCYR/8M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737024050; c=relaxed/simple; bh=FXr/R/ASTh37z1sRowGMoD+KS4XfQ1KUY2lWIB+fsm4=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=SMFHINSW2K1ZBNS/CyZJ6+r/diWaCJMTlVfL5yorCNak/ddzdQIiXsww3zJUNJJItHlpbBv3N1j/94HAYXj00nPQbfO9hWOutcM6bDwMJ1Wen41eQQuLeYtrsGlG2Yt172IxvGfXgM/JMiPti6LoNcCYKy1C5HQPJQEnmf5xAvI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=S2uuhxG8; arc=none smtp.client-ip=209.85.221.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="S2uuhxG8" Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-38a34e8410bso381711f8f.2 for ; Thu, 16 Jan 2025 02:40:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1737024047; x=1737628847; darn=vger.kernel.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=EO66lMiXI/Sfgw5U/SYlAlVA9b1JkDl1ZV2cUjyfDeo=; b=S2uuhxG8t3yRx9fYZgNa9BD6J7CV86GcTgAG7JnUuLOxGuO0kVeeesWIBLOtIQVJ+N 8HMpXlEf0IAZFtIfrRQRgfbyZfr3bBUsgE8gJBKCUO5Ck8Uoz45UbkjkjzOVzql3OTvz rRYJAZgj81jFnj6Z30H0SP1cdwadJkRn8ys0i0oqayUDaxreaGDZK6PuU2KoMlbiyupM 7EVofZb+6maXvdYiNbWE6uE2IvP5UdJSa8idBDWNmwDs0689nc4tJ2sejP6PhJ4wrGds OksisIUBPHEbtiqzGnmkrxWWH0fcqPoG0Z9AzjlYf7e0gkLFgUMYrgAJzZtxRdI5kdqF xmXA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737024047; x=1737628847; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=EO66lMiXI/Sfgw5U/SYlAlVA9b1JkDl1ZV2cUjyfDeo=; b=Ne9YIhFJt/CrDEZTaXMGreshH/9vrJ6IhpoGXV0NSIhkRU+DDFD3gdk1A+qlar+yob KIoPwtG083s8+Rgdy+VIUWMLccN9cwcTlvFWomqAzM3+cQsFfEemFbdXhRlcqfnN7vZu Akrm6Ccwiw/THKxM80y4fC7Q7BTvFH14+AhI2g/rAiXncUkbrkJ3vHV1q0iPQ5kYMO70 iXMVDiZM6bABmDyFQ5btfG6m4EhQv+8gECk3++dH/M4wP8Pw36bzb1WXBL983aSTYdxR VcaMdbUtjRskWl4MjzBeXNWmPS8LusErV7WZBnUmbFrj2CXS3ut9vb1xaCGu7AQrym6U Pswg== X-Forwarded-Encrypted: i=1; AJvYcCX6oJAPxyvqYfSgrj6BTBblW4rGeHgL4qiNS7AKNkE9EHViRxZ90z8bnDy4KVLkBTVnywRVRsNGO4Gdtq4=@vger.kernel.org X-Gm-Message-State: AOJu0YyDJaRNJoMlZtltIlcTnWOikKSV9w+XHSsVXmvMgksXrevOVzQ1 XnFN0LZMmN9zanggJuX9Y3xkzG870UX+g5YlF/d8Wi0hOvqJmviL05AF38L4E9Qp4Yzgncg8MOE q X-Gm-Gg: ASbGncs+PBOaiMfHZGinGYB5e4zkkeJh5UR+S6Fc3DjGJ8lgJMyeIO9R+vt3kr7l+rG ZndZgXwzU2sK3EDXLm0F9EGssAx44k3VMHD8FxnTcZ9IeXdWvALNYxvqR8Gbi2aBt144ia84tCj yq7RDy/iNgsYFSUGoce5xtBEBoaEKC92PrE3qcO6TKIR01N9YZeQqXg1XOihFBJSsEQmOmGHU2I 6gip5nUjEnPx2uZnpMxmhV66QFpp86gV1EIUwYww46nHGp5Y5MXK+zJI7Co5S1k1w== X-Google-Smtp-Source: AGHT+IENhP2degD2HSgnG/QrAKOGnMF+zY05b1bOVZLzbMSLYYZMykC89gMNgsoBqQE6xISFPPiCKA== X-Received: by 2002:adf:a411:0:b0:38a:87cc:fbee with SMTP id ffacd0b85a97d-38a87ccfd10mr22019911f8f.14.1737024047135; Thu, 16 Jan 2025 02:40:47 -0800 (PST) Received: from [192.168.68.163] ([145.224.65.18]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-38bec32b96dsm1625787f8f.21.2025.01.16.02.40.46 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 16 Jan 2025 02:40:46 -0800 (PST) Message-ID: Date: Thu, 16 Jan 2025 10:40:45 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2] perf bench: Fix undefined behavior in cmpworker() To: Kuan-Wei Chiu Cc: mark.rutland@arm.com, alexander.shishkin@linux.intel.com, jolsa@kernel.org, irogers@google.com, adrian.hunter@intel.com, kan.liang@linux.intel.com, Ching-Chun Huang , Chun-Ying Huang , linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, namhyung@kernel.org References: <20250107073906.3323640-1-visitorckw@gmail.com> Content-Language: en-US From: James Clark In-Reply-To: <20250107073906.3323640-1-visitorckw@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 07/01/2025 7:39 am, Kuan-Wei Chiu wrote: > 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 > --- > Changes in v2: > - Rewrite commit message > > tools/perf/bench/epoll-wait.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/tools/perf/bench/epoll-wait.c b/tools/perf/bench/epoll-wait.c > index ef5c4257844d..4868d610e9bf 100644 > --- a/tools/perf/bench/epoll-wait.c > +++ b/tools/perf/bench/epoll-wait.c > @@ -420,7 +420,7 @@ static int cmpworker(const void *p1, const void *p2) > > struct worker *w1 = (struct worker *) p1; > struct worker *w2 = (struct worker *) p2; > - return w1->tid > w2->tid; > + return w1->tid > w2->tid ? 1 : -1; I suppose you can skip the 0 for equality because you know that no two tids are the same? Anyone looking at this in the future might still think it's still wrong unless it does the full comparison. Even if it's not technically required I would write it like a "normal" one now that we're here: if (w1->tid > w2->tid) return 1; if (w1->tid < w2->tid) return -1; return 0;