From patchwork Tue Apr 30 12:44:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 13648932 Received: from mail-lf1-f53.google.com (mail-lf1-f53.google.com [209.85.167.53]) (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 2297913F43D; Tue, 30 Apr 2024 12:45:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714481102; cv=none; b=Cn4ER+HYIKUihI4qRZimWwdRmBSJaprKdrPTCMGNfN12G/5CyMt+rvz5g6HBJMq6npUHmV+WtqSV3b4bEjaCSX6epDQtYAEfdr1wCUci3EQVK2ofS4dkroB4wkaOXgvCoz12MkrrWi+eBH6Tkacy84UzP5F3K3xyhThLF6bE0Gw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714481102; c=relaxed/simple; bh=KIcNRVtmmQrdXcBKcIpo7M4EDNJemAxWuf3q7+mhCzE=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=geDMiR8zFAhcUkacgdiMNu3M6rMJGBZ/h7qY3wZ/QshObJKhaT8JGh+tVeix5g6/TXjCsLE1TakUQlqxhQWq01iv8lnIhxBSEA/HAZ4KRK5SipDmfMFgX1uVZz4nQGD7/WNzMLbd+A7bTS5qS68I8Ot2R2K/bUfOKJEQSBsSrII= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=cbRiJ3s8; arc=none smtp.client-ip=209.85.167.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="cbRiJ3s8" Received: by mail-lf1-f53.google.com with SMTP id 2adb3069b0e04-51967f75729so6694020e87.0; Tue, 30 Apr 2024 05:45:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714481099; x=1715085899; 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=FZfLTpIjJmVaTYS7lbwGBbdoLSbg7AHbUY1X0ThbnTM=; b=cbRiJ3s8fMekHfdWzc2oq5JAmPWtKu1LUfyouCIskto8bwHpnnIm1MMweZ41M137Fc ulncvLZhunlSOQZ36dyPyqEm31qe4ATZeIWdxuJxUYhmaWgGd8PirJZGZyUyI770KqL5 syd3vWizAgLj2m8so31bfe7wqBp+zCnT52kVj0gdmCXqjWR7+OybILLwzWAxOwqBUBm2 rIdgGg5gJr65NCGbG3p96IGt4LyKCC2xflEQHQ+ea/IJYLInxyAmBLflI+1am7HidZ1i fehUoz7A2FmmG9pMG1PD4GLdJTXl1U7+J73j52OXBAWyRwIFLdWDdIr0Kze7YxZbiBn4 PLAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714481099; x=1715085899; 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=FZfLTpIjJmVaTYS7lbwGBbdoLSbg7AHbUY1X0ThbnTM=; b=Cnp23Z9W5F7tBaWd3IxSi5gp+6oftPZr/EraufPzghN2NyvYLvwHgQJJscw6XUGg3U yPI7SrLXwQ/rmWvlzrqcnBxwDXSGuBXvX64lEFYcQ1clMQtf2QFWRqGHNi6WMHUX37Op Xd0VMolAaA6M+bGwIKrCTPMsLSiJYB1/Ycq3po0AICsDI3IuWFJEPAfjcwDh/3SX0+fg CR5ySqEyLQh1juAYpQjZditGY+k1VN1GbLYB+xjRs1nmR3nPXUB3nnnn3XxxOzFAWBOP MRvEtGIxYLY7/iwEO6IgyxH9zztGfkCeyAYRlAlGCoYifFDBqKbO3t2Qe30smK0jOJ70 dnAg== X-Forwarded-Encrypted: i=1; AJvYcCWF7TLPicGRz1BrRJPSiIuUy81h5euI1H0rctU29eLZq9SurKgaF5Ci4tLOwIapZJm4v9zdzzaJ/jvzIvg1oeSSmZOLQ3/0opZqq+YYhhDLgU9NpDyq5pV4qWkY4ggUyyxWoROQdXnE X-Gm-Message-State: AOJu0YyBuL5TeTvktuYeBaeU0uF8XLJWCS1jfqTyJ1nUfdJDeAhaah+c ehVHIfBaSL1vIv1yDciBzRruIkTDwsDDWuj8swMSUiGUhxtqSk4Y+4OEWg== X-Google-Smtp-Source: AGHT+IH75htWU9/5SwbMUu5MZuUwDRxz7PZdTtsvnZ4wkdMYhx9Bonhb+TgSenMUtfeYhdGqRsXd6A== X-Received: by 2002:ac2:5f6b:0:b0:51b:aa42:67c5 with SMTP id c11-20020ac25f6b000000b0051baa4267c5mr1515541lfc.60.1714481099066; Tue, 30 Apr 2024 05:44:59 -0700 (PDT) Received: from fedora ([213.255.186.46]) by smtp.gmail.com with ESMTPSA id y23-20020a196417000000b0051c3aea9c3csm2055536lfb.211.2024.04.30.05.44.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Apr 2024 05:44:57 -0700 (PDT) Date: Tue, 30 Apr 2024 15:44:53 +0300 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Matti Vaittinen , Jonathan Cameron , Lars-Peter Clausen , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Chenyuan Yang Subject: [PATCH v2 1/2] iio: Fix the sorting functionality in iio_gts_build_avail_time_table Message-ID: References: Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: From: Chenyuan Yang The sorting in iio_gts_build_avail_time_table is not working as intended. It could result in an out-of-bounds access when the time is zero. Here are more details: 1. When the gts->itime_table[i].time_us is zero, e.g., the time sequence is `3, 0, 1`, the inner for-loop will not terminate and do out-of-bound writes. This is because once `times[j] > new`, the value `new` will be added in the current position and the `times[j]` will be moved to `j+1` position, which makes the if-condition always hold. Meanwhile, idx will be added one, making the loop keep running without termination and out-of-bound write. 2. If none of the gts->itime_table[i].time_us is zero, the elements will just be copied without being sorted as described in the comment "Sort times from all tables to one and remove duplicates". For more details, please refer to https://lore.kernel.org/all/6dd0d822-046c-4dd2-9532-79d7ab96ec05@gmail.com. Reported-by: Chenyuan Yang Suggested-by: Matti Vaittinen Fixes: 38416c28e168 ("iio: light: Add gain-time-scale helpers") Signed-off-by: Chenyuan Yang Co-developed-by: Matti Vaittinen Signed-off-by: Matti Vaittinen --- Revision history: v1 => v2: - Fix the sender and From: tag - Drop the Co-developed-by tag from original author Original commit (by Chenyuan Yang) was amended by me to remove duplicates so that the user-space callers do not get multiple instances of same time as was discussed here: https://lore.kernel.org/all/a59061f8-5caa-43d4-bd4f-5ac4c39515ba@gmail.com/ --- drivers/iio/industrialio-gts-helper.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/drivers/iio/industrialio-gts-helper.c b/drivers/iio/industrialio-gts-helper.c index b51eb6cb766f..59d7615c0f56 100644 --- a/drivers/iio/industrialio-gts-helper.c +++ b/drivers/iio/industrialio-gts-helper.c @@ -362,17 +362,20 @@ static int iio_gts_build_avail_time_table(struct iio_gts *gts) for (i = gts->num_itime - 1; i >= 0; i--) { int new = gts->itime_table[i].time_us; - if (times[idx] < new) { + if (idx == 0 || times[idx - 1] < new) { times[idx++] = new; continue; } - for (j = 0; j <= idx; j++) { + for (j = 0; j < idx; j++) { + if (times[j] == new) + break; if (times[j] > new) { memmove(×[j + 1], ×[j], (idx - j) * sizeof(int)); times[j] = new; idx++; + break; } } } From patchwork Tue Apr 30 12:45:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 13648933 Received: from mail-lj1-f180.google.com (mail-lj1-f180.google.com [209.85.208.180]) (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 7BFFA171088; Tue, 30 Apr 2024 12:45:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.208.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714481135; cv=none; b=hZOUjcpxjSC9EHDeJ3jlFZqwvZxvesiPf+a62AjN7Fdo+XyI1WdwlPKo48RhkuxRSeMUTiqr0f7SujMgeuRl1AwFJ939xFVDKFTddb1vpr6L8Q+Sf+ySc/S556fchdKLDBB0At1aNiEHMZPkbIy3rWoQPZ/61otRrkJa5hIz3HI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714481135; c=relaxed/simple; bh=KSqsEM/ELNKUOxxne+/cC9lsGDSIHr6Ga7Be3GqcqW0=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=nab2UDDu1uRt2Xvjy1eG/8WABX8bCWiK9luo1hmmuyDyT/IH+M/rL3RXxyuVWSpCcVRFz2In7wmtEnpZQUHo5pg5kfLRe73MwexAEepcFEvW86ES/iNOl1pA3POPUMmxMwseCkIT1o3ranCuyxh/rNYrpdK1oD5seHYhqoJIkWM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=jp3Es5A+; arc=none smtp.client-ip=209.85.208.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="jp3Es5A+" Received: by mail-lj1-f180.google.com with SMTP id 38308e7fff4ca-2e0a2870bceso25786301fa.2; Tue, 30 Apr 2024 05:45:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1714481131; x=1715085931; 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=819KSoS21UAhzQAUss6vDrKjXlCt6LuTDp8QhzT3swc=; b=jp3Es5A+/vJDIURGue7PP9MTzfWFVwgtMtfsuZZked2rR09bkdVWUjJkdq6BtixFrg k56zWfn8/qF6aWUaiiD3aA1vBMjMI/JZowmGzaSFSAInmDaVGEsMhsmUqX6bG0MhfwyG nUkhpJl+L3qgDO2GIhhXngC3EiCyUu/T8Aev04/TL1g+zo+XCS0+VaGTSsZE8C4kf5ts 1QXfGF91+a9mJ0ciBGFCLQRK+SJNT4Z2LXqmkx7M9fwbhdykfNVfCuMMMvrh6uwddvJY cTgAg1egb5INbpS5iJSJ5Os5BqLpzPZ1Sfa097CHl3br2TS73mtFC09a2U5xZBr+AaHm E7XQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1714481131; x=1715085931; 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=819KSoS21UAhzQAUss6vDrKjXlCt6LuTDp8QhzT3swc=; b=QHXngc5PFFFgH0txHDO/4T4gLbqNx9YfjE9rmeWl0UuoNP1qcPPK6CnEIF9aUdYtHy uwHISPXq3qxfu15Ukd/V8xVhYiYsMgG8A6AknNdbWIWyVbFzugoHtD9ciUxzFo87WFpo EnZnmXcLMNynYlkxaJYmubn+m8n86bpuvIxf/FqWkNz9lfNyO9/ks5OokyJOZs9vX8Q7 nMMlEDf7jLks94P6bOURcLLHAQZuryjOKVOY/o9u6gQPstDA6s7oor1Ug/s3xcQn3Hc7 82ylt6UHTmIlU57W7UW+E4NgXDqTT6byQJsWuiOmHNI30SFhya5A9CHSQtgBS0zr0GRu Jz/w== X-Forwarded-Encrypted: i=1; AJvYcCVp4Zqm5Dx5M9CfRDknCzQua7fApPs/QeiqOxDxfxBMnv7rTdJY3tkwRfkWAwFiYzeKA6m0dlWi/TsKG9vY89AsyDX/fR5JiY/YD0jU8iWYkE1A0QLc2iyPClvh9OWi0nyArL+bS578 X-Gm-Message-State: AOJu0YyHUcivpO/vfsxLRibfAjOXfPap5ZDpYDDQ7QjOi0U4KOzly0rR 0JnyXh7XCVoHVOuESJZDktJp+2LjXWtqqz/dD7FA89sqnhplKGs3 X-Google-Smtp-Source: AGHT+IEwGHyM4P0p+YnR0uTivBpNct05kOjmg38WblTfBy+HqnoPf2fkC8vVcVkW86SOVRRm4tcjOA== X-Received: by 2002:a2e:8608:0:b0:2d2:f5fa:f37e with SMTP id a8-20020a2e8608000000b002d2f5faf37emr6038466lji.51.1714481126553; Tue, 30 Apr 2024 05:45:26 -0700 (PDT) Received: from fedora ([213.255.186.46]) by smtp.gmail.com with ESMTPSA id a8-20020a2e9808000000b002d9f8183e0esm3888502ljj.81.2024.04.30.05.45.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Apr 2024 05:45:25 -0700 (PDT) Date: Tue, 30 Apr 2024 15:45:21 +0300 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Matti Vaittinen , Jonathan Cameron , Lars-Peter Clausen , linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org, Chenyuan Yang Subject: [PATCH v2 2/2] iio: test: gts: test available times and gains sorting Message-ID: <365cc6de3c17a457db738f5fdf8dd3bd6f50d5f2.1714480171.git.mazziesaccount@gmail.com> References: Precedence: bulk X-Mailing-List: linux-iio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: The iio_gts helpers build available times and scales tables based on the times and gains arrays given from the driver. The driver should be able to list all valid register values so that conversion from register valu to correct gain/time works for all supported register values. It might be more convenient for drivers to list these times and gains in the order where they're listed in the data-sheet than ascending order. However, for user who requests the supported scales / times it is more convenient to get the results in asscending order. Also, listing duplicated values is not meaning for the user. Hence the GTS heler should do sorting and deduplication of the scales and times when it builds the tables listing the available times/scales. Note, currently duplicated gain values aren't handled by GTS-helpers. Unsort the gain and time arrays in the test code, and add duplicates to time array in order to test the sorting and deduplicating works. Signed-off-by: Matti Vaittinen --- Revision history: - No changes drivers/iio/test/iio-test-gts.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/drivers/iio/test/iio-test-gts.c b/drivers/iio/test/iio-test-gts.c index cf7ab773ea0b..5f16a7b5e6d4 100644 --- a/drivers/iio/test/iio-test-gts.c +++ b/drivers/iio/test/iio-test-gts.c @@ -70,6 +70,7 @@ */ static struct iio_gts gts; +/* Keep the gain and time tables unsorted to test the sorting */ static const struct iio_gain_sel_pair gts_test_gains[] = { GAIN_SCALE_GAIN(1, TEST_GSEL_1), GAIN_SCALE_GAIN(4, TEST_GSEL_4), @@ -79,16 +80,17 @@ static const struct iio_gain_sel_pair gts_test_gains[] = { GAIN_SCALE_GAIN(256, TEST_GSEL_256), GAIN_SCALE_GAIN(512, TEST_GSEL_512), GAIN_SCALE_GAIN(1024, TEST_GSEL_1024), - GAIN_SCALE_GAIN(2048, TEST_GSEL_2048), GAIN_SCALE_GAIN(4096, TEST_GSEL_4096), + GAIN_SCALE_GAIN(2048, TEST_GSEL_2048), #define HWGAIN_MAX 4096 }; static const struct iio_itime_sel_mul gts_test_itimes[] = { - GAIN_SCALE_ITIME_US(400 * 1000, TEST_TSEL_400, 8), - GAIN_SCALE_ITIME_US(200 * 1000, TEST_TSEL_200, 4), GAIN_SCALE_ITIME_US(100 * 1000, TEST_TSEL_100, 2), + GAIN_SCALE_ITIME_US(400 * 1000, TEST_TSEL_400, 8), + GAIN_SCALE_ITIME_US(400 * 1000, TEST_TSEL_400, 8), GAIN_SCALE_ITIME_US(50 * 1000, TEST_TSEL_50, 1), + GAIN_SCALE_ITIME_US(200 * 1000, TEST_TSEL_200, 4), #define TIMEGAIN_MAX 8 }; #define TOTAL_GAIN_MAX (HWGAIN_MAX * TIMEGAIN_MAX)