From patchwork Sun Jun 13 15:10:36 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jonathan Cameron X-Patchwork-Id: 12317739 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D1B29C48BE8 for ; Sun, 13 Jun 2021 15:08:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id AA2DB611C0 for ; Sun, 13 Jun 2021 15:08:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231782AbhFMPKv (ORCPT ); Sun, 13 Jun 2021 11:10:51 -0400 Received: from mail.kernel.org ([198.145.29.99]:59596 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231841AbhFMPKv (ORCPT ); Sun, 13 Jun 2021 11:10:51 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 995376128A; Sun, 13 Jun 2021 15:08:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1623596929; bh=TftDME3258diEPPsF5wBkvYXL7CwJi11GQ4g7EHXE28=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nTmzIc0vP6dSWnukkuLtzGPlmDxAu5yqzVmnB+D7FyfvRky55GOFQ0eUugOcQTUZH WhDXsJ5kG91GkuWXVfLbm3GcRtosg99Us7oOPLCmIsau15uOkYC8YBIm5YjOm4x5f/ PK+8brVU2SKXeOpq9siGaP/SszPgipkc//6kv+tfcrPpd+7qFxTLvr1kyUXQ5ZXYsN WGQdICoVphs4GPn9T3B/eHUFn/deNMPTER135gt86OTzdt4q+dcmYoRdX7Vnf2ROrr kXncA+aFRnLm1V9bx9RNnM67zaa7qX8re8vjWLZr3t/tQ0qf/cdGzlI0CGrfQPYqNe 67lh+rlt8YELA== From: Jonathan Cameron To: linux-iio@vger.kernel.org, Andy Shevchenko , Nuno Sa Cc: Linus Walleij , Jan Kiszka , Jonathan Cameron Subject: [PATCH v2 1/4] iio: core: Introduce iio_push_to_buffers_with_ts_unaligned() Date: Sun, 13 Jun 2021 16:10:36 +0100 Message-Id: <20210613151039.569883-2-jic23@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210613151039.569883-1-jic23@kernel.org> References: <20210613151039.569883-1-jic23@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Jonathan Cameron Whilst it is almost always possible to arrange for scan data to be read directly into a buffer that is suitable for passing to iio_push_to_buffers_with_timestamp(), there are a few places where leading data needs to be skipped over. For these cases introduce a function that will allocate an appropriate sized and aligned bounce buffer (if not already allocated) and copy the unaligned data into that before calling iio_push_to_buffers_with_timestamp() on the bounce buffer. We tie the lifespace of this buffer to that of the iio_dev.dev which should ensure no memory leaks occur. Signed-off-by: Jonathan Cameron Reviewed-by: Nuno Sá --- drivers/iio/industrialio-buffer.c | 46 +++++++++++++++++++++++++++++++ include/linux/iio/buffer.h | 4 +++ include/linux/iio/iio-opaque.h | 4 +++ 3 files changed, 54 insertions(+) diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c index fdd623407b96..5241b5a5c6c0 100644 --- a/drivers/iio/industrialio-buffer.c +++ b/drivers/iio/industrialio-buffer.c @@ -1730,6 +1730,52 @@ int iio_push_to_buffers(struct iio_dev *indio_dev, const void *data) } EXPORT_SYMBOL_GPL(iio_push_to_buffers); +/** + * iio_push_to_buffers_with_ts_unaligned() - push to registered buffer, + * no alignment or space requirements. + * @indio_dev: iio_dev structure for device. + * @data: channel data excluding the timestamp. + * @data_sz: size of data. + * @timestamp: timestamp for the sample data. + * + * This special variant of iio_push_to_buffers_with_timestamp() does + * not require space for the timestamp, or 8 byte alignment of data. + * It does however require an allocation on first call and additional + * copies on all calls, so should be avoided if possible. + */ +int iio_push_to_buffers_with_ts_unaligned(struct iio_dev *indio_dev, + const void *data, + size_t data_sz, + int64_t timestamp) +{ + struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev); + + /* + * Conservative estimate - we can always safely copy the minimum + * of either the data provided or the length of the destination buffer. + * This relaxed limit allows the calling drivers to be lax about + * tracking the size of the data they are pushing, at the cost of + * unnecessary copying of padding. + */ + data_sz = min_t(size_t, indio_dev->scan_bytes, data_sz); + if (iio_dev_opaque->bounce_buffer_size != indio_dev->scan_bytes) { + void *bb; + + bb = devm_krealloc(&indio_dev->dev, + iio_dev_opaque->bounce_buffer, + indio_dev->scan_bytes, GFP_KERNEL); + if (!bb) + return -ENOMEM; + iio_dev_opaque->bounce_buffer = bb; + iio_dev_opaque->bounce_buffer_size = indio_dev->scan_bytes; + } + memcpy(iio_dev_opaque->bounce_buffer, data, data_sz); + return iio_push_to_buffers_with_timestamp(indio_dev, + iio_dev_opaque->bounce_buffer, + timestamp); +} +EXPORT_SYMBOL_GPL(iio_push_to_buffers_with_ts_unaligned); + /** * iio_buffer_release() - Free a buffer's resources * @ref: Pointer to the kref embedded in the iio_buffer struct diff --git a/include/linux/iio/buffer.h b/include/linux/iio/buffer.h index b6928ac5c63d..451379a3984a 100644 --- a/include/linux/iio/buffer.h +++ b/include/linux/iio/buffer.h @@ -38,6 +38,10 @@ static inline int iio_push_to_buffers_with_timestamp(struct iio_dev *indio_dev, return iio_push_to_buffers(indio_dev, data); } +int iio_push_to_buffers_with_ts_unaligned(struct iio_dev *indio_dev, + const void *data, size_t data_sz, + int64_t timestamp); + bool iio_validate_scan_mask_onehot(struct iio_dev *indio_dev, const unsigned long *mask); diff --git a/include/linux/iio/iio-opaque.h b/include/linux/iio/iio-opaque.h index c9504e9da571..2be12b7b5dc5 100644 --- a/include/linux/iio/iio-opaque.h +++ b/include/linux/iio/iio-opaque.h @@ -23,6 +23,8 @@ * @groupcounter: index of next attribute group * @legacy_scan_el_group: attribute group for legacy scan elements attribute group * @legacy_buffer_group: attribute group for legacy buffer attributes group + * @bounce_buffer: for devices that call iio_push_to_buffers_with_timestamp_unaligned() + * @bounce_buffer_size: size of currently allocate bounce buffer * @scan_index_timestamp: cache of the index to the timestamp * @clock_id: timestamping clock posix identifier * @chrdev: associated character device @@ -50,6 +52,8 @@ struct iio_dev_opaque { int groupcounter; struct attribute_group legacy_scan_el_group; struct attribute_group legacy_buffer_group; + void *bounce_buffer; + size_t bounce_buffer_size; unsigned int scan_index_timestamp; clockid_t clock_id; From patchwork Sun Jun 13 15:10:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Cameron X-Patchwork-Id: 12317741 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E940DC49360 for ; Sun, 13 Jun 2021 15:08:52 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C08F061285 for ; Sun, 13 Jun 2021 15:08:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231852AbhFMPKw (ORCPT ); Sun, 13 Jun 2021 11:10:52 -0400 Received: from mail.kernel.org ([198.145.29.99]:59646 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231841AbhFMPKw (ORCPT ); Sun, 13 Jun 2021 11:10:52 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7904361354; Sun, 13 Jun 2021 15:08:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1623596931; bh=evQspTvNNzcemrt8Qfvezi2VvurRJB1NS3HL5Yx6//0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UHzUsIa9V64wW8yIFuoW2lgcUZysLRmXV+6KS03mK6dPCNtIM5Q4SFYWQduFZ1nqb ZS7zdWGiaEdr0de2D6ZQVYvPH70OfkioNhOaHpGci7lR97j/o7chYV2hzFRd41my0+ q/irdFSZLvHw76Z0t+9vqPIcCN5PnOz7BxtD0mMn/AGXRwAoqIiJ4L+loaW+0PKWQk SM0d1a/SH8LsWKPk0rKKe6CO/FR86RB0GaEWZVEVeN/67tjrqd/NilBUGD7bkeMx/A 3y3XfOeidOxFPFCADIjwKPSg94npl0p7V0DDEXGx2eQjrJzXJC64RR3StYetTf6lP/ DxYPlQF2Te7sw== From: Jonathan Cameron To: linux-iio@vger.kernel.org, Andy Shevchenko , Nuno Sa Cc: Linus Walleij , Jan Kiszka , Jonathan Cameron Subject: [PATCH v2 2/4] iio: adc: ti-adc108s102: Fix alignment of buffer pushed to iio buffers. Date: Sun, 13 Jun 2021 16:10:37 +0100 Message-Id: <20210613151039.569883-3-jic23@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210613151039.569883-1-jic23@kernel.org> References: <20210613151039.569883-1-jic23@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Jonathan Cameron Use the newly introduce iio_push_to_buffers_with_ts_unaligned() function to ensure a bounce buffer is used to provide the required alignment and space padding needed by the IIO core which requires the timestamp is naturally aligned. There will be a performance cost to this change but it will ensure the driver works on platforms that do not support unaligned 8 byte assignments, and with consumer drivers that may assume natural alignment of the timestamp. Issue found as part of an audit of all calls to iio_push_to_buffers_with_timestamp() Fixes: 7e87d11c9bda ("iio: adc: Add support for TI ADC108S102 and ADC128S102") Signed-off-by: Jonathan Cameron Cc: Jan Kiszka --- drivers/iio/adc/ti-adc108s102.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/drivers/iio/adc/ti-adc108s102.c b/drivers/iio/adc/ti-adc108s102.c index db902aef2abe..c8e48881c37f 100644 --- a/drivers/iio/adc/ti-adc108s102.c +++ b/drivers/iio/adc/ti-adc108s102.c @@ -75,9 +75,9 @@ struct adc108s102_state { * rx_buf: |XX|R0|R1|R2|R3|R4|R5|R6|R7|tt|tt|tt|tt| * * tx_buf: 8 channel read commands, plus 1 dummy command - * rx_buf: 1 dummy response, 8 channel responses, plus 64-bit timestamp + * rx_buf: 1 dummy response, 8 channel responses */ - __be16 rx_buf[13] ____cacheline_aligned; + __be16 rx_buf[9] ____cacheline_aligned; __be16 tx_buf[9] ____cacheline_aligned; }; @@ -149,9 +149,10 @@ static irqreturn_t adc108s102_trigger_handler(int irq, void *p) goto out_notify; /* Skip the dummy response in the first slot */ - iio_push_to_buffers_with_timestamp(indio_dev, - (u8 *)&st->rx_buf[1], - iio_get_time_ns(indio_dev)); + iio_push_to_buffers_with_ts_unaligned(indio_dev, + &st->rx_buf[1], + st->ring_xfer.len - sizeof(st->rx_buf[1]), + iio_get_time_ns(indio_dev)); out_notify: iio_trigger_notify_done(indio_dev->trig); From patchwork Sun Jun 13 15:10:38 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Cameron X-Patchwork-Id: 12317743 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5FC24C48BCF for ; Sun, 13 Jun 2021 15:08:56 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 452636128A for ; Sun, 13 Jun 2021 15:08:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231858AbhFMPK4 (ORCPT ); Sun, 13 Jun 2021 11:10:56 -0400 Received: from mail.kernel.org ([198.145.29.99]:59696 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231841AbhFMPKz (ORCPT ); Sun, 13 Jun 2021 11:10:55 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 0DBAF611C0; Sun, 13 Jun 2021 15:08:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1623596934; bh=NuLD9nXi+L3dTsAfhpzZdAOEKP0d7QlNkroYcf+j7lQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WCgLBfjWd9syT9jMuHK64PNqfW9hOBPEvdhCtx0AT0YQ170p2eR1FGlQQVaDXQf/4 EdWl4XxUrNYI19uqzKF0DeGzAD0/+kTUOHMJWL0RoqudFNTYSEsulrarHyI7VSqEmP sXmBc2yS8JUvsui5nFhXHNdbIEE9dzmUbFMYfpXlUn9zzezqgmw+/Q9oR1eAn79AJV WS9b1CTpgBeJyME9mKDu2T/swjxhFkAQaRnh5JogQL3tLyk26lXhUs9RxBgNY5P7cL 4SoGqBNX79zXoM+K2mceBOhxAJ16m87/PEkc1N/08HnXOXt2ZOpCwqJwF/5PxhS7hr Z37zD/wC/i68g== From: Jonathan Cameron To: linux-iio@vger.kernel.org, Andy Shevchenko , Nuno Sa Cc: Linus Walleij , Jan Kiszka , Jonathan Cameron Subject: [PATCH v2 3/4] iio: gyro: mpu3050: Fix alignment and size issues with buffers. Date: Sun, 13 Jun 2021 16:10:38 +0100 Message-Id: <20210613151039.569883-4-jic23@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210613151039.569883-1-jic23@kernel.org> References: <20210613151039.569883-1-jic23@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Jonathan Cameron Fix a set of closely related issues. 1. When using fifo_values() there was not enough space for the timestamp to be inserted by iio_push_to_buffers_with_timestamp() 2. fifo_values() did not meet the alignment requirement of iio_push_to_buffers_with_timestamp() 3. hw_values did not meet the alignment requirement either. 1 and 2 fixed by using new iio_push_to_buffers_with_ts_unaligned() which has no alignment or space padding requirements. 3 fixed by introducing a structure that makes the space and alignment requirements explicit. Fixes: 3904b28efb2c ("iio: gyro: Add driver for the MPU-3050 gyroscope") Signed-off-by: Jonathan Cameron Reviewed-by: Linus Walleij --- drivers/iio/gyro/mpu3050-core.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/drivers/iio/gyro/mpu3050-core.c b/drivers/iio/gyro/mpu3050-core.c index 3225de1f023b..ea387efab62d 100644 --- a/drivers/iio/gyro/mpu3050-core.c +++ b/drivers/iio/gyro/mpu3050-core.c @@ -471,13 +471,10 @@ static irqreturn_t mpu3050_trigger_handler(int irq, void *p) struct iio_dev *indio_dev = pf->indio_dev; struct mpu3050 *mpu3050 = iio_priv(indio_dev); int ret; - /* - * Temperature 1*16 bits - * Three axes 3*16 bits - * Timestamp 64 bits (4*16 bits) - * Sum total 8*16 bits - */ - __be16 hw_values[8]; + struct { + __be16 chans[4]; + s64 timestamp __aligned(8); + } scan; s64 timestamp; unsigned int datums_from_fifo = 0; @@ -572,9 +569,10 @@ static irqreturn_t mpu3050_trigger_handler(int irq, void *p) fifo_values[4]); /* Index past the footer (fifo_values[0]) and push */ - iio_push_to_buffers_with_timestamp(indio_dev, - &fifo_values[1], - timestamp); + iio_push_to_buffers_with_ts_unaligned(indio_dev, + &fifo_values[1], + sizeof(__be16) * 4, + timestamp); fifocnt -= toread; datums_from_fifo++; @@ -632,15 +630,15 @@ static irqreturn_t mpu3050_trigger_handler(int irq, void *p) goto out_trigger_unlock; } - ret = regmap_bulk_read(mpu3050->map, MPU3050_TEMP_H, &hw_values, - sizeof(hw_values)); + ret = regmap_bulk_read(mpu3050->map, MPU3050_TEMP_H, scan.chans, + sizeof(scan.chans)); if (ret) { dev_err(mpu3050->dev, "error reading axis data\n"); goto out_trigger_unlock; } - iio_push_to_buffers_with_timestamp(indio_dev, hw_values, timestamp); + iio_push_to_buffers_with_timestamp(indio_dev, &scan, timestamp); out_trigger_unlock: mutex_unlock(&mpu3050->lock); From patchwork Sun Jun 13 15:10:39 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jonathan Cameron X-Patchwork-Id: 12317745 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.2 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id AEB59C48BDF for ; Sun, 13 Jun 2021 15:08:57 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 8D68861354 for ; Sun, 13 Jun 2021 15:08:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231879AbhFMPK6 (ORCPT ); Sun, 13 Jun 2021 11:10:58 -0400 Received: from mail.kernel.org ([198.145.29.99]:59746 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231841AbhFMPK5 (ORCPT ); Sun, 13 Jun 2021 11:10:57 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 7D50361285; Sun, 13 Jun 2021 15:08:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1623596936; bh=GftFmaYqSVIZg3oByRPY+E4bX0nmqwmIDpCm3TeFH6s=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=lk9KY1cHW6XrfQ0X+1SLt5oqBg0WCfkHuXVwJTa0ArMbeE6cXYkh1ld948I4BrHjz f2AGoqKM+hrEpjrYYefQlXsFwHQZg3xcMBBwiQebq+zepDoh1aNOqr7nnGX3ZutSeL nZ/iDXHDoCT2TMakaXqToxhI7CudhX9gRwZp0hOa+VeyvUcxRLJ2Vjg0oYmLTy7CP0 803UhjjZu8yr6jDAw8sYxPpxxCXpN2VsSt+WJSy1fgiY3uj9ICV8l52esVIA/vRdqX uiW86C+19Zz3lVUjtl/PKT48Bc+1CzezcCrInOTYTdIyT8Z2FDn2M4zSX5workeR/B iRWbcsK3zUvKw== From: Jonathan Cameron To: linux-iio@vger.kernel.org, Andy Shevchenko , Nuno Sa Cc: Linus Walleij , Jan Kiszka , Jonathan Cameron , =?utf-8?q?Nuno_S=C3=A1?= Subject: [PATCH v2 4/4] iio: imu: adis16400: Fix buffer alignment requirements. Date: Sun, 13 Jun 2021 16:10:39 +0100 Message-Id: <20210613151039.569883-5-jic23@kernel.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210613151039.569883-1-jic23@kernel.org> References: <20210613151039.569883-1-jic23@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org From: Jonathan Cameron iio_push_to_buffers_with_timestamp() requires that the buffer is 8 byte alignment to ensure an inserted timestamp is naturally aligned. This requirement was not met here when burst mode is in use beause of a leading u16. Use the new iio_push_to_buffers_with_ts_unaligned() function that has more relaxed requirements. It is somewhat complex to access that actual data length, but a safe bound can be found by using scan_bytes - sizeof(timestamp) so that is used in this path. More efficient approaches exist, but this ensure correctness at the cost of using a bounce buffer. Fixes: 5075e0720d93 ("iio: imu: adis: generalize burst mode support") Signed-off-by: Jonathan Cameron Reviewed-by: Nuno Sá Reviewed-by: Nuno Sá --- drivers/iio/imu/adis16400.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/drivers/iio/imu/adis16400.c b/drivers/iio/imu/adis16400.c index cb8d3ffab6fc..66a83ebd3109 100644 --- a/drivers/iio/imu/adis16400.c +++ b/drivers/iio/imu/adis16400.c @@ -648,13 +648,23 @@ static irqreturn_t adis16400_trigger_handler(int irq, void *p) if (ret) dev_err(&adis->spi->dev, "Failed to read data: %d\n", ret); - if (st->variant->flags & ADIS16400_BURST_DIAG_STAT) + if (st->variant->flags & ADIS16400_BURST_DIAG_STAT) { buffer = adis->buffer + sizeof(u16); - else - buffer = adis->buffer; + /* + * The size here is always larger than, or equal to the true + * size of the channel data. This may result in a larger copy + * than necessary, but as the target buffer will be + * buffer->scan_bytes this will be safe. + */ + iio_push_to_buffers_with_ts_unaligned(indio_dev, buffer, + indio_dev->scan_bytes - sizeof(pf->timestamp), + pf->timestamp); + } else { + iio_push_to_buffers_with_timestamp(indio_dev, + adis->buffer, + pf->timestamp); + } - iio_push_to_buffers_with_timestamp(indio_dev, buffer, - pf->timestamp); iio_trigger_notify_done(indio_dev->trig);