Message ID | ZRq9lm/C7g9m9zen@dc78bmyyyyyyyyyyyyydt-3.rev.dnainternet.fi |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2a8e:b0:403:3b70:6f57 with SMTP id in14csp1423744vqb; Mon, 2 Oct 2023 06:23:07 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGC90BetZEylmgyakoqLHQUmdA8uw0nX7ml51OYJhPafQcaaXZan8jB3Rn1+hJvV1Y0Kehb X-Received: by 2002:a05:6a20:2d09:b0:163:a8c9:76c4 with SMTP id g9-20020a056a202d0900b00163a8c976c4mr7164860pzl.4.1696252986376; Mon, 02 Oct 2023 06:23:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696252986; cv=none; d=google.com; s=arc-20160816; b=rQ6JiqNW+XSqdFElQob60V7hz5VVRM/eYjQQfojPah7IKKQ2rqksVXCqPCHxY3e3F7 1Uctm/0cvzdL/Joi19sTAEV48G9Wrl2BYVrfBJ3icQZI6bO3MHb40Dp9W6TVk8DPyOSz E7qSMNPDwI1mSmrjLNjO9hlQnIGhd3KkZAncRouBa0NjzAFaU1zkknXv54JnxZrGxw7W 6EbvoIi0quyzu7kpBYwWSHSyD1somOlbiauxLNSbOZnYxjHalDpW42ng1eD1k6MIkqLP bvY5ZJBlvuSKFP1ShDCxV+sGj45jANsUSDUHA9sCBQnRUWVJB/9Bu6eVUuI048aayJet m4Sg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-disposition:mime-version:message-id :subject:cc:to:from:date:dkim-signature; bh=nZJR14f5l2U6HTkmIyT91fkWqxzAoTmODmYIX5RU5wM=; fh=1h4YjSgDDTHCV92oKY/OJnApqRtzfV+67imdyGUaLZQ=; b=R6ups0HoQGCghHB31Ci/sC164HOq8fLt3Mv3f6b2/TEd48cTgzcoV9OWxs03aWaJjQ +oyTVYRxpDcNbdvJzRR2lw9Q3I+dK5oohHBh+Y06ZlUD0J3wnWxAEelNxHnQBktaL+7W 5IAiywaBruJfS80Ia7O5YXLeU+c7xXglrLXYhbZOcq9JfBhPa6ArU20iW8kp5ZnpLskW FWY46en7XOfW/Bw9lPKt2tgIxt+6QlrSv222DBCKPcQ8G1rjOlLjKDYI3rlPhym+WlWG RRk6gOgIftOEbtsnS+IpcQ/Vm3ENoD8dMzjVTwKWA6aOheT1e5iLXqZ3/daalMrh2+OM S6sA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=SZtp6GhZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id i2-20020a17090332c200b001c72b13a1cbsi13504230plr.352.2023.10.02.06.23.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 06:23:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=SZtp6GhZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 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 lipwig.vger.email (Postfix) with ESMTP id 2492F807EB9F; Mon, 2 Oct 2023 05:55:06 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237156AbjJBMy5 (ORCPT <rfc822;pusanteemu@gmail.com> + 18 others); Mon, 2 Oct 2023 08:54:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229986AbjJBMy4 (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Mon, 2 Oct 2023 08:54:56 -0400 Received: from mail-lj1-x22d.google.com (mail-lj1-x22d.google.com [IPv6:2a00:1450:4864:20::22d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4E8E5AD; Mon, 2 Oct 2023 05:54:53 -0700 (PDT) Received: by mail-lj1-x22d.google.com with SMTP id 38308e7fff4ca-2c15463ddd4so193727991fa.3; Mon, 02 Oct 2023 05:54:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1696251291; x=1696856091; darn=vger.kernel.org; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=nZJR14f5l2U6HTkmIyT91fkWqxzAoTmODmYIX5RU5wM=; b=SZtp6GhZCIc2GkzCIUiI3kQ65UDDTeXBfXscS5B86AuRk3j+cWuvsE36arPOKyxB2O 6NVFCt1I/ZKZf2qQdkA5QP2QUReuxwBd4YmHha1q1eDVLshI+7icsVY/bUZDJlm4Ol99 lZEUgG4/Im0/tJs5xnQjjUx/0oi4NoBGYN0hrv9xNVAMt61Ftk3EmedRcy9a48FpGuCO 5v4DulxZ73pJgkeOze5UMPtxIbMjUD5kypscvEnOg6Mp13K1+VMaFpIh4B8RScU/nXVw kkl/8Kf+7aCrB3vipSdOSpxGKe4bLGdbq8arh5wel4IgUI3Mb3qxnZw8N5/U1BJGjV5y d1vQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696251291; x=1696856091; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=nZJR14f5l2U6HTkmIyT91fkWqxzAoTmODmYIX5RU5wM=; b=fBYfNRNQm+QK6lv5dgR3hl0qPYYJFRyn5Tl6ZIBizxLxWWZAtWmtH7SU9ZBpjUzKVJ 2CE3jb4J0a67TbKIqzgQHdN/FXlmbL6OnuKIzpLfzE58YgZaJOJ4MLAVY8ojR8KKn0XD FeNASdV0ehRuXSycdUitqIEoRt+ucgw26aUbSOhl+mdvSDcwNK8cUcXecSNVSacFUZsd XYGy0nCzJrl2cVzTHLgEHXw773n2wRPk0xsx0xG7TTZ4o9e5wdbKLFjBhjJc0FVv2a8P qq4z9arNiBBIyiiukZo6lb2j2uZXKugZk3Q8SGYkMM8SZck4KQWo9hdmd5DKXYQ70XE8 Yiig== X-Gm-Message-State: AOJu0YyNEyDQIL1fANB3Xm+HJIl6q3fURhGiAdrMkh0SyDQtTcfzNuiy ZmjKT0MggnE80WMwdVtexVfgmmpJLr8= X-Received: by 2002:a2e:9a89:0:b0:2b6:d13a:8e34 with SMTP id p9-20020a2e9a89000000b002b6d13a8e34mr9770893lji.46.1696251291323; Mon, 02 Oct 2023 05:54:51 -0700 (PDT) Received: from dc78bmyyyyyyyyyyyyydt-3.rev.dnainternet.fi (dc78bmyyyyyyyyyyyyydt-3.rev.dnainternet.fi. [2001:14ba:16f8:1500::7]) by smtp.gmail.com with ESMTPSA id c4-20020a2e9d84000000b002bfb1cc89b7sm5300465ljj.110.2023.10.02.05.54.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 05:54:50 -0700 (PDT) Date: Mon, 2 Oct 2023 15:54:46 +0300 From: Matti Vaittinen <mazziesaccount@gmail.com> To: Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>, Matti Vaittinen <mazziesaccount@gmail.com> Cc: Matti Vaittinen <mazziesaccount@gmail.com>, Jonathan Cameron <jic23@kernel.org>, Lars-Peter Clausen <lars@metafoo.de>, linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v5] tools: iio: iio_generic_buffer ensure alignment Message-ID: <ZRq9lm/C7g9m9zen@dc78bmyyyyyyyyyyyyydt-3.rev.dnainternet.fi> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="uWQp3c5/bnyFA+y2" Content-Disposition: inline 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 lipwig.vger.email Precedence: bulk List-ID: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Mon, 02 Oct 2023 05:55:06 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1778650171059511892 X-GMAIL-MSGID: 1778650171059511892 |
Series |
[v5] tools: iio: iio_generic_buffer ensure alignment
|
|
Commit Message
Matti Vaittinen
Oct. 2, 2023, 12:54 p.m. UTC
The iio_generic_buffer can return garbage values when the total size of
scan data is not a multiple of the 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 element is first in the buffer.
The IIO generic buffer code does not take into account the last two
padding bytes that are needed to ensure that the 4-byte data for next
scan is correctly aligned.
Add the padding bytes required to align the next sample with the scan size.
Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com>
Fixes: e58537ccce73 ("staging: iio: update example application.")
---
Revision history
v4 => v5:
- drop unnecessary comment.
- drop all other patches as those were already applied.
- add Fixes-tag.
v3 => v4:
- drop extra print and TODO coment
- add comment clarifying alignment sizes
---
tools/iio/iio_generic_buffer.c | 13 ++++++++++++-
1 file changed, 12 insertions(+), 1 deletion(-)
base-commit: 0bb80ecc33a8fb5a682236443c1e740d5c917d1d
Comments
On 10/2/23 15:54, Matti Vaittinen wrote: > The iio_generic_buffer can return garbage values when the total size of > scan data is not a multiple of the 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 element is first in the buffer. > > The IIO generic buffer code does not take into account the last two > padding bytes that are needed to ensure that the 4-byte data for next > scan is correctly aligned. > > Add the padding bytes required to align the next sample with the scan size. > > Signed-off-by: Matti Vaittinen <mazziesaccount@gmail.com> > Fixes: e58537ccce73 ("staging: iio: update example application.") > > --- > Revision history > v4 => v5: > - drop unnecessary comment. > - drop all other patches as those were already applied. > - add Fixes-tag. > v3 => v4: > - drop extra print and TODO coment > - add comment clarifying alignment sizes > --- > tools/iio/iio_generic_buffer.c | 13 ++++++++++++- > 1 file changed, 12 insertions(+), 1 deletion(-) > > diff --git a/tools/iio/iio_generic_buffer.c b/tools/iio/iio_generic_buffer.c > index 44bbf80f0cfd..c1c037ee0071 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 *channels, int num_channels) > { > unsigned int bytes = 0; > - int i = 0; > + int i = 0, max = 0; > + unsigned int misalignment; > > while (i < num_channels) { > + if (channels[i].bytes > max) > + max = channels[i].bytes; > if (bytes % channels[i].bytes == 0) > channels[i].location = bytes; > else > @@ -66,6 +69,14 @@ static unsigned int size_from_channelarray(struct iio_channel_info *channels, in > bytes = channels[i].location + channels[i].bytes; > i++; > } > + /* > + * We wan't the data in next sample to also be properly aligned so I just realized I didn't fix the typo mentioned by Andy. I can re-spin this with wan't => want. Sorry! > + * we'll add padding at the end if needed. Adding padding only > + * works for channel data which size is 2^n bytes. > + */ > + misalignment = bytes % max; > + if (misalignment) > + bytes += max - misalignment; > > return bytes; > } > > base-commit: 0bb80ecc33a8fb5a682236443c1e740d5c917d1d
diff --git a/tools/iio/iio_generic_buffer.c b/tools/iio/iio_generic_buffer.c index 44bbf80f0cfd..c1c037ee0071 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 *channels, int num_channels) { unsigned int bytes = 0; - int i = 0; + int i = 0, max = 0; + unsigned int misalignment; while (i < num_channels) { + if (channels[i].bytes > max) + max = channels[i].bytes; if (bytes % channels[i].bytes == 0) channels[i].location = bytes; else @@ -66,6 +69,14 @@ static unsigned int size_from_channelarray(struct iio_channel_info *channels, in bytes = 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. Adding padding only + * works for channel data which size is 2^n bytes. + */ + misalignment = bytes % max; + if (misalignment) + bytes += max - misalignment; return bytes; }