Received: by 2002:a05:7412:3784:b0:e2:908c:2ebd with SMTP id jk4csp420680rdb; Sat, 30 Sep 2023 09:28:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IH9+qiVAUReFX08fpKQckcb7OuwrvyunfHd5kYapacCsrbt/PEiMHuiX7i1mTH8/im5fXxj X-Received: by 2002:a05:6a20:6a0b:b0:137:23f1:4281 with SMTP id p11-20020a056a206a0b00b0013723f14281mr8180063pzk.12.1696091302423; Sat, 30 Sep 2023 09:28:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1696091302; cv=none; d=google.com; s=arc-20160816; b=Yrd0dDxFTxJ4JrcHFP0Ww2afQLAHEd/ZG+73y7n8jj/rV+7oejfAReRJQU6CnDESPu gFHqrqlaCcC5nxp8bFecd0Jk5B2HqBhBNwR3Q6CY4UnzAfbvHPzh37e+8xmSnGSh8igO F2rue8SpCFz0VQ7FyIts27dIuJ2xPzzwVe1ApfnDoLPP+oKmnM9jD1+hz+LsxnzNHYB5 NLFiN2tJL8W9lklwsXaFPWs1BR9iZM2jIS2sX5foZC7ArJDaR0ANLWx6htHpn+pLTq3p D+RerHyAd/8bjDLk7UN3UB7nqPstOfmhdrZBbzwlec0zsxMvxnFQpFx5nCOmKaui8AsC maMg== 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=j7bxTTQsE5YdoUqx0+d3Yo9DvpTsdJrRHA+j4E9vrEY=; fh=nsaJ5EmY9cN4qdx4ePr4Ez64Y55kEfk4FbRI2w+1kyI=; b=MxgzfGwwi9Fge0z7EmThMv/85q3i5w+WzBEIo4X/sMgsuqpA62I0Pm2bC54tW50dI5 rwp1j1wequQgJ+EpwJOgpXriFaWofQEZESih+8/HEcb0nxeSFKUu76bmuuQrv73mLh7p yX/wgw09KOrf/XcYImMWK+yO+vf6RceMEnN+q6n/3WtGdAr5/Vz2dAl2ui3+R9sZvtjJ HQVUzffX+jVilq5zSQ4rlJKqXc0YKT94mHpRK+0b1FHrkpqMBfeZ3OJ2INfvmV3ZciMJ KFsGcM1l4CZkzhmVTNlg3atrTlTJgNSRA+T7jrvtYCtANa57TOcnfEDltSkt+9OEfkk4 vavQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=hYLOloRR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (howler.vger.email. [23.128.96.34]) by mx.google.com with ESMTPS id m8-20020a170902db0800b001c41515c4c7si16430591plx.115.2023.09.30.09.28.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 30 Sep 2023 09:28:22 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 as permitted sender) client-ip=23.128.96.34; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=hYLOloRR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.34 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 howler.vger.email (Postfix) with ESMTP id E6249829F662; Sat, 30 Sep 2023 09:27:43 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234508AbjI3Q1j (ORCPT + 99 others); Sat, 30 Sep 2023 12:27:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232221AbjI3Q1i (ORCPT ); Sat, 30 Sep 2023 12:27:38 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3D33DA4; Sat, 30 Sep 2023 09:27:36 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 54D51C433C7; Sat, 30 Sep 2023 16:27:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696091255; bh=+088iW0UMANAokgkBFuPTbVCjL9y7V6oanlnC+U+iW0=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=hYLOloRRyAyDqd3RnWhpqSTs2aKZdWjxLN9WNKZKxCWF+UPjCtNv7mm3dbHHq/xir hYCHposnFCGyT5M2l2VfE2GFPzvuECaFyRBiS+YQEqb/n7TzlvsdzQwdqWEX80vzL1 oIE1wAbR+iKK77FO7Axo3qN80lrE9soDQKGKiR44tb6R3Z2VGKvj1Di4OJLw2MxQ8Q sEhtfsHde9Fc/Hb/twpTKmZT5AHFhPcIfAfThJR2rjuzPfcvl8HHQYWTuoKIRBQT5M TbHoGRb4vPfjGBy6pKRFErhCuYS9J23MSnAsHBcCNz36MZqJMHln6eHL1E3IPHS04l eyfbQgFaTuXaA== Date: Sat, 30 Sep 2023 17:27:33 +0100 From: Jonathan Cameron To: Matti Vaittinen Cc: Jonathan Cameron , Matti Vaittinen , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski , Conor Dooley , 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: Re: [PATCH v3 1/6] tools: iio: iio_generic_buffer ensure alignment Message-ID: <20230930172733.23844ce7@jic23-huawei> In-Reply-To: <50587108-4ba7-3885-0669-7efaf5528233@gmail.com> References: <029b4e3e18c76b330b606f5b14699e5ee4e5ed35.1695380366.git.mazziesaccount@gmail.com> <20230924165737.54631dd3@jic23-huawei> <7ff22aa4-475c-b524-9f7a-f47ad02e940b@gmail.com> <20230925141629.00004522@Huawei.com> <50587108-4ba7-3885-0669-7efaf5528233@gmail.com> 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 (howler.vger.email [0.0.0.0]); Sat, 30 Sep 2023 09:27:44 -0700 (PDT) On Tue, 26 Sep 2023 13:29:02 +0300 Matti Vaittinen wrote: > On 9/25/23 16:16, Jonathan Cameron wrote: > > On Mon, 25 Sep 2023 10:01:09 +0300 > > Matti Vaittinen wrote: > > > >> On 9/24/23 18:57, Jonathan Cameron wrote: > >>> On Fri, 22 Sep 2023 14:16:08 +0300 > >>> Matti Vaittinen wrote: > >>> > >>>> 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 *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,16 @@ 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. TODO: should we use fixed > >>>> + * 8 byte alignment instead of the size of the biggest samnple? > >>>> + */ > >>> > >>> Should be aligned to max size seen in the scan. > >> > >> Or, maybe it should be > >> min(max_size_in_scan, 8); > >> ? > > > > Definitely not. If you are grabbing just one channel of 8 bit data, > > we want it to be tightly packed. > > I think that in this case the max_size_in_scan would be 1, and min(1, 8) > would be 1 as well, resulting a tightly packed data. I am just wondering > if we should use 8 as maximum alignment - eg, if our scan has 16 bytes > data + 1 byte data, we would add 7 bytes of padding, not 15 bytes of > padding. I am not sure what is the right thing to do. Ah I read that backwards as you've noticed. We don't have any such big channels, so indeed have some flexibility here. I think we stick to naturally aligned so 16 bytes case would be 16 byte aligned - mostly because I don't expect to see one any time soon and because it makes the docs simpler. > > > If we have a bug that already made that true then we might be stuck > > with it, but I'm fairly sure we don't. > >> > >> I think my suggestion above may yield undesirable effects should the > >> scan elements be greater than 8 bytes. (Don't know if this is supported > >> though) > > > > It is supported in theory, in practice not seen one yet. > > So, whether to unconditionally use largest scan element sized alignment > - or largest scan element up to 8 bytes - is a question we haven't hit > yet :) > > Actually, more I stare at the alignment code here, less sure I am it is > correct - but maybe I don't understand how the data should be aligned. > > I think it works if allowed data sizes are 1, 2, 4, and 8. However, I > suspect it breaks for other sizes. Indeed - it's meant to be power of 2 only. More than possible we don't check that rigorously enough or have it clearly documented though. The aim is for the data to be padded for efficient accesses + because it is a pain to deal with arbitrary padding so we restrict it to power of 2 naturally aligned only. One relaxation we've talked about in the past is packing multiple channels per byte (for logic analyser cases). Not done it yet though. > > For non power of2 sizes, the alignment code will result strange > alignments. For example, scan consisting of two 6-byte elements would be > packed - meaning the second element would probably break the alignment > rules by starting from address '6'. I think that on most architectures > the proper access would require 2 padding bytes to be added at the end > of the first sample. Current code wouldn't do that. > > If we allow only power of 2 sizes - I would expect a scan consisting of > a 8 byte element followed by a 16 byte element to be tightly packed. I'd > assume that for the 16 byte data, it'd be enough to ensure 8 byte > alignment. Current code would however add 8 bytes of padding at the end > of the first 8 byte element to make the 16 byte scan element to be > aligned at 16 byte address. To my uneducated mind this is not needed - > but maybe I just don't know what I am writing about :) 16 byte alignement is probably not needed - but who knows for future architecture. Note this has been really non obvious in the past and is why we force alignment for timestamp elements in lots of drivers. Most architectures align an s64 in a c structure to an 8 byte boundary but x86 32 bit doesn't. Hence we have to force it all over the place. Fixing that (because userspace ABI data placement should not vary across architectures) was a pain. I don't want to do it again! > > In any case, the patch here should fix things when allowed scan element > sizes are 1, 2, 4 and 8 and we have to add padding after last scan > element. It won't work for other sizes, but as I wrote, I suspect the > whole alignment code here may be broken for other sizes so things > shouldn't at least get worse with this patch... I think this should be > revised if we see samples of other sizes - and in any case, this might > at least warrant a comment here :) (I reserve a right to be wrong. > Haven't been sleeping too well lately and my head is humming...) Might be worth a power of 2 only comment. J