From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.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 C6891331209 for ; Thu, 13 Aug 2026 05:45:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=205.220.180.131 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786599919; cv=none; b=ED0ApRUaXuWzTtrrI7WmNsgO2pwANRtpuOqPFCi76tipbsofl0EggIe2t+j4xmYb77vt3/3zIyIJonx+1IJYbEWwNm0cd1wYj9xo1FdFxP0nOgXSDMxTomkvB3SPKM4ki3I4GI8erSam/wrgTi/UyDe8wW9elZPM9I+1PMYIzR8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786599919; c=relaxed/simple; bh=4Ih1IY9sJGO2xZu7SZLwTbHcPlFuLXeuvKTBxpJN7zI=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=KFH7DmsI5JZ5aHUtAoY271KIlRZEvoZnYnyD4EAzf5ACRIvA75oBFSTgGjeWQdN//uq9ZeXl5r0KNnEu7EOE/eDDQe3dyqKnQFjMFwHJX/0s3xZ4QX1TOWhHP+gnj/tAPGztA37I8idSI+0ROepM4bYyNAv32Xpu6s7sFJrO3es= 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=i1BabF9n; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b=Paq2fpsm; arc=none smtp.client-ip=205.220.180.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="i1BabF9n"; dkim=pass (2048-bit key) header.d=oss.qualcomm.com header.i=@oss.qualcomm.com header.b="Paq2fpsm" Received: from pps.filterd (m0279868.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.11/8.18.1.11) with ESMTP id 67D5gaAP1750106 for ; Thu, 13 Aug 2026 05:45:15 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= NWub6h7L1pIlGN7Rro0PKlrW45DCmQOmhfogtD6KZ9A=; b=i1BabF9nWETJRGgo JXUlRE8MBe81Rx9nZI7syen64Nma9H29FEzDf9Ce/Cqp3bz7JatO+0bKT7aa21sS y7octZlBBKuU7cx2U24mBd+c3FJxi5HkFWp4us3ibHqz98kTSOXQbF7MuYD4OF1G Z1us0ZHdvuKjfm1WZFr5qLdYIc9zDHTkdDgx9O8OZue0ynh7sp35s6XFG5a8xH3d Ar6kZoFelF1U6ne/fhM4dha/wCN8Ytl8FeEE3QrU+OqIpJ1B7Pe3tquv1DcZg6Hk PSEMq5jEpBcQm6EYeL2r7Q2IdISCWDXQQdk0XN/KtZ99fcPslFNPEhL3dOhddbM/ I2k+PA== 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 4g0xx1sxax-1 (version=TLSv1.3 cipher=TLS_AES_128_GCM_SHA256 bits=128 verify=NOT) for ; Thu, 13 Aug 2026 05:45:15 +0000 (GMT) Received: by mail-pg1-f197.google.com with SMTP id 41be03b00d2f7-cbb6433e9d4so2249952a12.3 for ; Wed, 12 Aug 2026 22:45:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oss.qualcomm.com; s=google; t=1786599914; x=1787204714; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=NWub6h7L1pIlGN7Rro0PKlrW45DCmQOmhfogtD6KZ9A=; b=Paq2fpsmojRrAn6XM3igT3RRh+RjfGaXolNZVqyKg9cd0FD4h3VbSlzjTPHjmmfdgU 7Pa+eloRTHaZgzg2cLeq8oN/jbjMSWWx2wzgAXrdwVgEXfdWV1VYQw05bAD14D0RdVJO 4ewnHx2miRW/WZZBvsXwzUmqb2kIl2prqbDNcsynEgU0OFf58RY3uUTNPt9QsELilpll NU1LQ9mhT3JS6wcALsGcEoUyQ4T0cDSAOcZsetDDNq9yuZ0Q0ZsbggIE5WI+HCFONkpX alcsBOWpiktV4vkoLoW9nnBD0MwL9A8/T72Wd0EvdQlyWV8pgSKILs0HfhKvDjVfJg2E xh4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786599914; x=1787204714; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :content-type:mime-version:subject:date:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=NWub6h7L1pIlGN7Rro0PKlrW45DCmQOmhfogtD6KZ9A=; b=GeUOSfHl4s0G+iYPP+oMbwg+YwNjwdrnyiZuv3iTRoWVVncZet7l6yq0RKSH7jN2D6 ZFCV+xiRVYW7+kvUHho2cwcBXREOUm96Lj7PaDSb+7/74kxEETd3TVl7HOnTF/V04n+3 BXWmxFOIazYk+exaTqSNJ5DVvcMJs10GEal/HAm2tTUw+LKux5JjN+cpckCWb+odAiNJ b0pGAirRCDmA+aph0QaUkzfLGMuxU4Dt8eSL5j2GrY6aCcOhcmjixC5B8H3Q2aa9yqKf w+qBWcWnZArivcwQMRH9/AnjsAB/2Ts2YiDgH33Unwv/KRz4OKynEHhc+fpfdXKIq6/q fJ1w== X-Forwarded-Encrypted: i=1; AHgh+Rrm79HkKYGYybDFYwjaU8NBKVOuM9uHtUhKWqUcssIgLVO6N94EkQJ3QLTOmNcpbRvHvubZyPeZyO1sraQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yx8EnT5Ka0zvBHN7fYwKQep4fgi0Sp9vU2rdG8b6KQPAA4xSJCO qzzdnbmWjBzn4ph+v7N+EFzIUb0928huT4EW1xLW/vmAFFH6RnyRzAEtCpsHGO3qz/72shOpguI Vi4IIfeFWwVJxtduMRCwQy27S0xihGe4rlg+8PB4pywEKJnnKI94M8wXnp3+pq6cQZQP8iut7HP 8= X-Gm-Gg: AR+sD12WecbVtdy6J9amyg8pdu+b9jfAkJqbqaawsbCGY9T2Qux3vCQw5eAa9pKY+nz g+ek8zwyRvue/ra3OH2S4e3Jo2uO1Mc2htDPXopz+3hiR4fPLvPloE/hOHdt0xVCvKjiswt54Dx zbQdhbinmY3SXWvzoLDX+HX2a/zzQgHZO9Cx/Gt4+g3EXrE6VJlHXYVo77vKYkiaoKRASwzkc79 S9dSV9T00tGK90RsXZ/GcPikbnqZS5woqjz9Nk+YOVHB5AuJxPfizwIo12wW+gxsX79eLpGU65E cFD8CCmzzt9IZsbRS4cMApJF7ZPXQW4VRn82Hnnfeq4squWa6djdo9jzI4YYTluHq/QrsY3R+S9 nZasjwnCPmWZlGU5jVKaTxmTNmKr0Y7P3nOk= X-Received: by 2002:a05:6a00:4fc9:b0:845:4cb5:2343 with SMTP id d2e1a72fcca58-84fc745dac0mr3096360b3a.19.1786599914235; Wed, 12 Aug 2026 22:45:14 -0700 (PDT) X-Received: by 2002:a05:6a00:4fc9:b0:845:4cb5:2343 with SMTP id d2e1a72fcca58-84fc745dac0mr3096298b3a.19.1786599913630; Wed, 12 Aug 2026 22:45:13 -0700 (PDT) Received: from hu-arandive-hyd.qualcomm.com ([202.46.22.19]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-84fc477e53esm381500b3a.14.2026.08.12.22.45.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Aug 2026 22:45:13 -0700 (PDT) From: Aniket Randive Date: Thu, 13 Aug 2026 11:15:02 +0530 Subject: [PATCH v7 1/2] i2c: core: Add i2c_update_timeout() helper for dynamic transfer timeouts Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260813-master-v7-1-3f9010cf9229@oss.qualcomm.com> References: <20260813-master-v7-0-3f9010cf9229@oss.qualcomm.com> In-Reply-To: <20260813-master-v7-0-3f9010cf9229@oss.qualcomm.com> To: Wolfram Sang , Andi Shyti , Mukesh Savaliya , Dmitry Guzman Cc: Viken Dadhaniya , linux-i2c@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, Aniket Randive X-Mailer: b4 0.15.2 X-Developer-Signature: v=1; a=ed25519-sha256; t=1786599906; l=6603; i=aniket.randive@oss.qualcomm.com; s=20260325; h=from:subject:message-id; bh=4Ih1IY9sJGO2xZu7SZLwTbHcPlFuLXeuvKTBxpJN7zI=; b=AHnu1ClrGKYq7Fjcdx+xaazaWz9OK+B9az/wUe6K63YRVNv/KthGFKu7ROWaXNN81GcVzOWi0 h3+csATiZ1OCWhPnz3zrkXN6I3uqEvbPqEG67HijO9d+an5Bdp7dSiF X-Developer-Key: i=aniket.randive@oss.qualcomm.com; a=ed25519; pk=4o37X1ZqGbCO/T2SR8kq/HnWvq0yb1RUlFuNukovbQk= X-Proofpoint-GUID: -LhfKIiGiP93YCpFg7iviOU8y8sqpWXZ X-Proofpoint-Spam-Details-Enc: AW1haW4tMjYwODEzMDAzNyBTYWx0ZWRfX8CKrBt2LEKk/ ZhNVCNlg3JmQutsYkU3XSVxEFS7HezcPQSM+WSz+e7GP8SEL7xxgf1/md4zcUoG/JTKA3E70tQd JnPJaCiKEa3tA0XEwnv+giAL9jZy2eV1jCJfYX4fPmOtPqUzK3SRNkGCNdagkaoMVPqlMg4fknn 6t5kGUE6iehTb70olWlIKullvlg5Cb7sFWTt7itZba6UWBpR2PpsioWrXCUlKOE+OMr/uGLy/Pg gTcsFoE7LuIovAlhxra6CEYidbJGHeqybwMIy+pbjLbpHQjfNBb77AAt3ILjxlUMgfbbV6FzOV7 YpMoqGq4GuBJoLD6fsKkdO66vQQgc+Xu5wi/oAn15nta9RmeHrUb2wS55q5arIViNHWNXjE1t7r 4IbuC4gRxJORa896p++TTNzDjpuIoCCLnFW5TAlAHSc97F3DH361lwQmtFNZS2EMPPiaJOotMt2 Pl+w5S7PQPKv4GYCsJA== X-Authority-Analysis: v=2.4 cv=J6KaKgnS c=1 sm=1 tr=0 ts=6a7d59eb cx=c_pps a=rz3CxIlbcmazkYymdCej/Q==:117 a=fChuTYTh2wq5r3m49p7fHw==:17 a=IkcTkHD0fZMA:10 a=Sv0fKeRqtYgA:10 a=s4-Qcg_JpJYA:10 a=VkNPw1HP01LnGYTKEx00:22 a=u7WPNUs3qKkmUXheDGA7:22 a=ZpdpYltYx_vBUK5n70dp:22 a=EUspDBNiAAAA:8 a=Xw8hV2vT4XMzfsrjPA4A:9 a=QEXdDO2ut3YA:10 a=bFCP_H2QrGi7Okbo017w:22 X-Proofpoint-Spam-Info: AW1haW4tMjYwODEzMDAzNyBTYWx0ZWRfX2/V7qE8Z31E4 ow0Ceia4T1PYsu9L+14T13yAyujdrUkQe93WWfKvi2z+mFhSN+eNW08hExf0AvrlolaTno2hxhu 2ichaoAt6mzZmgPY42QDAe4kAdxF46w= X-Proofpoint-ORIG-GUID: -LhfKIiGiP93YCpFg7iviOU8y8sqpWXZ X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1176,Hydra:6.1.134,FMLib:17.12.100.49 definitions=2026-08-13_02,2026-08-12_01,2025-10-01_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 clxscore=1015 malwarescore=0 bulkscore=0 impostorscore=0 lowpriorityscore=0 phishscore=0 spamscore=0 adultscore=0 priorityscore=1501 classifier=typeunknown authscore=0 authtc= authcc= route=outbound adjust=0 reason=mlx scancount=1 engine=8.22.0-2606150000 definitions=main-2608130037 The transfer timeout for an I2C controller should reflect the actual message length and bus frequency rather than a static 1-second value. A static timeout causes unnecessary delays on error paths for short messages, and may be insufficient for very long transfers. Add i2c_update_timeout() to i2c-core which computes a transfer-specific timeout and stores it directly in the standard adap->timeout field. The formula accounts for 9 bits per byte (8 data + 1 ACK) at the configured bus frequency. The caller supplies a safety multiplier and a minimum floor so that each driver retains full control over its timing policy without those values becoming public API. Storing the result in adap->timeout makes it visible to all consumers of that field, including the arbitration-loss retry loop in __i2c_transfer(). The function is gated by CONFIG_I2C_DYNAMIC_TIMEOUT. When the config is disabled, i2c_update_timeout() compiles to a no-op inline stub so drivers that call it build cleanly and the existing static 1-second default is preserved unchanged. A timeout explicitly configured by userspace via the I2C_TIMEOUT ioctl is stored in a new adap->user_timeout field and always takes precedence over the kernel-computed value. When userspace has not configured a timeout, the computed value is used. Signed-off-by: Aniket Randive --- drivers/i2c/Kconfig | 13 +++++++++++++ drivers/i2c/i2c-core-base.c | 40 ++++++++++++++++++++++++++++++++++++++++ drivers/i2c/i2c-dev.c | 4 ++++ include/linux/i2c.h | 12 ++++++++++++ 4 files changed, 69 insertions(+) diff --git a/drivers/i2c/Kconfig b/drivers/i2c/Kconfig index c232054fddd6..1d9691ec0e7e 100644 --- a/drivers/i2c/Kconfig +++ b/drivers/i2c/Kconfig @@ -136,6 +136,19 @@ config I2C_SLAVE_TESTUNIT endif +config I2C_DYNAMIC_TIMEOUT + bool "Dynamic per-transfer timeout based on message length" + depends on I2C + help + When enabled, the I2C core computes a per-transfer timeout from the + message length and bus frequency instead of using a static 1-second + default. A timeout explicitly configured via the I2C_TIMEOUT userspace + interface always takes precedence over the computed value. + + When disabled, the existing static 1-second timeout is preserved. + + If unsure, say N. + config I2C_DEBUG_CORE bool "I2C Core debugging messages" help diff --git a/drivers/i2c/i2c-core-base.c b/drivers/i2c/i2c-core-base.c index fb25704219c7..caa0748253ab 100644 --- a/drivers/i2c/i2c-core-base.c +++ b/drivers/i2c/i2c-core-base.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -2001,6 +2002,45 @@ void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_de } EXPORT_SYMBOL_GPL(i2c_parse_fw_timings); +#ifdef CONFIG_I2C_DYNAMIC_TIMEOUT +/** + * i2c_update_timeout - compute and set a dynamic transfer timeout on an adapter + * @adap: the i2c_adapter whose timeout field will be updated + * @bus_freq_hz: I2C bus clock frequency in Hz + * @len: transfer length in bytes + * @safety_coeff: multiplier applied over the theoretical wire time + * @min_usec: minimum timeout floor in microseconds + * + * Computes a transfer-specific timeout from the message length and bus + * frequency, applies a safety multiplier and a minimum floor, then stores + * the result in adap->timeout (in jiffies). The caller supplies the policy + * constants so they remain internal to the driver. + * + * A timeout explicitly configured by userspace via the I2C_TIMEOUT ioctl + * always takes precedence; the computed value is used only when userspace + * has not configured one. + */ +void i2c_update_timeout(struct i2c_adapter *adap, u32 bus_freq_hz, + size_t len, unsigned int safety_coeff, + unsigned int min_usec) +{ + u64 bit_usec = mul_u64_u32_div(len * 9, USEC_PER_SEC, bus_freq_hz); + u64 total_usec = bit_usec * safety_coeff + min_usec; + unsigned long jiffies_val; + + /* Userspace-configured timeout always takes precedence. */ + if (adap->user_timeout > 0) { + adap->timeout = adap->user_timeout; + return; + } + + jiffies_val = usecs_to_jiffies((unsigned int)min_t(u64, total_usec, UINT_MAX)); + /* adap->timeout is int; guard against signed overflow. */ + adap->timeout = (int)min_t(unsigned long, jiffies_val, INT_MAX); +} +EXPORT_SYMBOL_GPL(i2c_update_timeout); +#endif /* CONFIG_I2C_DYNAMIC_TIMEOUT */ + /* ------------------------------------------------------------------------- */ int i2c_for_each_dev(void *data, int (*fn)(struct device *dev, void *data)) diff --git a/drivers/i2c/i2c-dev.c b/drivers/i2c/i2c-dev.c index ccaac5e29f90..2d918bcbf8f7 100644 --- a/drivers/i2c/i2c-dev.c +++ b/drivers/i2c/i2c-dev.c @@ -494,7 +494,11 @@ static long i2cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg) if (arg > INT_MAX / 10) return -EINVAL; +#ifdef CONFIG_I2C_DYNAMIC_TIMEOUT + client->adapter->user_timeout = msecs_to_jiffies(arg * 10); +#else client->adapter->timeout = msecs_to_jiffies(arg * 10); +#endif break; default: /* NOTE: returning a fault code here could cause trouble diff --git a/include/linux/i2c.h b/include/linux/i2c.h index 14ab4d3055af..068217549266 100644 --- a/include/linux/i2c.h +++ b/include/linux/i2c.h @@ -742,6 +742,9 @@ struct i2c_adapter { struct rt_mutex mux_lock; int timeout; /* in jiffies */ +#ifdef CONFIG_I2C_DYNAMIC_TIMEOUT + int user_timeout; /* I2C_TIMEOUT ioctl value in jiffies; 0 = not set */ +#endif int retries; struct device dev; /* the adapter device */ unsigned long locked_flags; /* owned by the I2C core */ @@ -912,6 +915,15 @@ void i2c_put_adapter(struct i2c_adapter *adap); unsigned int i2c_adapter_depth(struct i2c_adapter *adapter); void i2c_parse_fw_timings(struct device *dev, struct i2c_timings *t, bool use_defaults); +#ifdef CONFIG_I2C_DYNAMIC_TIMEOUT +void i2c_update_timeout(struct i2c_adapter *adap, u32 bus_freq_hz, + size_t len, unsigned int safety_coeff, + unsigned int min_usec); +#else +static inline void i2c_update_timeout(struct i2c_adapter *adap, u32 bus_freq_hz, + size_t len, unsigned int safety_coeff, + unsigned int min_usec) {} +#endif /* Return the functionality mask */ static inline u32 i2c_get_functionality(struct i2c_adapter *adap) -- 2.34.1