From nobody Thu Apr 9 14:09:29 2026 Received: from mail-oa1-f44.google.com (mail-oa1-f44.google.com [209.85.160.44]) (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 5A11527F754 for ; Sun, 8 Mar 2026 01:45:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.44 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772934302; cv=none; b=LicTfUde8q5eehZpjdDeLJZx/9rP1REYrsHpXvJ5W0CI/QB9ZSorqNesoTDHrcVq+IxwCJiSXjAduBREHPg7i5BZesN929FcNlIkPFk8nuSW6shKIM9Fq3aU1/OAqTTg0J/zYTtAX3KWVfYF9ghN9QjX8YQsaCmfAktBNvbysvk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772934302; c=relaxed/simple; bh=U6FHGWdz2Bvdzm9NUMGnmiRLKvyra1JEcjIxN8ogkEw=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jZKBD1UFIJEZ4dx8AJXfSzYAnGPOnM/y+VJT8jAbf7ECvSVSj5qUAg7bB4DDRoTbUW4R7LomCDbVHNToeeNhgWoV76D30m7cAyOWO4Iu6wHPxIeJRLdHfx2E2ugaxEg5QfW4E+x2IRqLGBAG89bMDZje5yJOnigt3GwTHnzCX+Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=rIm6ojcx; arc=none smtp.client-ip=209.85.160.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="rIm6ojcx" Received: by mail-oa1-f44.google.com with SMTP id 586e51a60fabf-40429b1d8baso2209475fac.0 for ; Sat, 07 Mar 2026 17:45:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1772934300; x=1773539100; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=fX8dp/t2b2t4ZLjEsHFbwzNcx+4POL6FqcGVJe++/XY=; b=rIm6ojcx1FPYNAu7olc43GfUWeynLXqyzZetOkMApqKBhl8zM6Hml+bXEjbCy9bC7a 5Yp2p7RKd1STA6ZaVR01cBgr2pTxXfif4BsbslvDhr/FGJtKLmoWYCN5CtaQaxA8/MPb zVl773MmMsKccLeEukmOJhmlEcyyuUjjoLSPeILcSWE4vn4SQJNIIsE6qmRoF3ulc9ak uDw8hbxV7+toXFR/UzUB+yupS9b6a/ODxxauL6RIgqCsshW6SHD7z+pWWeCiTNOYLOGS 3XSxpXxIkal/EjG97pKR4tTHOveiF6AeXBIUrQnyi1YHZ6hK7Qm5V8wJB0uCTi58sPQY 0V4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772934300; x=1773539100; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=fX8dp/t2b2t4ZLjEsHFbwzNcx+4POL6FqcGVJe++/XY=; b=ZMXiHN0lSWF5+lCPn5gzYO3yGnCuFAHrO2ywhApHSSirKozZOc9cAxG5LlpA+8Ba53 G4s9Y33LDwGY0QNKMs+mBh8Q3wJ2RHjNKb3EW3Wu8mVZw7+mrctTVr4Fa0wVBIFP6aP+ XI7SdJNvxR9N49VeApNSmdtRgAA0qYiHYtB0FWbE94TJRK+1+/THF5xDvkFSMC9k8Tkg uMW/Jfy351ROyxsRirKeAFx177v2REoaJ1EJNQMa/iU9cf9lKWNvMvdeMVp+MuS515uO IztN9fs6w+Wxrp01iWA2OzrizRcOSPbLK2dLPr341fBy5xvyXqBKwyTAwCUzfwexVbmL oWkQ== X-Forwarded-Encrypted: i=1; AJvYcCXOSZKV+y3ii9TM8JcgwKgObnQ/5NNw3/Mqpotk7kYexiujYHgkbp9GoOLt5Z1hRh1eMgDll0EsRtnA04c=@vger.kernel.org X-Gm-Message-State: AOJu0YzdL7FmZorl6ZstUS3E6wpFwCjfAesyNOmWIU9dfgUYc/pOyu5C iMhz1qG5dOLqoXUSDSVRdufbulaPuhWh/euP482wjkENOIxxO8YUZ5Yx94p15Tf5zFU= X-Gm-Gg: ATEYQzxJvuYUXSYKiUfGvoqE3vzOm5couK8f6JyJhapsZTwQGqxIZFBbmuec6w8fpvx i4r6OjpcaN+bTIXMRkfJCdDCRuYtyYP8XH1q+gMzOMj1dC6vrJimwS2Bn9EoL6F8f60mmnVNO1a NtWYJnf/8qAgWjfYOFs0IUkpCD/LEGTEKtY+MYSA+wKXDvOKsDJqwsNeq8IquOx2Yw2iBnCpitI EERhgkjuUh5xc8dibCxz05EKPrHlIk076v3yWtSwceaxIYJq4pZaTZpAGx9smi/b0l5wj10qghb xgrlxanIgF8B7HDParA5K3FRmGEk08aGGxxJjQSkXHM7EGHoYBVdMZ0YGHE5sqVCCNWvupVYfxR 0GyEw6vhUAi8ggozhqMVLMCV+3/lmgjAnQ+MmMDDffno4dQjmLGhc/Mc2ZoyXACNL+fh9JxlHhq dxofW3j1IpMoUwgFF1YQlckbMXDjtG X-Received: by 2002:a05:6808:4f1e:b0:466:fd2c:9570 with SMTP id 5614622812f47-466fd2cb1c4mr819176b6e.18.1772934300244; Sat, 07 Mar 2026 17:45:00 -0800 (PST) Received: from [127.0.1.1] ([2600:8803:e7e4:500:a7b4:e550:6d81:e067]) by smtp.gmail.com with ESMTPSA id 5614622812f47-466dfa96903sm3244357b6e.13.2026.03.07.17.44.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Mar 2026 17:44:58 -0800 (PST) From: David Lechner Date: Sat, 07 Mar 2026 19:44:09 -0600 Subject: [PATCH v2 1/5] iio: orientation: hid-sensor-rotation: add timestamp hack to not break userspace 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: quoted-printable Message-Id: <20260307-iio-fix-timestamp-alignment-v2-1-d1d48fbadbbf@baylibre.com> References: <20260307-iio-fix-timestamp-alignment-v2-0-d1d48fbadbbf@baylibre.com> In-Reply-To: <20260307-iio-fix-timestamp-alignment-v2-0-d1d48fbadbbf@baylibre.com> To: Jiri Kosina , Jonathan Cameron , Srinivas Pandruvada , =?utf-8?q?Nuno_S=C3=A1?= , Andy Shevchenko , =?utf-8?q?Lars_M=C3=B6llendorf?= , Lars-Peter Clausen , Greg Kroah-Hartman Cc: Jonathan Cameron , Lixu Zhang , Francesco Lavra , linux-input@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, David Lechner X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=4892; i=dlechner@baylibre.com; h=from:subject:message-id; bh=U6FHGWdz2Bvdzm9NUMGnmiRLKvyra1JEcjIxN8ogkEw=; b=owEBbQGS/pANAwAKAcLMIAH/AY/AAcsmYgBprNR1OsLLAxolA8HVS3rzGa+qNh/BN81hlw9zP 3RG6J2viwGJATMEAAEKAB0WIQTsGNmeYg6D1pzYaJjCzCAB/wGPwAUCaazUdQAKCRDCzCAB/wGP wHWkCAChcCxM7jrEnq62LcdCVUnk/uqRO9FKZ8KZHVclomQbcbUgZdTF8mqI1O3NA6ougHz4Uiz Crmb4OAVYxcTmirKu+BwKmGC+9NNEWZfHw14eNDvTW0LUXRMdcv/eLU0+QYIaMw++XAFkQwoJMv CVFeIj30yLXhdh0M24IgAFeHuXoCIyWL487LKhxj9BuJ71htmE/JM2Npkh9+vr7Rj7E+dpWBPqg 5fJgEFKbqOZz1G6thR4aM7ZbzvtMYYKF7qyGrU8AyEuZ3G2xxvJ4ENP1u28hfFhew1RDCd7rT9o JJqGpnsWviC9acvQy9ntW4BDWnfJvaGeFckEaGrgsrc4f+CS X-Developer-Key: i=dlechner@baylibre.com; a=openpgp; fpr=8A73D82A6A1F509907F373881F8AF88C82F77C03 Add a hack to push two timestamps in the hid-sensor-rotation scan data to avoid breaking userspace applications that depend on the timestamp being at the incorrect location in the scan data due to unintentional misalignment in older kernels. When this driver was written, the timestamp was in the correct location because of the way iio_compute_scan_bytes() was implemented at the time. (Samples were 24 bytes each.) Then commit 883f61653069 ("iio: buffer: align the size of scan bytes to size of the largest element") changed the computed scan_bytes to be a different size (32 bytes), which caused iio_push_to_buffers_with_timestamp() to place the timestamp at an incorrect offset. There have been long periods of time (6 years each) where the timestamp was in either location, so to not break either case, we open-code the timestamps to be pushed to both locations in the scan data. Reported-by: Jonathan Cameron Closes: https://lore.kernel.org/linux-iio/20260215162351.79f40b32@jic23-hua= wei/ Fixes: 883f61653069 ("iio: buffer: align the size of scan bytes to size of = the largest element") Signed-off-by: David Lechner Reviewed-by: Nuno S=C3=A1 --- v2 changes: - Rebased on fix that also touches the scan struct. - Improved comments. I found that I could emulate this thanks to /dev/uhid. And thanks to AI code generators, I was able to reasonably quickly make a script that worked for emulating "HID-SENSOR-20008a". See v1 message for test script source code. [v1]: https://lore.kernel.org/linux-iio/20260301-iio-fix-timestamp-alignmen= t-v1-1-1a54980bfb90@baylibre.com/ I set up the buffer like this: cd /sys/bus/iio/devices/iio:device1/buffer0 echo 1 > in_rot_quaternion_en echo 1 > in_timestamp_en echo 1 > enable Before this series is applied, we can see that the timestamp (group of 8 ending in "98 18") is at offset of 24 in the 32-byte data. hd /dev/iio\:device1 00000000 6a 18 00 00 ac f3 ff ff 83 2d 00 00 02 d3 ff ff |j........-....= ..| 00000010 00 00 00 00 00 00 00 00 5a 17 a0 2a 73 cb 98 18 |........Z..*s.= ..| 00000020 ad 17 00 00 6a f4 ff ff 35 2b 00 00 ca d0 ff ff |....j...5+....= ..| 00000030 00 00 00 00 00 00 00 00 2a a6 bb 30 73 cb 98 18 |........*..0s.= ..| 00000040 92 1e 00 00 50 ec ff ff ea c1 ff ff 78 f0 ff ff |....P.......x.= ..| 00000050 00 00 00 00 00 00 00 00 8f 3b a7 39 77 cb 98 18 |.........;.9w.= ..| After the first patch, we can see that the timestamp is now repeated at both the correct and previous incorrect offsets (24 and 32). (Normally, the last 8 bytes would be all 00 for padding.) 00000000 dd e0 ff ff 0e e0 ff ff 75 07 00 00 90 3f 00 00 |........u....?= ..| 00000010 f4 38 82 d0 3a cc 98 18 f4 38 82 d0 3a cc 98 18 |.8..:....8..:.= ..| 00000020 a0 e0 ff ff 1d e0 ff ff a0 0a 00 00 1c 3f 00 00 |.............?= ..| 00000030 3a 29 9f d6 3a cc 98 18 3a 29 9f d6 3a cc 98 18 |:)..:...:)..:.= ..| 00000040 a9 e1 ff ff 1e 14 00 00 6c c1 ff ff 98 f2 ff ff |........l.....= ..| 00000050 39 21 77 11 55 cc 98 18 39 21 77 11 55 cc 98 18 |9!w.U...9!w.U.= ..| --- drivers/iio/orientation/hid-sensor-rotation.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/drivers/iio/orientation/hid-sensor-rotation.c b/drivers/iio/or= ientation/hid-sensor-rotation.c index 6806481873be..5a5e6e4fbe34 100644 --- a/drivers/iio/orientation/hid-sensor-rotation.c +++ b/drivers/iio/orientation/hid-sensor-rotation.c @@ -20,7 +20,12 @@ struct dev_rot_state { struct hid_sensor_hub_attribute_info quaternion; struct { IIO_DECLARE_QUATERNION(s32, sampled_vals); - aligned_s64 timestamp; + /* + * ABI regression avoidance: There are two copies of the same + * timestamp in case of userspace depending on broken alignment + * from older kernels. + */ + aligned_s64 timestamp[2]; } scan; int scale_pre_decml; int scale_post_decml; @@ -154,8 +159,19 @@ static int dev_rot_proc_event(struct hid_sensor_hub_de= vice *hsdev, if (!rot_state->timestamp) rot_state->timestamp =3D iio_get_time_ns(indio_dev); =20 - iio_push_to_buffers_with_timestamp(indio_dev, &rot_state->scan, - rot_state->timestamp); + /* + * ABI regression avoidance: IIO previously had an incorrect + * implementation of iio_push_to_buffers_with_timestamp() that + * put the timestamp in the last 8 bytes of the buffer, which + * was incorrect according to the IIO ABI. To avoid breaking + * userspace that may be depending on this broken behavior, we + * put the timestamp in both the correct place [0] and the old + * incorrect place [1]. + */ + rot_state->scan.timestamp[0] =3D rot_state->timestamp; + rot_state->scan.timestamp[1] =3D rot_state->timestamp; + + iio_push_to_buffers(indio_dev, &rot_state->scan); =20 rot_state->timestamp =3D 0; } --=20 2.43.0 From nobody Thu Apr 9 14:09:29 2026 Received: from mail-oi1-f179.google.com (mail-oi1-f179.google.com [209.85.167.179]) (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 573E0286D4E for ; Sun, 8 Mar 2026 01:45:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772934304; cv=none; b=ImbychFSrNALo7semqVpOoxVu7N9i0ClI1mGXJ7CK1JwG69bBHwWUCTb+34skblFe89WILd5QGAmwgY1C4B/HWUVi4+KA2aklzpLG/cYG4f0FQQ4JdNpS9cHBV8FFiCj/4GauwSn+OP+saV701hk8zL7UY6t/9V8PJuwtpLbkBA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772934304; c=relaxed/simple; bh=HHzlMBc13MMwF6toEFotAoexdJQCCcdOvX6xGB6CV3Y=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=GVke4ShdVpMc+3yVfvbLYy1cF4IUGgdLjoR+I9ITSBYp3cXPcO4K7ueDY+Koc0kwtDabZWJVlIVvSzzWU83Dd+BSdLxMu5NGgFEdRec4ilXBGlBQJAcQVSDAzvUg4M5M2J0jwPhFSEYFHIUHNh3hgOg+uyFfQbIRvOyivDSSEKE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=FoU6KoWh; arc=none smtp.client-ip=209.85.167.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="FoU6KoWh" Received: by mail-oi1-f179.google.com with SMTP id 5614622812f47-466effa81c6so436884b6e.0 for ; Sat, 07 Mar 2026 17:45:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1772934302; x=1773539102; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=jGxgpavVCm5ubQX5YkJ9mR4ETgfWkKx58BqM6JjVDbU=; b=FoU6KoWhFvLpN1kV8ueZP+anb14cUPaMjiZnHlq2Q60VJT4K6PqkHaN65TnUJQ9H1S tkdhAeCcq+KFhhw+D5MrPViXlgBZLVCZdJ6SDD8ZhaggaRhEjKpKEP/haF0gN+SNz0fS 5a5opuSbEL4OOp9uu1qjDD1k0W1249vPBh8ew/6fatO+QVRU6qzKYN34z/8bxZH+bl7B VsluMJ8dAil58jPFstoYJcSeL7RHP+N8gHbm7Qb2y4apmenYyubKFRdOyL3s3rmkVRP5 sQscTgR1iebnPdkj7EzK2xDoxUCYrUFxhEfRUoQzsMxrM/7/FHcXZNGSG4XsyUIVajJ4 vlMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772934302; x=1773539102; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=jGxgpavVCm5ubQX5YkJ9mR4ETgfWkKx58BqM6JjVDbU=; b=KRneu+W/FyVNJozVRP+BayvR57/KEAWUWIlAOSdcYKL/pdyzHh8QWIhoI8J+2OUOek Bm31tYdEXoeV9rClA+Ik9qnnpSPp9QfR54rfRYGDWKvqjmkjKikqoegmBTsYnzwA6YYx BdixGJ3i1s7ca+S3EcmvNef8rxo/F9BPZFKy3Z86UgcnYW74e2HzSkYSDtn9m4yYwA09 ChaIj9ViA0F21PrddGAQ4hmIxmKmkDPNx8qA9xQuV01WX2ByYtLRg+SR9FhvWRID4YDy Ty8RFS84ttkJta5goQ/EB2KKjFb2pMB3KstFQgLew9jdDJlZjLhoQTglDkzLT+hrJY30 ZqiQ== X-Forwarded-Encrypted: i=1; AJvYcCWYQKcuW2T0eeLOsFR/ENtiA6vjBlJ1AiDsOw/E7gXhJHttPltXvTx0PWNZwjsHZKZH/tbwvuyiIdlFYtk=@vger.kernel.org X-Gm-Message-State: AOJu0YxcVs+z8zcZWDQUzD57Exoq3zhxX3ArrWyrO5SS5HUCgSBpvoZB gdjV5iku5BkjUJ60uyK4XnS5y2XzGAmJ7wWnvyGwwd8s48l4K1sR2X5/TlAOqZrH0uQ= X-Gm-Gg: ATEYQzxPzzGOkcj5/AMVlwXXBcPliLlBoxKXCmTpWU2t3HOAJ2Fi5hasC72TfZeckGq U84nb/JQfBEZcZD/vcwdpBzBx5UbDw/DR2pU1/YoO3vh+PsQSD0Z5WQoZe6+AuBPOh7VsEyovTx ipX9OajxJqI/9tl+RKH5rAt7T7Znp6jJpThP8KJ4An04sqoUhAGYTy3tSQQ7SwPkXebTeyCl8oW MVn0wQaNlJVqzjqmVbVaxR+BFo0zxrZKIWn2YMXxHXPsTmhqL+mX2zy5fAfLI1a+R5l23IDaRvW EgEAYJ05qxeHZRhepFqt8nuSzJuqMMoBbv0dxGrncEFBh5Rt1rzfdWaFQgB2SH8iilLxY5eHJiq nL7180rRD8/tBjG7qxIdB6MKZvEOZQRPmegg9Y5ZtyA+N7a5or6/nguPit967Cinf3t931pNy5t DnU1IXP9BMg4hhXFUsk//NcFCwsCE6+KQHnh4KOUw= X-Received: by 2002:a05:6808:2445:b0:450:3e60:fb47 with SMTP id 5614622812f47-466dcbd1f97mr4102020b6e.60.1772934302213; Sat, 07 Mar 2026 17:45:02 -0800 (PST) Received: from [127.0.1.1] ([2600:8803:e7e4:500:a7b4:e550:6d81:e067]) by smtp.gmail.com with ESMTPSA id 5614622812f47-466dfa96903sm3244357b6e.13.2026.03.07.17.45.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Mar 2026 17:45:01 -0800 (PST) From: David Lechner Date: Sat, 07 Mar 2026 19:44:10 -0600 Subject: [PATCH v2 2/5] iio: buffer: check return value of iio_compute_scan_bytes() 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: quoted-printable Message-Id: <20260307-iio-fix-timestamp-alignment-v2-2-d1d48fbadbbf@baylibre.com> References: <20260307-iio-fix-timestamp-alignment-v2-0-d1d48fbadbbf@baylibre.com> In-Reply-To: <20260307-iio-fix-timestamp-alignment-v2-0-d1d48fbadbbf@baylibre.com> To: Jiri Kosina , Jonathan Cameron , Srinivas Pandruvada , =?utf-8?q?Nuno_S=C3=A1?= , Andy Shevchenko , =?utf-8?q?Lars_M=C3=B6llendorf?= , Lars-Peter Clausen , Greg Kroah-Hartman Cc: Jonathan Cameron , Lixu Zhang , Francesco Lavra , linux-input@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, David Lechner X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=3313; i=dlechner@baylibre.com; h=from:subject:message-id; bh=HHzlMBc13MMwF6toEFotAoexdJQCCcdOvX6xGB6CV3Y=; b=owEBbQGS/pANAwAKAcLMIAH/AY/AAcsmYgBprNR7b767suN6jIKEqDrRz3nX7EsZpiKJ6DtLp WwYVCLT/LCJATMEAAEKAB0WIQTsGNmeYg6D1pzYaJjCzCAB/wGPwAUCaazUewAKCRDCzCAB/wGP wHz8B/404pwOs/Vt3dvW9KS8BQtWuQsOAsPEw79yoNxkAp1XprMgmWvc+HDTabcL6WVP+6xakIm XksXkmHCZh+1FuydrDCHGDvsG9q9YEcGpeB/FTZywede0OYd02ATP/jONq9PfDXvHh7oO9pViEA z/xEVl7INOOIyfM0dHbj+5s+mCldMSfkLbLIDFbNDsq13Q4ZRcxWllg03KLLPmdzRU2K0rd5Bbr 1N0O+VN/Nepd+ixC3Ue7HH+EGlXdStU9x+7U3bLTV9/2BQ3lWhdUjzuIyJMi/rWhIrbcVzTI6MV GHn5YkrE1NrT6wMc7IgxCcMSjI63qqlD+VvDHXs92U42DG4k X-Developer-Key: i=dlechner@baylibre.com; a=openpgp; fpr=8A73D82A6A1F509907F373881F8AF88C82F77C03 Check return value of iio_compute_scan_bytes() as it can return an error. The result is moved to an output parameter while we are touching this as we will need to add a second output parameter in a later change. The return type of iio_buffer_update_bytes_per_datum() also had to be changed to propagate the error. Signed-off-by: David Lechner Reviewed-by: Nuno S=C3=A1 --- drivers/iio/industrialio-buffer.c | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-b= uffer.c index f15a180dc49e..4e413b4bb073 100644 --- a/drivers/iio/industrialio-buffer.c +++ b/drivers/iio/industrialio-buffer.c @@ -762,7 +762,8 @@ static int iio_storage_bytes_for_timestamp(struct iio_d= ev *indio_dev) } =20 static int iio_compute_scan_bytes(struct iio_dev *indio_dev, - const unsigned long *mask, bool timestamp) + const unsigned long *mask, bool timestamp, + unsigned int *scan_bytes) { unsigned int bytes =3D 0; int length, i, largest =3D 0; @@ -788,8 +789,9 @@ static int iio_compute_scan_bytes(struct iio_dev *indio= _dev, largest =3D max(largest, length); } =20 - bytes =3D ALIGN(bytes, largest); - return bytes; + *scan_bytes =3D ALIGN(bytes, largest); + + return 0; } =20 static void iio_buffer_activate(struct iio_dev *indio_dev, @@ -834,18 +836,23 @@ static int iio_buffer_disable(struct iio_buffer *buff= er, return buffer->access->disable(buffer, indio_dev); } =20 -static void iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev, - struct iio_buffer *buffer) +static int iio_buffer_update_bytes_per_datum(struct iio_dev *indio_dev, + struct iio_buffer *buffer) { unsigned int bytes; + int ret; =20 if (!buffer->access->set_bytes_per_datum) - return; + return 0; =20 - bytes =3D iio_compute_scan_bytes(indio_dev, buffer->scan_mask, - buffer->scan_timestamp); + ret =3D iio_compute_scan_bytes(indio_dev, buffer->scan_mask, + buffer->scan_timestamp, &bytes); + if (ret) + return ret; =20 buffer->access->set_bytes_per_datum(buffer, bytes); + + return 0; } =20 static int iio_buffer_request_update(struct iio_dev *indio_dev, @@ -853,7 +860,10 @@ static int iio_buffer_request_update(struct iio_dev *i= ndio_dev, { int ret; =20 - iio_buffer_update_bytes_per_datum(indio_dev, buffer); + ret =3D iio_buffer_update_bytes_per_datum(indio_dev, buffer); + if (ret) + return ret; + if (buffer->access->request_update) { ret =3D buffer->access->request_update(buffer); if (ret) { @@ -896,6 +906,7 @@ static int iio_verify_update(struct iio_dev *indio_dev, struct iio_buffer *buffer; bool scan_timestamp; unsigned int modes; + int ret; =20 if (insert_buffer && bitmap_empty(insert_buffer->scan_mask, masklength)) { @@ -983,8 +994,11 @@ static int iio_verify_update(struct iio_dev *indio_dev, scan_mask =3D compound_mask; } =20 - config->scan_bytes =3D iio_compute_scan_bytes(indio_dev, - scan_mask, scan_timestamp); + ret =3D iio_compute_scan_bytes(indio_dev, scan_mask, scan_timestamp, + &config->scan_bytes); + if (ret) + return ret; + config->scan_mask =3D scan_mask; config->scan_timestamp =3D scan_timestamp; =20 --=20 2.43.0 From nobody Thu Apr 9 14:09:29 2026 Received: from mail-oi1-f176.google.com (mail-oi1-f176.google.com [209.85.167.176]) (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 9E98029898B for ; Sun, 8 Mar 2026 01:45:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772934307; cv=none; b=M9rJbp39tAu5dD1gEPqyKxKI8lOMRJA8fmPzEkfbfSlJ10m5Mc/KiZgpma1ShIlO4L8Vo5xR1MY3wlqq/1N6l1AEsKk63ckGoq2YK6tAknH39VIQbJZOFrqIuCkCMjjtEmncrCD9K3ngrhlRKKiQVAs9zXysauQYExMYQt6EI5c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772934307; c=relaxed/simple; bh=/1MB53WxYwYrJxGtXUsGnlz4muAeJwSJR6MVneiXaRc=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Zvayw6axElOF3QdyOmj1l3yPCpML79HemIAhsmdSwoFIH7zFWtRskRmv7dh823+G6LcT2w8eeWXCAeSViPbNZOW/5OiuKavXg4lWCr/Cq2Ol0yohlggFM+1E25m403SElQyAcziDVPQ1iPTwdlJXQ/sAy1RssjhtxSvGGsHhCsk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=VAtF+K2o; arc=none smtp.client-ip=209.85.167.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="VAtF+K2o" Received: by mail-oi1-f176.google.com with SMTP id 5614622812f47-466effa81c6so436903b6e.0 for ; Sat, 07 Mar 2026 17:45:05 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1772934304; x=1773539104; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=4Y0Q32Eai6u50Ztr+HUMEtNeNWPFKto/mfq53aBfCfY=; b=VAtF+K2oW8XGdA97HfsOHS7wWaatQbWhOGpS6eMMTOOkVM0ROXdpNF5Jq+fDyRjbzg eqvj9iApd0KFDe5H2Mi8k90mt8kDtaf1+H5x7c9o7bFdMeCIwGaMivHXGWUR1HrjCxXN ZZWh5I8p9/JwzvQ+5+2/9KRqVuDmgHPqtOuFxE/3FMzK1kLGswEjsLFup7Vz3VuX1ctZ 52YSPvaBJUrpF8RUlDXIVX3LEjLVxSB5IVsIDZknPaHj44QLFPUuETaOSgYveb/9NBrM dDchqst/BKWKYrxZjSGI5pbXLQKEtd/b2aQhYzXjDIvb0xxRf81qt+59zKqA8ZPScSpx v6zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772934304; x=1773539104; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=4Y0Q32Eai6u50Ztr+HUMEtNeNWPFKto/mfq53aBfCfY=; b=BSN00rtDYEUxec5/adNZlwyMduDHdr80nyEVPqRpHE+6BdIsIIJt9yBotFulyR8by8 TR05+R2JcfDnEVqoWH7+7m5Mimz6p4rgrW+qaDX8qEJKrw5AKo0CYSmig4xeviIRC9lY HS9u83cV+qxwZqf8vEwnowhVQ3mXRYTz3A2oeVt+Q3Mh7PDjWvl2xqZlHMmwHxTFl1MT 9C9zXOuxI8itGnpHMKUWlVwasE/7XlC/t+zbPwv9p9sg0FteHcUyyePVDPZPUG8Y1pQC Oz/rkVrSXbd+2hRBoIttGe6Ph911VfhUrhuVXeEXrbzhiB74WpQ7qkyK7Kde8tDlHBaw /eDA== X-Forwarded-Encrypted: i=1; AJvYcCU2ilh6R0D0vLwz3JaLIEab1Tzk81mZeODoJmcadbFbWz0vbwBSjLzDDRsL//TKfLhcepoemyCZ1P89Ksg=@vger.kernel.org X-Gm-Message-State: AOJu0YzRgLDmEfBr6Kr6AEZHw/DP3XArbu7lWMaSHL39toPIUTX0tiDG Md+9+aVbSVn04REhaBIIdVzNVJFgMXHSKLclKH7xI3os2IFQrVc4FeO+bc1VBlC6pY4= X-Gm-Gg: ATEYQzyaXdLqQF+Wx+po65rgKZhFF1T/O/F4uwc9Wv+xFcvlSO+ZOiPLlhrbjE2mRKI NnyYk3muxEAPstVfFPEEWQtlHwDlbPj7nKzHmHaiIydJYuEItCmvpUXNSSu3dNV07iKjPLmxPLV YORIEW95C8wZbbmId/0hThXoGoC8MQqbG4xlH2yM8J4D7ibKrmEvZqnd0lAPbXVl0vawIbW+gAl 39vt8tfSoZlXteMTIirmlikUlpd4qJxIHabtO0WGysEBCCJ6biyhPptfLSdwSrztcx0EWfFdHfq ZrsL38+eMLn4tmlEkyu0ZQKpjLZoZ5nuT6vl7RRv2pAbtR6TCNPHltoBteXwGagWY18wRVaz8nB S22XAmuLy7yrW3C/xHFu0SMZGnVwYzx3ZH9kjSaGSrf+f5qeHzl/CI2qsrhi0hW1MQWURQ0Etmk g01gl4yMVM+FMVpxUPxwncdBqwfmQf X-Received: by 2002:a05:6808:6f92:b0:45a:a7fd:cc9f with SMTP id 5614622812f47-466dca55caemr4030618b6e.18.1772934304258; Sat, 07 Mar 2026 17:45:04 -0800 (PST) Received: from [127.0.1.1] ([2600:8803:e7e4:500:a7b4:e550:6d81:e067]) by smtp.gmail.com with ESMTPSA id 5614622812f47-466dfa96903sm3244357b6e.13.2026.03.07.17.45.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Mar 2026 17:45:02 -0800 (PST) From: David Lechner Date: Sat, 07 Mar 2026 19:44:11 -0600 Subject: [PATCH v2 3/5] iio: buffer: cache timestamp offset in scan buffer 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: quoted-printable Message-Id: <20260307-iio-fix-timestamp-alignment-v2-3-d1d48fbadbbf@baylibre.com> References: <20260307-iio-fix-timestamp-alignment-v2-0-d1d48fbadbbf@baylibre.com> In-Reply-To: <20260307-iio-fix-timestamp-alignment-v2-0-d1d48fbadbbf@baylibre.com> To: Jiri Kosina , Jonathan Cameron , Srinivas Pandruvada , =?utf-8?q?Nuno_S=C3=A1?= , Andy Shevchenko , =?utf-8?q?Lars_M=C3=B6llendorf?= , Lars-Peter Clausen , Greg Kroah-Hartman Cc: Jonathan Cameron , Lixu Zhang , Francesco Lavra , linux-input@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, David Lechner X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=3645; i=dlechner@baylibre.com; h=from:subject:message-id; bh=/1MB53WxYwYrJxGtXUsGnlz4muAeJwSJR6MVneiXaRc=; b=owGbwMvMwMV46IwC43/G/gOMp9WSGDLXXGnqyeKWLQh2Fjt7evvBFn83yUbuv38ll7Fu8ncQr DC97s3dyWjMwsDIxSArpsjyRuLmvCS+5mtzbmTMgBnEygQyhYGLUwAmov6c/X/dzBA2wXOVW2OU Y/azp/yNmRhg17V9hkqC4SbXEmZR3zWuv6aHLu16KRrMdopb7ZBOac6G+YExGy4KCQZ2x2gYu16 0YFWPc2JuetA90zB0l83CYgdL0f3trHd8GzSuhE+ULMwLnbpcl3XF9/Wl4QI3i83+rZ3Oe1xKqq S/6nfCR/HXjIXxE/cHpTXbpS2yqftZvNFU2P6yzQal5m0FjfFyoobPzHnZOFeuM26aKDhb09dq1 0XfTUEl3EF1e2MF7qnFSPTpblkWeedCpr3iaeOZ5fUVv58m5HGon/lm5T7/5S2NnzauYSV+/+32 /H/esEbGzyH0zN0at+npy9bwXE4Kqln6xSzPhi/2QRYA X-Developer-Key: i=dlechner@baylibre.com; a=openpgp; fpr=8A73D82A6A1F509907F373881F8AF88C82F77C03 Cache the offset (in bytes) for the timestamp element in a scan buffer. This will be used later to ensure proper alignment of the timestamp element in the scan buffer. The new field could not be placed in struct iio_dev_opaque because we will need to access it in a static inline function later, so we make it __private instead. It is only intended to be used by core IIO code. Signed-off-by: David Lechner Reviewed-by: Nuno S=C3=A1 --- v2 changes: * Cache the timestamp offset instead of the largest scan element size. --- drivers/iio/industrialio-buffer.c | 14 +++++++++++--- include/linux/iio/iio.h | 3 +++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-b= uffer.c index 4e413b4bb073..ecfe0c9740e2 100644 --- a/drivers/iio/industrialio-buffer.c +++ b/drivers/iio/industrialio-buffer.c @@ -763,7 +763,8 @@ static int iio_storage_bytes_for_timestamp(struct iio_d= ev *indio_dev) =20 static int iio_compute_scan_bytes(struct iio_dev *indio_dev, const unsigned long *mask, bool timestamp, - unsigned int *scan_bytes) + unsigned int *scan_bytes, + unsigned int *timestamp_offset) { unsigned int bytes =3D 0; int length, i, largest =3D 0; @@ -785,6 +786,10 @@ static int iio_compute_scan_bytes(struct iio_dev *indi= o_dev, return length; =20 bytes =3D ALIGN(bytes, length); + + if (timestamp_offset) + *timestamp_offset =3D bytes; + bytes +=3D length; largest =3D max(largest, length); } @@ -846,7 +851,7 @@ static int iio_buffer_update_bytes_per_datum(struct iio= _dev *indio_dev, return 0; =20 ret =3D iio_compute_scan_bytes(indio_dev, buffer->scan_mask, - buffer->scan_timestamp, &bytes); + buffer->scan_timestamp, &bytes, NULL); if (ret) return ret; =20 @@ -890,6 +895,7 @@ struct iio_device_config { unsigned int watermark; const unsigned long *scan_mask; unsigned int scan_bytes; + unsigned int scan_timestamp_offset; bool scan_timestamp; }; =20 @@ -995,7 +1001,8 @@ static int iio_verify_update(struct iio_dev *indio_dev, } =20 ret =3D iio_compute_scan_bytes(indio_dev, scan_mask, scan_timestamp, - &config->scan_bytes); + &config->scan_bytes, + &config->scan_timestamp_offset); if (ret) return ret; =20 @@ -1153,6 +1160,7 @@ static int iio_enable_buffers(struct iio_dev *indio_d= ev, indio_dev->active_scan_mask =3D config->scan_mask; ACCESS_PRIVATE(indio_dev, scan_timestamp) =3D config->scan_timestamp; indio_dev->scan_bytes =3D config->scan_bytes; + ACCESS_PRIVATE(indio_dev, scan_timestamp_offset) =3D config->scan_timesta= mp_offset; iio_dev_opaque->currentmode =3D config->mode; =20 iio_update_demux(indio_dev); diff --git a/include/linux/iio/iio.h b/include/linux/iio/iio.h index 2c91b7659ce9..ecbaeecbe0ac 100644 --- a/include/linux/iio/iio.h +++ b/include/linux/iio/iio.h @@ -584,6 +584,8 @@ struct iio_buffer_setup_ops { * and owner * @buffer: [DRIVER] any buffer present * @scan_bytes: [INTERN] num bytes captured to be fed to buffer demux + * @scan_timestamp_offset: [INTERN] cache of the offset (in bytes) for the + * timestamp in the scan buffer * @available_scan_masks: [DRIVER] optional array of allowed bitmasks. Sor= t the * array in order of preference, the most preferred * masks first. @@ -610,6 +612,7 @@ struct iio_dev { =20 struct iio_buffer *buffer; int scan_bytes; + unsigned int __private scan_timestamp_offset; =20 const unsigned long *available_scan_masks; unsigned int __private masklength; --=20 2.43.0 From nobody Thu Apr 9 14:09:29 2026 Received: from mail-oi1-f182.google.com (mail-oi1-f182.google.com [209.85.167.182]) (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 663C2283159 for ; Sun, 8 Mar 2026 01:45:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.182 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772934308; cv=none; b=VaUy3z87eJ4+2YpmAB+pJ+G/8+YwxEbBUYNoW++GVRVmoBbNBD0x24NkI3oag0uLg+hYZ58ZtjW4hR7dC5MNl5BLa/j9OB4XKCel+39QAWS0jMkenoXsQbdkHfA5XdZnOZDaEDH+gZiuY5E/m8ww+s0EnbdF5MLcSoYg0+KhnaQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772934308; c=relaxed/simple; bh=KGGRE2l5CPjgVQf6RrPuOXOkhsx986TzL2/v8BKzEhs=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=YW7aYtXurCBjm+uN95pYfRgG4/8Bqkwd54r0vGTW37XyiQrPZX7pw0JwOyUmkZg2tixRYTE3vFmE5J2XEqxoPMq2sq6eMRvG5/QajXE3c0rn2HtNcNwWOV9z0p/esK2oLvXRNdGPNh2TU/kPZh4zpGiK+JS4GvFNtw5kjJOS7UY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=avbRg2Nb; arc=none smtp.client-ip=209.85.167.182 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="avbRg2Nb" Received: by mail-oi1-f182.google.com with SMTP id 5614622812f47-466f1c3c646so465638b6e.0 for ; Sat, 07 Mar 2026 17:45:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1772934306; x=1773539106; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=X+TAeqDEeBH+L0ImN7q+ZuCaBBTUct7hL3W/vdGYMsM=; b=avbRg2NbpEQmJDI0V9ij0HawrzKvT4eoiZuKVpFqHUpq+lch2jo+Lr6SPPhHu4v9Jd 5ktfHxm+ssFg0TTkcshRErbhW3qxF47sIxJsBKhuPpb/sdl4By35pBiUAYdIYXfhkZ5w /SHq/VhltBvT9gD/amEl3TPqT8PUms6edQf4Xf5PmOn8drJUA83qP2uvNhac6C9AU8n7 fpiWlqEnahhtp1hyyR8q8vWfyBR9rhtV5zoytfqffVDNRZtfO0LDfDPV3shzp2bJXmmG ERlBS01aZO6udLll3MiKo7Y7wGsiiUVRnI3ybOXD71l77kmVBdGsnWo9+LkWhQaDYxhD cdjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772934306; x=1773539106; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=X+TAeqDEeBH+L0ImN7q+ZuCaBBTUct7hL3W/vdGYMsM=; b=h45qfWxwQ+QhMI9YQfMU0xiVTycbidpIq4xTFGL0JxLX4wzY74oJMJKwZvoTZm/VuZ K28Km4wpv2AhkKtftfvVbB99omE09QN3LzkxqW0iQRz6KwZ+IoxELHhk69fOx6kmNb3/ xaY3gS3cu/3OGzik1kBQ0XFfpb3nWCDzsQdXmpuKSCbTKqLi/OJQGFJJLuJsF4W1prJf z/bDv7zV+v1DngG6ZZewABrWEerxssiraE5nt2HT5dkYFWPcCx/Bp6T1CovtlU2wqa8r 8Hkuquxkl2anXBZQWxH5UTdSOqUDfpulCl75PtuQQGMAzsqXX94mkFxCrBB+CiCoQxXP cLlQ== X-Forwarded-Encrypted: i=1; AJvYcCVHeOgfrWdW7GjzprLDCMOxxdpaZp+/QsKyrMk2eggtuUIRkcIFtkrDmbK0UsAPanue0t+fLQD4l4/7Hgc=@vger.kernel.org X-Gm-Message-State: AOJu0YysNOBvV5BOynXQXyK/Eg/5M7q8ZMUrdBm1cl+IHZ7cB/JH+39b M7a4Oshg1aRwCsVeNXX+m7MiuBg9OpbjhVuHsdimXaUkRGMB30LVsMt6zPCnX46iQmU= X-Gm-Gg: ATEYQzytP+nexoINTPXRGfxPLVcR8X3ade61ur/ylsW9joYsQ4RPXjWyIaYXtWQtMqX vk1D06QZi2Y/zuRlNONy9z5/A0enq2TY5YMvOd+OcAzKWs5DeGrSoODnYAX1G77ORGH6dHKO2Vm xCcaWcYbtPWioZuBqQ/yE2i0yiLWMK+PoImmjzHjth7RB1l2GWQKf9lX7cEfaWRye33pe+vYN+K COxlsKrrybFPTE8F6G76NPnTwycFOam43rK8PK0mxITgGEyC14f9ZE3dfgqcBFFaTqDtK5N9pjf vAAO/LaSwySFY3RE3oh40q47lNPpK/lQFA3st5HhV/Q5jX8qZ2jIe1HQ0nOWSykga6SPI3nMHHM HOCVsfFznVdGVZDBes8J4cG0vBAjlbr+sH49piQ+qKNgW4FlWhd+QeWolkBIM4YNc1Kes4XJQIY YDZOIxuhsQDTzOU6fdwj1WTlT0sQPd X-Received: by 2002:a05:6808:3a0f:b0:463:ae2a:ebec with SMTP id 5614622812f47-466dca4883emr4113475b6e.22.1772934306280; Sat, 07 Mar 2026 17:45:06 -0800 (PST) Received: from [127.0.1.1] ([2600:8803:e7e4:500:a7b4:e550:6d81:e067]) by smtp.gmail.com with ESMTPSA id 5614622812f47-466dfa96903sm3244357b6e.13.2026.03.07.17.45.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Mar 2026 17:45:04 -0800 (PST) From: David Lechner Date: Sat, 07 Mar 2026 19:44:12 -0600 Subject: [PATCH v2 4/5] iio: buffer: ensure repeat alignment is a power of two 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: quoted-printable Message-Id: <20260307-iio-fix-timestamp-alignment-v2-4-d1d48fbadbbf@baylibre.com> References: <20260307-iio-fix-timestamp-alignment-v2-0-d1d48fbadbbf@baylibre.com> In-Reply-To: <20260307-iio-fix-timestamp-alignment-v2-0-d1d48fbadbbf@baylibre.com> To: Jiri Kosina , Jonathan Cameron , Srinivas Pandruvada , =?utf-8?q?Nuno_S=C3=A1?= , Andy Shevchenko , =?utf-8?q?Lars_M=C3=B6llendorf?= , Lars-Peter Clausen , Greg Kroah-Hartman Cc: Jonathan Cameron , Lixu Zhang , Francesco Lavra , linux-input@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, David Lechner X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=1535; i=dlechner@baylibre.com; h=from:subject:message-id; bh=KGGRE2l5CPjgVQf6RrPuOXOkhsx986TzL2/v8BKzEhs=; b=owEBbQGS/pANAwAKAcLMIAH/AY/AAcsmYgBprNSI+w5dXqe1KCxUBkRwVln7cnSQ30EL5CJJI YxY1TzRbAGJATMEAAEKAB0WIQTsGNmeYg6D1pzYaJjCzCAB/wGPwAUCaazUiAAKCRDCzCAB/wGP wH4hB/90QpXGpaE2wP8nKyQz/wOwnUDGw6z5fgcxEBtazFY2zIWfUwisWjYYQyu35pJtx7Bw898 MUdE8yz3wQ77dhAL2cy36rCPInbcWktvIe4w94jnvzzBFvT5FeCOpNFvS3GJQpS6HY6qDb7iCbN wmjgCQnTJAUE8qtrYaevWBFMMiolx6JbQjwrj41/iORpBDwUB7Yp+Lju9zdTDbawi/auffhnbTE DmMScQWs9neU662/sMmXpqtR+mP/+KgiYW3BErGnzZnS4K40utpHfLTAnCTC1B5FMsK4E8oL0ij WzlyxVFeZoTLI3RgRk7Jeq1SKRruGvY/HGwqysIJQTUtq+uI X-Developer-Key: i=dlechner@baylibre.com; a=openpgp; fpr=8A73D82A6A1F509907F373881F8AF88C82F77C03 Use roundup_pow_of_two() in the calculation of iio_storage_bytes_for_si() when scan_type->repeat > 1 to ensure that the size is a power of two. storagebits is always going to be a power of two bytes, so we only need to apply this to the repeat factor. The storage size is also used for alignment, and we want to ensure that all alignments are a power of two. The only repeat in use in the kernel currently is for quaternions, which have a repeat of 4, so this does not change the result for existing users. Signed-off-by: David Lechner Reviewed-by: Nuno S=C3=A1 --- v2 changes: new patch In v1, Nuno made the point that if the size isn't a power of two, then the alignment won't be a power of two either. And this could cause unexpected problems regarding alignment in general. This will affect the work Francesco is doing with IIO_MOD_QUATERNION_AXIS which will have a repeat of 3, so this is a good time to think about this. --- drivers/iio/industrialio-buffer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-b= uffer.c index ecfe0c9740e2..c38da24561c0 100644 --- a/drivers/iio/industrialio-buffer.c +++ b/drivers/iio/industrialio-buffer.c @@ -748,7 +748,7 @@ static int iio_storage_bytes_for_si(struct iio_dev *ind= io_dev, bytes =3D scan_type->storagebits / 8; =20 if (scan_type->repeat > 1) - bytes *=3D scan_type->repeat; + bytes *=3D roundup_pow_of_two(scan_type->repeat); =20 return bytes; } --=20 2.43.0 From nobody Thu Apr 9 14:09:29 2026 Received: from mail-oi1-f176.google.com (mail-oi1-f176.google.com [209.85.167.176]) (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 656BB29E10F for ; Sun, 8 Mar 2026 01:45:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.176 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772934310; cv=none; b=Il8vebzemXG04doDknt3Ultl3u5eZmLaQIeJh3NPR2vhmLGGC6Pn6j0kJZVEL03V6XSqI1NGOo4+q3SSvFyza+duOtA/KYCvlBbjQP4frszLLgNkloGI4rz/j6rlYNrlcXkjZ7gcezA9gcr7UrNfNU9Sakoe0HBS2XmIKyygtr4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772934310; c=relaxed/simple; bh=w0Og34GEi1Pn7pz/AH2AqV6Spqskx9CZxx/s0UZTH+c=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=hC4p/ncYomNFQcWm7WHyVce3KgppO0972WN/8xbE6xN6p0ch3ca7EeBjNx21YOazOVIkJeVQ3fE1trhBKEeKTY+86heae9LHRLojXuWKxTs5jl1jGYqo1ljQ3I6BFyi9cwXSfrFxfd2sSf6VZCx6Hb0cz6AuL2g7nHU63DT+oH0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com; spf=pass smtp.mailfrom=baylibre.com; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b=QvchBrTQ; arc=none smtp.client-ip=209.85.167.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=baylibre.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=baylibre-com.20230601.gappssmtp.com header.i=@baylibre-com.20230601.gappssmtp.com header.b="QvchBrTQ" Received: by mail-oi1-f176.google.com with SMTP id 5614622812f47-466f00535cfso509099b6e.1 for ; Sat, 07 Mar 2026 17:45:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20230601.gappssmtp.com; s=20230601; t=1772934308; x=1773539108; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=2CGaHrjCmbnPLyL4ZyvnQXb7Y/JQNhM/tEkjcoUuxPI=; b=QvchBrTQnquNbz8bDuvk3Wy/bC3PW/J7Ocl4e5DGvthIKN7y4wgaVHjfh8s56+zuPO sTLIrLQJ/gGv0SdJiam87TUs57Uhtct6x6yI4Y82JxJsMgqs6sVyLgsvRjDOwst+SBEc VcNKpf8/FaK6qTl0JIU7lrT3rLiE0c+mRQ5mg+7vM0/AKRWbeo62GGTLlqmmmWekO8Rp CM0HBaVOaUrgMmkxQgAK8jnDIkJo5vzA2OHcOwsODlrKENf8mgiYl9Bqyq5bFe/3PONj pcWmvTDs0Rlq60Wm4E3yImnmBZSrGn0syn6vgwijZhEaC4U3dCWkkhwj44wFoKyB9WbO ml+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772934308; x=1773539108; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=2CGaHrjCmbnPLyL4ZyvnQXb7Y/JQNhM/tEkjcoUuxPI=; b=u3qlePXxIJvbRO3nFqz3puqLsKov4yRfdXu1eAaeWmNhOzWvB7ExL6ocT6DnatAmJg nTC2c01QKH1u5YgvRRRQ+aiyScfzXiXPn0KpVMNpF2IHLll7u/bijwmfOG0DZ6XKwTqy EHQiydXLW0pSXE8/YHaeknicfsCnfk1gOZXETt+1lkzovMfGlEoOz6uwWeig764Yw0WC 5NulfJ3jsJx2pR1B9owV3DFOwjLRByOCWzca7stQtVxwgAPb93Idz6Hk32k4eIUSseTX ng5j3Wg5e4v7NUtKhH+qscmOoCGnkHGoVlXjUPyhLdmmd/sWE2I7fbL0RbvtTeyICIQb 3oUQ== X-Forwarded-Encrypted: i=1; AJvYcCUyDNIYw8J1C3S0gFewIY8yOt5jvP9D+bN5Cr5eEfJd7iQsrGBGxRvb6l4Ii+2Gmau2LHL12r2HwtsBsIE=@vger.kernel.org X-Gm-Message-State: AOJu0Yx0OUolwHzs1o9LxSLRgf1N6ChjLChxP45u2W76q/KqvNfj4rQb ExK0dkJrVwmrJX+boRYq22+mO8Jp2bY7aL61e6BwMjyLmRkHJlWJMP2dG5XcA2AwrY7a+goYGh1 7ylRJ X-Gm-Gg: ATEYQzw8Bu2q5vi6hJx/TQxszQN49R7KWQJZDmtS7JVtdG5pqhMpbJFj33Qu1U2s51H CYTypauiXuVHsj3A2z9tyB8jeDUO+ra25NgwvVcdAkOiHn6BFbyhJkx7vrMsOa8+G3RX1zhI5M5 k5EGuHqeVnQOJIBSpTTFacTg6OA44lEcActTG2eRtTYD2760UwIH2MjS0wMaA50qr7+vpm7v2/c D9Sl0kE1sAD5xukYPiCih2dkc5wKyM/iDsrlTKd1K28ltLqgcOqPu+7seLCJZB0bbOaHIuKWGbV cqCJMpUEKd1RdYnsgKAHBjwKboddiT+hPCBanzfzeQHgkfkHKG3lfo6JjGQnEUt9OUnMOcQV3Vj PA3pJVWnZuN5JrO/2nqJgxxuj53j9UVR1iGK/4JiGbh+86AvM56LkGOZ+LfbQYvKiP8c6VXu5sK FkROv94GKkCU6JVlWZcQe11EkqzxkdoAHh7kD0CFE= X-Received: by 2002:a05:6808:a54a:20b0:45e:ada4:f2ba with SMTP id 5614622812f47-466dcb1aa0fmr2781061b6e.38.1772934308352; Sat, 07 Mar 2026 17:45:08 -0800 (PST) Received: from [127.0.1.1] ([2600:8803:e7e4:500:a7b4:e550:6d81:e067]) by smtp.gmail.com with ESMTPSA id 5614622812f47-466dfa96903sm3244357b6e.13.2026.03.07.17.45.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Mar 2026 17:45:07 -0800 (PST) From: David Lechner Date: Sat, 07 Mar 2026 19:44:13 -0600 Subject: [PATCH v2 5/5] iio: buffer: fix timestamp alignment when quaternion in scan 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: quoted-printable Message-Id: <20260307-iio-fix-timestamp-alignment-v2-5-d1d48fbadbbf@baylibre.com> References: <20260307-iio-fix-timestamp-alignment-v2-0-d1d48fbadbbf@baylibre.com> In-Reply-To: <20260307-iio-fix-timestamp-alignment-v2-0-d1d48fbadbbf@baylibre.com> To: Jiri Kosina , Jonathan Cameron , Srinivas Pandruvada , =?utf-8?q?Nuno_S=C3=A1?= , Andy Shevchenko , =?utf-8?q?Lars_M=C3=B6llendorf?= , Lars-Peter Clausen , Greg Kroah-Hartman Cc: Jonathan Cameron , Lixu Zhang , Francesco Lavra , linux-input@vger.kernel.org, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, David Lechner X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=openpgp-sha256; l=3379; i=dlechner@baylibre.com; h=from:subject:message-id; bh=w0Og34GEi1Pn7pz/AH2AqV6Spqskx9CZxx/s0UZTH+c=; b=owEBbQGS/pANAwAKAcLMIAH/AY/AAcsmYgBprNSPruc5cJFHnS3aL00cqgukj78lxIP6Uxqvq YxfRHlWNXiJATMEAAEKAB0WIQTsGNmeYg6D1pzYaJjCzCAB/wGPwAUCaazUjwAKCRDCzCAB/wGP wHA7CACcsWKER/egQdfZKlLPUp16lhhStL2FvmJ9UEsQmzoM1cI9FSIBa/G9/kJRYeM+fs36Ab+ imG1igOODLYZZoQfaR/AtT4avUA9JPGG8Xv/UVfRPq9UUS5Br8vxjlJXCuX5zcTVlsPXDmrBjmp ryG+2cpQF3cC733JZ3M3Uid5YV7/wr5yCKTpqppTj8A/BVk+4Q+BH60BxpEpxLPMY3UxWCMHnk3 62hiX7mbBmhfQRkDJzOhCGRy1OB8Pcj0/EDZrRtJG5RisEtoooA2jS7k1cGnUVUrTbuRLXZ0E8S v8deHr2r8HastiMcZJviIAiGBghNZD5mHcM0R0Qrv6yGzrQg X-Developer-Key: i=dlechner@baylibre.com; a=openpgp; fpr=8A73D82A6A1F509907F373881F8AF88C82F77C03 Fix timestamp alignment when a scan buffer contains an element larger than sizeof(int64_t). Currently s32 quaternions are the only such element, and the one driver that has this (hid-sensor-rotation) has a workaround in place already so this change does not affect it. Previously, we assumed that the timestamp would always be 8-byte aligned relative to the end of the scan buffer, but in the case of a scan buffer a 16-byte quaternion vector, scan_bytes =3D=3D 32, but the timestamp needs to be placed at offset 16, not 24. ts_offset is now a value in bytes so we have to change how the array access is done. Signed-off-by: David Lechner Reviewed-by: Nuno S=C3=A1 --- v2 changes: * Use cached timestamp offset instead of largest scan element size. * Mention change of ts_offset semantics in commit message. To test this, I used hid-sensor-rotation minus the first patch in the series so that we can see that the timestamp actually moved to the correct location. Before this patch, the timestamp (8 bytes ending with "98 18") is in the wrong location. 00000000 6a 18 00 00 ac f3 ff ff 83 2d 00 00 02 d3 ff ff |j........-....= ..| 00000010 00 00 00 00 00 00 00 00 5a 17 a0 2a 73 cb 98 18 |........Z..*s.= ..| 00000020 ad 17 00 00 6a f4 ff ff 35 2b 00 00 ca d0 ff ff |....j...5+....= ..| 00000030 00 00 00 00 00 00 00 00 2a a6 bb 30 73 cb 98 18 |........*..0s.= ..| 00000040 92 1e 00 00 50 ec ff ff ea c1 ff ff 78 f0 ff ff |....P.......x.= ..| 00000050 00 00 00 00 00 00 00 00 8f 3b a7 39 77 cb 98 18 |.........;.9w.= ..| After this patch, timestamp is now in the correct location. 00000000 55 0f 00 00 dd 1f 00 00 af 0b 00 00 ec 3e 00 00 |U............>= ..| 00000010 c7 17 68 42 6d d0 98 18 00 00 00 00 00 00 00 00 |..hBm.........= ..| 00000020 57 0e 00 00 c8 1f 00 00 d1 0e 00 00 42 3e 00 00 |W...........B>= ..| 00000030 56 a2 87 48 6d d0 98 18 00 00 00 00 00 00 00 00 |V..Hm.........= ..| 00000040 a3 e2 ff ff d3 1b 00 00 0b c9 ff ff cc 20 00 00 |............. = ..| 00000050 27 59 4d b3 72 d0 98 18 00 00 00 00 00 00 00 00 |'YM.r.........= ..| I also tested this with a different driver not affected by this bug to make sure that the timestamp is still in the correct location for all other drivers. --- include/linux/iio/buffer.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/include/linux/iio/buffer.h b/include/linux/iio/buffer.h index d37f82678f71..8fd0d57d238f 100644 --- a/include/linux/iio/buffer.h +++ b/include/linux/iio/buffer.h @@ -34,8 +34,16 @@ static inline int iio_push_to_buffers_with_timestamp(str= uct iio_dev *indio_dev, void *data, int64_t timestamp) { if (ACCESS_PRIVATE(indio_dev, scan_timestamp)) { - size_t ts_offset =3D indio_dev->scan_bytes / sizeof(int64_t) - 1; - ((int64_t *)data)[ts_offset] =3D timestamp; + size_t ts_offset =3D ACCESS_PRIVATE(indio_dev, scan_timestamp_offset); + + /* + * The size of indio_dev->scan_bytes is always aligned to the + * largest scan element's alignment (see iio_compute_scan_bytes()). + * So there may be padding after the timestamp. ts_offset contains + * the offset in bytes that was already computed for correctly + * aligning the timestamp. + */ + *(int64_t *)(data + ts_offset) =3D timestamp; } =20 return iio_push_to_buffers(indio_dev, data); --=20 2.43.0