Received: by 2002:a05:7412:37c9:b0:e2:908c:2ebd with SMTP id jz9csp3021409rdb; Fri, 22 Sep 2023 15:52:04 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEswLKITPw61QOtSZijI7rgZt2Dbx+UPGgv80VwMwHVh9KbDDISUXHhejd0w1Fev1rar0CE X-Received: by 2002:a17:90a:aa8b:b0:270:1611:484b with SMTP id l11-20020a17090aaa8b00b002701611484bmr961354pjq.41.1695423123471; Fri, 22 Sep 2023 15:52:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695423123; cv=none; d=google.com; s=arc-20160816; b=llbob6Db5WcfiOWUGEsHjMBIxDLa4IJnULWVyvaD1uwHWLNmbGTnFGWk+PainiPjjE zlvFtZk82j25q788f0lGl19yvS1teY6iS/OomIhXA08OY2YgjWNpTQkDPCOURcWta/qD DoAoT3ENZT4qWF+FJOmoRTEJ7Ea09o9AMD7n1qM974WB5FkadI14kIpaKd8VQiLPVY5n e0w1XohOvIWxuixmPyVZezYIcHKqQhdyilZ68Rd/sasBSsx7M1O7Yrkq1KsR8V9T0HIW Im/M04EqNDp1fSytSaXZHzIJ1qXooNw/bArqKUKy5yJzJfQf0F/bCr0SAq7gzsjwCV1A nl5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=9THroEikiL60Yi8fcuQ2zOCWbKMjxLA15+wfPD4+rk8=; fh=+XkJcXOKJ4Dl6AYTln3UzvhZHjSiM5yCnDKdoCP8e7U=; b=KH/Q051/AxiEY+ZM2QGNkCC6+mLzL5F3TZva71BcMIpv30/4g/uW+NnktpRaN4eY34 URwhmDMACoATrQ7r43wHduf2UhMVCXDgyNXkawnVxtJSE4BhyhuhjY5SYOTQqTS5TXQB 8aEbqSkyG21dTSJJZPLwwhQH2Q/Wm0sJZ/viF131eXcK3Omcd7BRvtDzJ16AZKr8EAyg /Z3rPo8faDrs1it4O++IcH7umXx5Bm86gSaZxyL68eUHtVs2ZoydzLt5KX+gZMnhBUt/ bN4qlA6DoWxsJFIXreqo9gPGWZHiuJ04Fib4QH+WjMWV27LJLF+oMorQOmgvln3G4gCm xF/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=XWcrk36L; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id c23-20020a17090a8d1700b0026b74f6aaa8si7059495pjo.63.2023.09.22.15.52.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 15:52:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=XWcrk36L; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id 33F0883CB594; Fri, 22 Sep 2023 04:16:30 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233539AbjIVLQ1 (ORCPT + 99 others); Fri, 22 Sep 2023 07:16:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46226 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230156AbjIVLQZ (ORCPT ); Fri, 22 Sep 2023 07:16:25 -0400 Received: from mail-lf1-x12a.google.com (mail-lf1-x12a.google.com [IPv6:2a00:1450:4864:20::12a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9F4A1AC; Fri, 22 Sep 2023 04:16:19 -0700 (PDT) Received: by mail-lf1-x12a.google.com with SMTP id 2adb3069b0e04-502e7d66c1eso3322084e87.1; Fri, 22 Sep 2023 04:16:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695381378; x=1695986178; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=9THroEikiL60Yi8fcuQ2zOCWbKMjxLA15+wfPD4+rk8=; b=XWcrk36LErSz9FQfU5G3Qc8MV4yZvGckoRZGh0S5w4xy76sNGCEz9OTMARfeVwu4+T pbXCXTQobWxq6sJGFMgjE1wnGNNUpKrUhTB8CExDTBhz97vzFZTweAuMEYwJNgrx03HF huW8XhcnCIGeZAf0X2LfD24C27B7F0GN65q1c2wVgJ/JhzE83yJTyLxYpdAjfO4Dyv1D IEdJOCv07HOPs7ZvnV40+ZwUbVPkMdzNEkKPfZUOKUD8hu/3X6KHwvlVgITzrR3md8xa FrA35w7+YLjMkTZpS/Fvh8Ubn4svsbFrOZYDq01IFiNo23VGZaikSTd0DVQUKxWizNom RKJA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695381378; x=1695986178; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=9THroEikiL60Yi8fcuQ2zOCWbKMjxLA15+wfPD4+rk8=; b=VpZMGa06c5YjCpGs1pbpgYG5IjoKOY0dEINyAF1elZ612vDwLrQ7WYgNC3daQ4WVip aZCcKIc/U7/HjEbnAFCoDigBAYL+v9+Ud9gcbt31RD5jxBADr3gvw6AmcOsOI0RNHyJm Q2jstiYvlb8ypr2ouL9019gBXk56y1q/+sJW0HQTN6Q5LiZ/PDyLAFztUqycm//q7x/j 2PrxysASKIbWioO/97cdkmSYPGuBT8w+coDWx2frylb8aBpzoyZoSlOO753fgkp/JdD9 Eq3oJCiEWCj9QZK/4hxnw/4ZLszxSytujEDR6FrllFyPj7V/gBMPbOmqgx5ffqVGIMH7 6EtQ== X-Gm-Message-State: AOJu0Yz5GueauLAMVVwy+T6q23G6L3OlIaA/ZufqjWNCufgR/SfIPnIv FZ9bjsocEW9+xhLwhZi266M= X-Received: by 2002:a05:6512:2013:b0:502:ffdf:b098 with SMTP id a19-20020a056512201300b00502ffdfb098mr6463433lfb.6.1695381377612; Fri, 22 Sep 2023 04:16:17 -0700 (PDT) Received: from dc78bmyyyyyyyyyyyyyyt-3.rev.dnainternet.fi (dc78bmyyyyyyyyyyyyyyt-3.rev.dnainternet.fi. [2001:14ba:16f8:1500::1]) by smtp.gmail.com with ESMTPSA id eq25-20020a056512489900b004fe1f1c0ee4sm696297lfb.82.2023.09.22.04.16.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Sep 2023 04:16:16 -0700 (PDT) Date: Fri, 22 Sep 2023 14:16:08 +0300 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Matti Vaittinen , Andy Shevchenko , Angel Iglesias , Andreas Klinger , Christophe JAILLET , Benjamin Bara , linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 1/6] tools: iio: iio_generic_buffer ensure alignment Message-ID: <029b4e3e18c76b330b606f5b14699e5ee4e5ed35.1695380366.git.mazziesaccount@gmail.com> References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="ubfT4fawWH07RnEo" Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Fri, 22 Sep 2023 04:16:30 -0700 (PDT) --ubfT4fawWH07RnEo Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable The iio_generic_buffer can return garbage values when the total size of scan data is not a multiple of largest element in the scan. This can be demonstrated by reading a scan consisting for example of one 4 byte and one 2 byte element, where the 4 byte elemnt is first in the buffer. The IIO generic buffert code does not take into accunt the last two padding bytes that are needed to ensure that the 4byte data for next scan is correctly aligned. Add padding bytes required to align the next sample into the scan size. Signed-off-by: Matti Vaittinen --- Please note, This one could have RFC in subject.: I attempted to write the fix so that the alignment is done based on the biggest channel data. This may be wrong. Maybe a fixed 8 byte alignment should be used instead? This patch can be dropped from the series if the fix is not correct / agreed. tools/iio/iio_generic_buffer.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/tools/iio/iio_generic_buffer.c b/tools/iio/iio_generic_buffer.c index 44bbf80f0cfd..fc562799a109 100644 --- a/tools/iio/iio_generic_buffer.c +++ b/tools/iio/iio_generic_buffer.c @@ -54,9 +54,12 @@ enum autochan { static unsigned int size_from_channelarray(struct iio_channel_info *channe= ls, int num_channels) { unsigned int bytes =3D 0; - int i =3D 0; + int i =3D 0, max =3D 0; + unsigned int misalignment; =20 while (i < num_channels) { + if (channels[i].bytes > max) + max =3D channels[i].bytes; if (bytes % channels[i].bytes =3D=3D 0) channels[i].location =3D bytes; else @@ -66,6 +69,16 @@ static unsigned int size_from_channelarray(struct iio_ch= annel_info *channels, in bytes =3D channels[i].location + channels[i].bytes; i++; } + /* + * We wan't the data in next sample to also be properly aligned so + * we'll add padding at the end if needed. TODO: should we use fixed + * 8 byte alignment instead of the size of the biggest samnple? + */ + misalignment =3D bytes % max; + if (misalignment) { + printf("Misalignment %u. Adding Padding %u\n", misalignment, max - misa= lignment); + bytes +=3D max - misalignment; + } =20 return bytes; } --=20 2.41.0 --=20 Matti Vaittinen, Linux device drivers ROHM Semiconductors, Finland SWDC Kiviharjunlenkki 1E 90220 OULU FINLAND ~~~ "I don't think so," said Rene Descartes. Just then he vanished ~~~ Simon says - in Latin please. ~~~ "non cogito me" dixit Rene Descarte, deinde evanescavit ~~~ Thanks to Simon Glass for the translation =3D]=20 --ubfT4fawWH07RnEo Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAEBCAAdFiEEIx+f8wZb28fLKEhTeFA3/03aocUFAmUNd3gACgkQeFA3/03a ocWxrAf+OjGyX8noJscOhtQe6df8AGR7ncgBjoEJfcbetKg7x8In5EzuCgdp7PBr lM7r/JvyzjTqbRGPSDaLfagdcxMBoBZGHYF9Kr0MeNwZjTwGOrp08XE8hfjgzzw6 rqt7YLw3hM/YY17Iy/JVmLeP/DHcxElSQkgoh4LN6wmrJUUhgel9oxiyyd2IXZi1 af1Svhcp4egFc8qd7LOn23YKWPZOdASQzY418oVJn0+MVkCQZ8j6Tg5VLtmrorsf TReq9MVPhFVAuJ3qtjSxZJ34bzMEaxgOS3OrIT47DQ0wbams6vYtcvcEMGrByvX+ t8fK00BIB8eKUfgcSLcj7J50hKFHFA== =aouA -----END PGP SIGNATURE----- --ubfT4fawWH07RnEo--