From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0a-0031df01.pphosted.com (mx0a-0031df01.pphosted.com [205.220.168.131]) (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 91E0C3C98AE for ; Wed, 27 May 2026 14:26:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.168.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779892009; cv=none; b=EBP4FkUEofUft9rcjlMXFRLO2hF1O7kn5v28/1iD5LDtL99yNyNFnkUrFkRHEBfiGZyr4YoPLgtQU4xyjg+IxnqTqLWxU9qBwFw9U2yV0t33xpaznjyR7EZTg3iIF4iU6kWsqWjZbmFyWAO0SLmIEUkhYPjOtEQhRqTa5tPtctQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779892009; c=relaxed/simple; bh=p0fc+TweU4yaDgwD97IW1GkjSBHIlSMDS6Qj+2AHseg=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=DJJ7UUwfW6kQz+bnnGmvvby37fljJFSY1XQEeb9KfkyTGdnyivRiWyqLGB7N1jTkrj1/YhmqNn29YEyJXdLc2LAjjycSC8REQWOJKhHlMXrox9ULdi0HjxIsVv0ZFp8+VN395zgDe1rWe6On3e/w2FIsKmjbpLB3rYvmzBZlWQM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com; spf=pass smtp.mailfrom=oss.qualcomm.com; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b=Y3vr6Znd; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=TiozGbRP; arc=none smtp.client-ip=205.220.168.131 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=oss.qualcomm.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=qualcomm.com header.i=@qualcomm.com header.b="Y3vr6Znd"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="TiozGbRP" Received: from pps.filterd (m0279867.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 64RA6h621149577 for ; Wed, 27 May 2026 14:26:45 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qualcomm.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= r7tUykhC5tSiT4yDAefapRxy4lcsiJZ62SujSiahRh4=; b=Y3vr6ZndV9AH3rPN BaG2d93xTNzXUv8SD2U9Nf5qvSgB7ODiGGHoAvcu9KYI+21SvmMRVAwA+YK/tEgG FlfzAiIIZZk2LQ4ez6mVkEPFsdCy2HW6MwyI3H1nWX5roYnsvJy9PeBdkgTcnRjR lALOrKJntMzOkLZuNvjDMxDwii1Pq60KslXIRPcZWrzKITFdoznUE9L6sy9kEtC8 dbJD5jBnmXT0UzTTXPX/VytVXCoOkBM4GBH4ZVB/RNpgylkBJWWbb+lUUESGI5IA Q4AaqLKNhj80VFIMPTs+ldeGHaWtRO4AcnBl/a1T2/Z0WZuZHAmD1wuyofbxjVBn dvjzxQ== Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 4edxjsgva0-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Wed, 27 May 2026 14:26:44 +0000 (GMT) Received: by mail-pg1-f197.google.com with SMTP id 41be03b00d2f7-c82c477290bso6521927a12.3 for ; Wed, 27 May 2026 07:26:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1779892004; x=1780496804; 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=r7tUykhC5tSiT4yDAefapRxy4lcsiJZ62SujSiahRh4=; b=TiozGbRPy/hxy+DQwQv/x2bFtRBmsthkIr9weM/7AyebRMdQ1f4mXrBdLAE+/jEhME L/UtcUtzJruXTJG+HeCG6BpgW6v2KEJ4SFOonmgCRF0ywAY6CSfUYxdtgnDH1/pdtNmd eAMy+LN52UYWjVUV0nzj2dMVcLFLHriLiZimuKqPI0RC1C6TjtSYF7DIsP4GF65KgD4N 9wHpFNNdu0eeIA1STNt04kAKnlkbdJWjOCKWKIML6EViB0Q6QlOZtlhR/N3kdA34wWfo PalNsmEF6G/FaQZjJy8uy2ClE6hrT49y8penO43PT5nUi28OOz/eDSn7r/xUlNaHtic3 gCzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779892004; x=1780496804; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-gg:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=r7tUykhC5tSiT4yDAefapRxy4lcsiJZ62SujSiahRh4=; b=JsiltChdIPWLElovHQTnlZl4Qt/0mK1EwAXCMOPVaYkXVn+Yn5JTZoxX1a5emRNK1E Sc3yScpXw8fk+6FDeiiRI67Dxi+i5HTYPKeZWbqHd/86vPvC0+RMFnLAg7YA7B70gvW6 2BxTEInfD8WVwo2VI4QFwzuXpK1L3LvCIRqrJC6VeZbCfzUaoxrPnBrE/6BAE+GDs2mC /EcstX16CQKyEg7xdl7ghC/LstHP0ZiYg7JNTijPOM/TOmHD77FNdGJ/hzBwXZQDuMDe NDSEOMFkZbXa8UG7+I6tgpO9pRmYIw0JhCgo1yS3xCzO0jAo18aI/O9U+/7L3JltYeAu 8bOw== X-Forwarded-Encrypted: i=1; AFNElJ8xDy88jPl6GoJrbrKx3OihmQBCOz75Y6vaIFUYNhdnzgLUN/E0SrDJOQyG1m37Xy19MOVg0FsT9/p2GeU=@vger.kernel.org X-Gm-Message-State: AOJu0YxshVWBLAiYFrtPOn5j3gXQy+VA/P/8LK5FU3g45X6mk1ftaX48 /VFYqiHF4Gb6NFyk0/iAsB0X6u1jOcBGLPNvukjpUEDEyESNWDmxYeSNgl/dqryL2lPAA1yz13R IRGYXFPBKR4KlvANaIBQXH/cpZVZ6fwg3KmogD+bfQejuBlEraq5pqb5lWrXGL8XKNus= X-Gm-Gg: Acq92OFPNS6mfSzWRCu5f8ZiBnxNIb0s798FkdCdAocX8+FukAgONvSnKDV2x7TBWmj 4AxqNW0bTwzMnI+khwHUd5brnTcsu/VA9tgg+dKJk6e5nNB6hKSBvq28EGsQ/zJxWwooWgbiIuG xLdHhwNi70Uzt+vYJfGWfqWfLhLKIiv94CI1mO+NycKVpv/9JJBNRju6EQmdumdYMfVhng8hXE4 jZxRs+7JgZ0LiwpgawQoYyNRMZoHgsbkA0yJ5NsakN3lIr37cnF3r7PISSB7VQkH8QYETr7BcNI AOd20sj0+HbwxwHmQlQjC3oVXB+lsfckkqiAl8dmwSzVyHJ3TeFaOiU1N9o+9my3HI60oAPIsoP UXgfQDTIDNFjRQqBRppuejDqwWqjZ0mIHxSQDNna+LZLshy2FeR7tjQ2CJIGdnv7LHJiZsDdDvW X7KZxU3AYzRRFBxv3XWue3ue54KrTC X-Received: by 2002:a05:6a21:694:b0:3b3:2703:111 with SMTP id adf61e73a8af0-3b328ee2f1cmr24572558637.44.1779892004198; Wed, 27 May 2026 07:26:44 -0700 (PDT) X-Received: by 2002:a05:6a21:694:b0:3b3:2703:111 with SMTP id adf61e73a8af0-3b328ee2f1cmr24572519637.44.1779892003645; Wed, 27 May 2026 07:26:43 -0700 (PDT) Received: from [10.133.33.240] (tpe-colo-wan-fw-bordernet.qualcomm.com. [103.229.16.4]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-c85205616bdsm13292837a12.23.2026.05.27.07.26.40 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 27 May 2026 07:26:43 -0700 (PDT) Message-ID: <18685e08-d304-4138-8c70-9b239e5c5925@oss.qualcomm.com> Date: Wed, 27 May 2026 22:26:38 +0800 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 v3 0/2] governor: Fix races and stale baseline on prev_cpu_nice To: "Rafael J. Wysocki" Cc: viresh.kumar@linaro.org, venkatesh.pallipadi@intel.com, davej@redhat.com, trenn@suse.de, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, zhongqiu.han@oss.qualcomm.com References: <20260419132655.3800673-1-zhongqiu.han@oss.qualcomm.com> <0b1ff222-ecda-4b21-9604-7d4e8fc86279@oss.qualcomm.com> Content-Language: en-US From: Zhongqiu Han In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Authority-Analysis: v=2.4 cv=C4PZDwP+ c=1 sm=1 tr=0 ts=6a16ff25 cx=c_pps a=rz3CxIlbcmazkYymdCej/Q==:117 a=nuhDOHQX5FNHPW3J6Bj6AA==:17 a=IkcTkHD0fZMA:10 a=NGcC8JguVDcA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=eoimf2acIAo5FJnRuUoq:22 a=VwQbUJbxAAAA:8 a=EUspDBNiAAAA:8 a=c92rfblmAAAA:8 a=E0ooF4KgEWxh89Kj4k4A:9 a=3ZKOabzyN94A:10 a=QEXdDO2ut3YA:10 a=bFCP_H2QrGi7Okbo017w:22 a=GvGzcOZaWPEFPQC_NcjD:22 X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwNTI3MDE0MSBTYWx0ZWRfX7RiFihgYJzIe 8NeYW2BggpNbQvKxSUBmytM0LpUhHxkJx/KVDzUU3IP62llhimEuomf/OjLwjpw1sbCt9jYUZN+ V+L3+GZJxxumwG9+EEX65Zt442vupzqkBaJcyvVQ8AYEV9EwbJ52bcSfGm0BLGLPO6D8aVvfxOY 5xYinnGLViSzHfcWAR2ubxYmvkSy8GBwFFpV3wHYfhdQDfvG21Yi92jDZr9z7uHr8h5ioQIDz82 4iCr7J80pBeCD2sn+lfrhSGO/CDeD0GhPipyNcxvz9gaiZlnStwhl+0rkkKdTOphFMloxSvT0NN jMdyTpNfZkhhvKIcCiwKauRP9UmZgFGKM76IZZtaKF0DMaIIwy3x6pA5nHKLrbJt8x+tjy1I6+j alhb4O0MqhQAszO5O1DpfyghEPX7Mvv+QlmOJ4OktWT0bKzRHFMTTHQm7ziyQosOSujKe4MXvfN ashb5xbW2ozymTDmgSA== X-Proofpoint-ORIG-GUID: X1ItJ10myj69spa4Ze6M7jFFmeXF0O23 X-Proofpoint-GUID: X1ItJ10myj69spa4Ze6M7jFFmeXF0O23 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1143,Hydra:6.1.125,FMLib:17.12.100.49 definitions=2026-05-27_02,2026-05-26_03,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 impostorscore=0 adultscore=0 lowpriorityscore=0 malwarescore=0 bulkscore=0 phishscore=0 spamscore=0 priorityscore=1501 suspectscore=0 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2605130000 definitions=main-2605270141 On 5/27/2026 1:09 AM, Rafael J. Wysocki wrote: > On Sun, May 24, 2026 at 7:57 AM Zhongqiu Han > wrote: >> >> On 5/22/2026 11:14 PM, Rafael J. Wysocki wrote: >>> On Sun, Apr 19, 2026 at 3:27 PM Zhongqiu Han >>> wrote: >>>> >>>> Patch 1 fixes two baseline-consistency issues in the DBS governor >>>> framework. >>>> >>>> gov_update_cpu_data() writes prev_cpu_idle and prev_cpu_nice while >>>> holding only attr_set->update_lock, whereas dbs_update() reads and >>>> writes the same fields while holding only policy_dbs->update_mutex. >>>> Because these are independent locks, the two paths are not mutually >>>> exclusive. The fix acquires policy_dbs->update_mutex inside >>>> gov_update_cpu_data() for each policy. >>>> >>>> Additionally, cpufreq_dbs_governor_start() reads io_is_busy before >>>> acquiring policy_dbs->update_mutex. A concurrent io_is_busy_store() >>>> can update io_is_busy and call gov_update_cpu_data(), which writes >>>> prev_cpu_idle with the new value under the mutex. >>>> cpufreq_dbs_governor_start() then overwrites prev_cpu_idle with the >>>> stale value. The fix reads io_is_busy inside the mutex. >>>> >>>> Patch 2 fixes a stale-baseline race on prev_cpu_nice when >>>> ignore_nice_load is enabled via sysfs. There is a race window >>>> between setting ignore_nice_load and gov_update_cpu_data() acquiring >>>> update_mutex: a concurrent dbs_update() can see the new ignore_nice_load >>>> value but read a stale prev_cpu_nice, producing an inflated idle_time >>>> for that sample. >>>> >>>> The fix unconditionally advances prev_cpu_nice on every dbs_update() >>>> call. With prev_cpu_nice always reflecting the most recent sample, >>>> enabling ignore_nice_load can never produce a stale-baseline spike. >>>> To keep prev_cpu_nice handling consistent, gov_update_cpu_data() >>>> resets prev_cpu_nice unconditionally alongside prev_cpu_idle, and >>>> cpufreq_dbs_governor_start() initializes prev_cpu_nice unconditionally. >>>> >>>> v3: >>>> - Update linux-next base >>>> - Fix two additional issues found during sashiko.dev review: >>>> - Patch 1: move io_busy read inside policy_dbs->update_mutex in >>>> cpufreq_dbs_governor_start() to close a TOCTOU window against >>>> concurrent io_is_busy_store(). >>>> - Patch 2: restore unconditional prev_cpu_nice reset in >>>> gov_update_cpu_data() to keep both baselines consistent when >>>> io_is_busy changes with ignore_nice_load enabled; update comment. >>>> - Patch 2: keep gov_update_cpu_data() in ignore_nice_load_store() to >>>> prevent a stale-baseline spike in the governor-start window (when >>>> ignore_nice_load is set before the first dbs_update() call); only >>>> cpufreq_governor.c is modified. Update commit message and add one >>>> more fixes tag '326c86deaed54a ("[CPUFREQ] Remove unneeded locks").' >>>> - Update the commit messages. >>>> - Link to v2: https://lore.kernel.org/all/20260409111407.9775-1-zhongqiu.han@oss.qualcomm.com/ >>>> - Sashiko v2 comments: https://sashiko.dev/#/patchset/20260409111407.9775-1-zhongqiu.han%40oss.qualcomm.com >>> >>> So sashiko.dev still has some comments on the first patch in v3: >>> >>> https://sashiko.dev/#/patchset/20260419132655.3800673-1-zhongqiu.han%40oss.qualcomm.com >>> >>> Can you please have a look at that and let me know what you think? >>> >> >> >> Hi Rafael, >> Thanks for pointing this out. I've looked at both comments: >> >> Comment 1 (io_is_busy transition window): >> >> The race is real but pre-existing. After Patch 1 the only remaining >> window is the gap between the io_is_busy write and gov_update_cpu_data() >> acquiring the lock: >> >> CPU0 (io_is_busy_store): CPU1 (dbs_update): >> io_is_busy = 1 mutex_lock() >> /* waiting for mutex */ io_busy = 1 /* sees new value */ >> cur_idle = get_cpu_idle_time(io_busy=1) >> /* prev_cpu_idle still set with io_busy=0 */ >> /* idle_time may be clamped to 0 */ >> /* load inflated for this sample */ >> mutex_unlock() >> mutex_lock() >> prev_cpu_idle = ../* reset */ >> mutex_unlock() >> >> The mismatch between the two metrics causes a one-sample anomaly in >> either direction: 0→1 tends to inflate the load estimate (frequency >> pushed upward), while 1→0 tends to deflate it. The AI's "drives to >> minimum" conclusion is wrong for the 0→1 case. >> >> This window is inherent to the design. One possible fix is to add a per >> policy io_is_busy copy inside policy_dbs_info and update it under >> update_mutex in gov_update_cpu_data(), so dbs_update() always reads a >> value consistent with the current prev_cpu_idle baseline. The impact >> should be a single-sample frequency spike that self-corrects. >> >> >> Comment 2 (sampling_rate_store vs. gov_set_update_util): >> >> Also pre-existing. The race: >> >> cpufreq_dbs_governor_start(): sampling_rate_store(): >> mutex_unlock() mutex_lock() >> gov->start() sample_delay_ns = 0 >> gov_set_update_util() mutex_unlock() >> sample_delay_ns = rate <---> >> >> If needed, e possible approach would be to move both gov->start() and >> gov_set_update_util() inside the mutex would work. > > Yeah. > > So I've applied both patches in the series as 7.2 material. > > Please feel free to send a follow-up patch making the change mentioned above. > > Thanks! Thanks Rafael, Sure, I'll look into that and follow up with a patch accordingly. -- Thx and BRs, Zhongqiu Han