From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.178 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795890228; b=Gho8np3eyjjMy81dA0y/dcJp9oW0XTdLZfm81+hhoTRpM6O7y/adq1ZCKs0dpWD4Hbox dkqD0Wvk9ZjycT5WuYgTCfEICcdjYBBAuP16DjqK+NbKu4tFhCGvZSNQqFSXYspSAA2Bq O+BtGvRe3vlDcdtWuDui1wYh8kn8A2a0ylHAifZ3j5mPXN1DU/XKqFZTUceMKGMuOl1BY 1CxStCGuprQa56TVqlWVSu34mzH3E3YsUH/o1nN00b9whZQtJ/cL2mUJjQvhAAtbNcOzC EuDjPxaUTJVjB35NoldIveLHpCDyJsDwJdY9f4SnBEaLJxIXPNRN69bjhPxZkuzPp5A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795890228; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=JQ8uRDUWF+NQ4T0bTDuUTJs9j6sya5WeJf5kOO78yyY=; b=qZFKiXVCGtPEoiCgJH7Y/4LXlsyPFwOMdcLqHUnGjbPExLlYn2rN69MCNjMWhJgOaZG+ setqH0/UYLWwuy7JU0kNx3/J5y/qxcv0cihajWImZ+r39UuNebVVnVgHRbzBrEbR8zNvS C3070T50Vi8Bmax8gxWTmlYoZJMQV1HMsNEt0biS+P8S3Ndl0bT5w0jBkpME38Ny4+AIU Dv+mQjTomPrfs5VvHXJW+WjonyitK5PhY6AtvLRVKjZ5EHaZL8pIoclc+VOdNLsULfoiP U74hvtE8ceHfXKEOhaR4sbvCwaCWPbMwpjwiEgn7rO+8GVA8+wH3G06SalybmnGR7Mg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=CPKuNePI; arc=none smtp.remote-ip=74.125.82.178 Received: from mail-dy1-f178.google.com (mail-dy1-f178.google.com [74.125.82.178]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2013B606A31C for ; Sat, 28 Nov 2026 18:23:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2013B606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=CPKuNePI Received: by mail-dy1-f178.google.com with SMTP id 5a478bee46e88-386ab9ab619so13031577eec.1 for ; Sat, 28 Nov 2026 10:23:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795890227; x=1796495027; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=JQ8uRDUWF+NQ4T0bTDuUTJs9j6sya5WeJf5kOO78yyY=; b=CPKuNePIxfu0Gdzs+QycIJg/y9F64AW/FMTLyXCIM73OdYDJH4NRleQbgaW2tDoVz0 yy4mouM8e9w08eLEwT46IeoRMlo4hjwZpEsFBScuPcJKAmLLwVsgckDsC3jT3XV//kja 0u8dHFB1tUxyxVBO+oPSkdANRfx7M1PwktcY2/gJ0exBomoXo/mUBQcnw3ohfMB9gf/b ztkG32jcs2F+rbam0sxIuHTe0xUt1NKw9+PWc7RjerwUzrBrQ5slxyE2Flvxqjd627XB fUt9eWvAVXVQXUeMRPHdrIX0gwKQs+yEFheX80cSLxyvdOG6svvGmFa9O+CNbJO3FNPo C+NA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795890227; x=1796495027; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=JQ8uRDUWF+NQ4T0bTDuUTJs9j6sya5WeJf5kOO78yyY=; b=OZtVFPUdyxS8G42igqKGW9TcwhJtFbwihKZiT5X6qKoMEEHeXv0C8WXmV8dRNH603O 2BlHaxlu1UQyutdeUaEqo1sAulxf3w2dbXuZD/pDyxdQgVCScRmMy+ooSjog5Vqmlzkq 53LLYMSph5oXSSwRYU6U6bCHdnS/RqR0xnmcaLxkPcGyOOwit9CQ40OA2aUssUp2lp+N Jn1PErxYpTcF49hiad5rWgiUz+J02bw4CeEIexq+sKCpHEBVXgytM3iEFaIv1Xwj2GiE ndvZeDC+m6GEDbucGDwKZb+C+Gb/bqpg1DQpBKu4LyOuReyS+XSd3VFni1Rs6J2mW1Ef 7RRA== X-Forwarded-Encrypted: i=1; AL+hErFkM5ymScMletuT4vttL7C2koMFkabbpQxbpx+ud45JztiEr6HhWnW97xDN9c2nhmLFyzYu2qRsTI/xV8A=@vger.kernel.org X-Gm-Message-State: APuU4Z+B56eJbcXoVio++0PMSxW84uita930GppFh7pLhFyqy6MTgQUI agg4eT4FvKbCPd3i6lltnDh6X1LEomOdFhvlyeONibgSOw6c39R7NwOL X-Gm-Gg: AZFdTZf0AntSt29Waa+0gWaK4TxyidztlyXn3BiLDAiObJFavLwTxYWLs3Vt9R/3JEw CY4jqFQXmxVC2Y7QpSDlZwIFL60vOK4bCeKQLfhrwtO6SLhxjY+xMC/or2OIsv0xrt/i4K8AQst 6MvmI1m3pzIiAn9GGGo7Yt5mt5IEi2TsfQvbpWpR1mCze8A450+pNBNfmPo0+9jX+1Bd3ASRDZW LGeCw2qvAXtORE3np0NKC+nV2zn+ONNIdYPASaSUAyzxOTD+5CpSaTkofLTiXrz4jNvhterqHWD H5oKuIFGlBGKULx3HVB2DedH1DD3IBD1iCFzJER0bUhZVBHDIBbP3R17USVLPu92hWwWT7JdDdk n1KzpitOS+MffeHT7CIw1wTpcq/yrs6+VauyZR0wQrYUurDEMjqze/JPM0GDLy4+e8y2eVax21+ 4ZPz/0vFICPpdqC6hBVnr00dE4nFv2JwGE6vDn9zGG+3poOTNuaGIhMr6qYHZyCBSGgSYuiRBOh Yt81O5zIuYKMPqrLtgxeNI0PcgEZEO71JW8DEXwmsk/nNeYc3fVSzBjEg4NN2JaO/+DAraV/3Gv TZWn452dRrEMGJozeR4mz0W05gtY0NH+Xfx4kbym1TfcudDbiMPP X-Received: by 2002:a05:7300:de44:b0:375:c5e1:931d with SMTP id 5a478bee46e88-3857073ebabmr27949898eec.25.1795890227118; Sat, 28 Nov 2026 10:23:47 -0800 (PST) Received: from sumeet-VirtualBox ([58.84.62.188]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-385c6830815sm76505422eec.20.2026.11.28.10.23.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 28 Nov 2026 10:23:46 -0800 (PST) From: Sumeet Pawnikar To: rafael@kernel.org, daniel.lezcano@kernel.org, linux-pm@vger.kernel.org Cc: srinivas.pandruvada@linux.intel.com, amit.kucheria@linaro.org, linux-kernel@vger.kernel.org, sumeet4linux@gmail.com Subject: [PATCH] powercap: idle_inject: Fix overflow in duration calculations Date: Sat, 28 Nov 2026 23:53:23 +0530 Message-ID: <20261128182323.7294-1-sumeet4linux@gmail.com> X-Mailer: git-send-email 2.53.0 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit The idle injection API accepts run and idle durations independently as unsigned int microseconds. Their sum can exceed UINT_MAX. For example, a run duration of 1 and an idle duration of UINT_MAX wrap the sum to zero, causing idle_inject_set_duration() to discard both values. Other sums wrap to a short period, so the timer can wake the threads much sooner than the requested interval. Test the durations individually for zero and use u64 for the sum before converting it to a timer interval. The full sum of two unsigned int microsecond values fits in ktime_t after conversion to nanoseconds. The multiplication used to pass idle duration and latency to play_idle_precise() also overflows on 32-bit systems because it is performed before conversion to u64. Widen both operands first. Built bzImage with i386_defconfig and x86_64_defconfig, idle injection enabled, GCC 8.5.0 and W=1. Both kernels passed guest runtime checks in QEMU/TCG for zero checks, normal and maximum periods, periodic callbacks, stop/restart, and overflow during timer rearming. Kprobes verified the actual duration and latency arguments passed to play_idle_precise() on both architectures. An unpatched i386 guest failed the four overflow checks while passing the normal behavior checks. The tests inspected long timer periods without waiting for them to expire; hardware power and thermal behavior were not tested. Backport notes: On trees using ns_to_ktime(), retain that helper and widen the sum before multiplication in idle_inject_start(). Changing the callback accumulator to u64 also widens its multiplication. On trees without c7cd6f04c0df, keep the existing requirement that both durations be nonzero and omit the two guard changes. Neither that 100% idle support change nor the us_to_ktime() cleanup is a backport prerequisite. Fixes: cd4c0763064f ("powercap: idle_inject: Use higher resolution for idle injection") Fixes: c7cd6f04c0df ("powercap: idle_inject: Support 100% idle injection") Fixes: 333cff6c963f ("powercap/drivers/idle_inject: Specify idle state max latency") Signed-off-by: Sumeet Pawnikar --- drivers/powercap/idle_inject.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/powercap/idle_inject.c b/drivers/powercap/idle_inject.c index a25eb2018acd..b6a52f7a294a 100644 --- a/drivers/powercap/idle_inject.c +++ b/drivers/powercap/idle_inject.c @@ -123,7 +123,7 @@ static void idle_inject_wakeup(struct idle_inject_device *ii_dev) */ static enum hrtimer_restart idle_inject_timer_fn(struct hrtimer *timer) { - unsigned int duration_us; + u64 duration_us; struct idle_inject_device *ii_dev = container_of(timer, struct idle_inject_device, timer); @@ -158,8 +158,8 @@ static void idle_inject_fn(unsigned int cpu) */ iit->should_run = 0; - play_idle_precise(READ_ONCE(ii_dev->idle_duration_us) * NSEC_PER_USEC, - READ_ONCE(ii_dev->latency_us) * NSEC_PER_USEC); + play_idle_precise((u64)READ_ONCE(ii_dev->idle_duration_us) * NSEC_PER_USEC, + (u64)READ_ONCE(ii_dev->latency_us) * NSEC_PER_USEC); } /** @@ -172,7 +172,7 @@ void idle_inject_set_duration(struct idle_inject_device *ii_dev, unsigned int run_duration_us, unsigned int idle_duration_us) { - if (run_duration_us + idle_duration_us) { + if (run_duration_us || idle_duration_us) { WRITE_ONCE(ii_dev->run_duration_us, run_duration_us); WRITE_ONCE(ii_dev->idle_duration_us, idle_duration_us); } @@ -223,7 +223,7 @@ int idle_inject_start(struct idle_inject_device *ii_dev) unsigned int idle_duration_us = READ_ONCE(ii_dev->idle_duration_us); unsigned int run_duration_us = READ_ONCE(ii_dev->run_duration_us); - if (!(idle_duration_us + run_duration_us)) + if (!idle_duration_us && !run_duration_us) return -EINVAL; pr_debug("Starting injecting idle cycles on CPUs '%*pbl'\n", @@ -232,7 +232,7 @@ int idle_inject_start(struct idle_inject_device *ii_dev) idle_inject_wakeup(ii_dev); hrtimer_start(&ii_dev->timer, - us_to_ktime(idle_duration_us + run_duration_us), + us_to_ktime((u64)idle_duration_us + run_duration_us), HRTIMER_MODE_REL); return 0; -- 2.53.0