Received: by 2002:a05:7412:518d:b0:e2:908c:2ebd with SMTP id fn13csp397810rdb; Thu, 5 Oct 2023 09:01:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEqsRkGxFo4parSNm/rioMbphaR42qADDFUAVCJA6UobEy/ksXobt1+NplEO2VbBggndl0O X-Received: by 2002:a17:902:ec84:b0:1c5:d063:b72a with SMTP id x4-20020a170902ec8400b001c5d063b72amr6696397plg.27.1696521660965; Thu, 05 Oct 2023 09:01:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696521660; cv=none; d=google.com; s=arc-20160816; b=v9lb0+yYbB9iC5bA8DOcAVUMhzeX2DS3sm6SKUY9Ged7Gpka1YUZyg0NEABqUCU5Xu 7/1/50bg1AT+9rcKpJKVjTe3tYtM+1Y5ryzk819hbgfiqXGCpmB58fZYZHRKGCL7Wef/ PQXc9AYZ32zeY+YuVQ9M5TixdwJ4lsIuZ/iM1WKPgFqbnkWVlXpjCva4EjMmoSAscVmh modapS+XvviN76xrhc2iqlMHCQ5LjaSXfcnIbQID+3bnaS4ZKL9/55cQFnZ7v4dUFZrS v26rIjm2O3NVXuHZyGYCgRU23WRrqXhxZlE9MOcmQQ9YKLF1beLZkpopNKjhjXFcdnJf WQAQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=urhzKfFjoZFBx3D72cYR814YbUp/BJQ19duaM3zHudg=; fh=/VP8wnGJxj4RZEiUggbFzzqmab1L5RcmHMz1AJF0GB8=; b=osnZg5HARo9LlDWWZiK8uu9qWjHTH7J+y0YPV07W9tE8vQCTr2sRicpku/mPdyIUuJ Chuz64eMIVxyIDpKAs9moIDlZfFGJEgoOCmIW0oHL84IzAMH8fgcN9blIuvRwbECqklQ aZ8OtEi+82dWMWKHxvsdiCDZP1pUd5fThTpby1FtHF2o6dErNKUHdvhtFvypDT0Npl7n F65Cy+W9H3EEsiMgf5OyFiR7DJNXLb1iwlzc51TSN5XF3cq8CGj5KwW//m80ndOn25c0 flo5rf1d2qAruczoShsH5FxpqqIqeVCMnFuobaRUEogLJwN6zb+Zeb3U415TXf/IiLAL hwfw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RmrQmJWs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id kv4-20020a17090328c400b001ae40e07fb0si1677740plb.216.2023.10.05.09.01.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 05 Oct 2023 09:01:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=RmrQmJWs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id 0E61E80781F9; Thu, 5 Oct 2023 09:00:29 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235245AbjJEQAS (ORCPT + 99 others); Thu, 5 Oct 2023 12:00:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37758 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237198AbjJEP5g (ORCPT ); Thu, 5 Oct 2023 11:57:36 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C9F95FEB; Thu, 5 Oct 2023 08:17:13 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3EB54C433C7; Thu, 5 Oct 2023 15:17:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696519033; bh=CaTNBuS9Fb+l4tmi5Xy/KGFrITEbvOikbxtS6uzbucQ=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=RmrQmJWsiOI0JOKuWZWH5xdVVFwz84p4aSCWCLx4AbpCab9LDFs+I7CKPX4pWDy71 y8S1ua7EdGnzFqblYc7vfLslKvpbicVKf9a9jx2Gi7UZH1Brwsin7U39NGSrlz2bjg 2CpMIAyxKbfSrs2ySjKKLzB6X7JCNECwTURpmEjjIEt8h/QU+wU5uOjfSGReSFWqMy VSBZ9Ecczyox3r88Si/HGCH96nlwsN2/7kREX5rHFrLqIfEICs2WqnnKXxMHFBcjTA YByJwxMeeQy08EwPtvKSZhIocbYJx1EeCn1a7QrifspH0K1HkhDFgm8dKDOwnz2qhM 4Py4qq+UtZvlg== Date: Thu, 5 Oct 2023 16:17:18 +0100 From: Jonathan Cameron To: Matti Vaittinen Cc: Matti Vaittinen , Lars-Peter Clausen , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v6] tools: iio: iio_generic_buffer ensure alignment Message-ID: <20231005161718.612fab8c@jic23-huawei> In-Reply-To: References: X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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 (snail.vger.email [0.0.0.0]); Thu, 05 Oct 2023 09:00:29 -0700 (PDT) On Tue, 3 Oct 2023 12:57:47 +0300 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 > Fixes: e58537ccce73 ("staging: iio: update example application.") Applied to the togreg branch of iio.git and pushed out as testing for all the normal reasons. Thanks, Jonathan > > --- > Revision history > v5 => v6: > - fix typo pointed by Andy (wan't => want). > 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..0d0a7a19d6f9 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 want 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; > } > > base-commit: 0bb80ecc33a8fb5a682236443c1e740d5c917d1d