Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp9102868pxu; Mon, 28 Dec 2020 06:42:29 -0800 (PST) X-Google-Smtp-Source: ABdhPJzqgKNVd/xxSKsPod1R3xL8+6XAkpsoSzGpOhbgBQG+4aPSFeZ3DVG5X7049zTSoM0jDAHL X-Received: by 2002:a17:907:961b:: with SMTP id gb27mr24042282ejc.313.1609166549277; Mon, 28 Dec 2020 06:42:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1609166549; cv=none; d=google.com; s=arc-20160816; b=eLU079vPWdTlRmP+26i21zPXM6wCAsac9E37N2uaUV9h2Qi5QeKgtlLqESH/psCRfM OXyq2ok6ZxOjLGxytbT4LijqdM0GKcVpvPhqDKM7ylOnAZuUq9qEw0nY5jWHc5YJmvsH 0CXvEDqhiZzK1H+EQ6GLHpQ/H3ygiAft3YAEbUtKdIYzR2sxI7Ot3QQJ4lzp36rJxaAs FFBtZ0eyBIgzyiHicpVQtmyYKfYf8TwFRobAwMI7OmJRWVbFm8xzf+/+OzqhJTY0hwPB +sqSnnZ3JaURJ09Dwdwck/jZG3YmZp88k9qbOgLRE/nVfwNfMAyJpHxzRIMAuTixC9hi KtDw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=qnip/D4fc+n5McSmfiVibrcuSqwfCNbmok0Vxv4C2X4=; b=Yulcg+MwKcPVzl7+8kar5SOzS8VuvE9geC0GUMwwp+wb7DpyOCZe9DNfYoqQuuz8fd Ln4YCO7d3h9mH4Fgc+Z2LEtwhXoQezY+WdolvlUZqn3kOI3DMJ0rivUP468dEJ9AZ93y OP79tvE8MuRaEY3faNEE91TQ3guXmmk7iVevdDRURP+u1iFx/j8IqlOsvEiupvZxX+X8 4J9usiQHFM5migGVHoUcb5YZHdBzq2yIjU89uA1pJWHpv4htXNxMyJtoyZ2AQyQX4s3u j0qdSx4d90Yco6dHHcKQMZdGR71lXL6Yx8nVVepfdOwzF1w6F0QsFpwg6D28SbhMCbaT L0lw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=EeOubMit; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d10si20045775edk.240.2020.12.28.06.42.06; Mon, 28 Dec 2020 06:42:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=EeOubMit; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2505763AbgL1Ojx (ORCPT + 99 others); Mon, 28 Dec 2020 09:39:53 -0500 Received: from mail.kernel.org ([198.145.29.99]:36226 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2503587AbgL1O3Z (ORCPT ); Mon, 28 Dec 2020 09:29:25 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 3E9AB206D4; Mon, 28 Dec 2020 14:29:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1609165749; bh=Pm31Go5VQ99lxSIseW0XmyID5TuYxNjyEIb1Y1j1Bbc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EeOubMit7U4QMTPBRhTavGFS1XzatHjY6HWiGv8AXvoR3sw2oR8gSeF9ZpiswnwZs bCv8+XYhqQBvd27ID6ZpU1MYeQ9hhw5/DEiRoZheddbWwQwNj8KC2diwcBIo1BPynM 5itK5M53sH9wkMYp5iM94ZnOvvO6tw3WFQO/sccQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lars-Peter Clausen , Alexandru Ardelean , Stable@vger.kernel.org, Jonathan Cameron Subject: [PATCH 5.10 640/717] iio: ad_sigma_delta: Dont put SPI transfer buffer on the stack Date: Mon, 28 Dec 2020 13:50:38 +0100 Message-Id: <20201228125051.594041708@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201228125020.963311703@linuxfoundation.org> References: <20201228125020.963311703@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Lars-Peter Clausen commit 0fb6ee8d0b5e90b72f870f76debc8bd31a742014 upstream. Use a heap allocated memory for the SPI transfer buffer. Using stack memory can corrupt stack memory when using DMA on some systems. This change moves the buffer from the stack of the trigger handler call to the heap of the buffer of the state struct. The size increases takes into account the alignment for the timestamp, which is 8 bytes. The 'data' buffer is split into 'tx_buf' and 'rx_buf', to make a clearer separation of which part of the buffer should be used for TX & RX. Fixes: af3008485ea03 ("iio:adc: Add common code for ADI Sigma Delta devices") Signed-off-by: Lars-Peter Clausen Signed-off-by: Alexandru Ardelean Link: https://lore.kernel.org/r/20201124123807.19717-1-alexandru.ardelean@analog.com Cc: Signed-off-by: Jonathan Cameron Signed-off-by: Greg Kroah-Hartman --- drivers/iio/adc/ad_sigma_delta.c | 18 ++++++++---------- include/linux/iio/adc/ad_sigma_delta.h | 6 +++++- 2 files changed, 13 insertions(+), 11 deletions(-) --- a/drivers/iio/adc/ad_sigma_delta.c +++ b/drivers/iio/adc/ad_sigma_delta.c @@ -57,7 +57,7 @@ EXPORT_SYMBOL_GPL(ad_sd_set_comm); int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg, unsigned int size, unsigned int val) { - uint8_t *data = sigma_delta->data; + uint8_t *data = sigma_delta->tx_buf; struct spi_transfer t = { .tx_buf = data, .len = size + 1, @@ -99,7 +99,7 @@ EXPORT_SYMBOL_GPL(ad_sd_write_reg); static int ad_sd_read_reg_raw(struct ad_sigma_delta *sigma_delta, unsigned int reg, unsigned int size, uint8_t *val) { - uint8_t *data = sigma_delta->data; + uint8_t *data = sigma_delta->tx_buf; int ret; struct spi_transfer t[] = { { @@ -146,22 +146,22 @@ int ad_sd_read_reg(struct ad_sigma_delta { int ret; - ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->data); + ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->rx_buf); if (ret < 0) goto out; switch (size) { case 4: - *val = get_unaligned_be32(sigma_delta->data); + *val = get_unaligned_be32(sigma_delta->rx_buf); break; case 3: - *val = get_unaligned_be24(&sigma_delta->data[0]); + *val = get_unaligned_be24(sigma_delta->rx_buf); break; case 2: - *val = get_unaligned_be16(sigma_delta->data); + *val = get_unaligned_be16(sigma_delta->rx_buf); break; case 1: - *val = sigma_delta->data[0]; + *val = sigma_delta->rx_buf[0]; break; default: ret = -EINVAL; @@ -395,11 +395,9 @@ static irqreturn_t ad_sd_trigger_handler struct iio_poll_func *pf = p; struct iio_dev *indio_dev = pf->indio_dev; struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev); + uint8_t *data = sigma_delta->rx_buf; unsigned int reg_size; unsigned int data_reg; - uint8_t data[16]; - - memset(data, 0x00, 16); reg_size = indio_dev->channels[0].scan_type.realbits + indio_dev->channels[0].scan_type.shift; --- a/include/linux/iio/adc/ad_sigma_delta.h +++ b/include/linux/iio/adc/ad_sigma_delta.h @@ -79,8 +79,12 @@ struct ad_sigma_delta { /* * DMA (thus cache coherency maintenance) requires the * transfer buffers to live in their own cache lines. + * 'tx_buf' is up to 32 bits. + * 'rx_buf' is up to 32 bits per sample + 64 bit timestamp, + * rounded to 16 bytes to take into account padding. */ - uint8_t data[4] ____cacheline_aligned; + uint8_t tx_buf[4] ____cacheline_aligned; + uint8_t rx_buf[16] __aligned(8); }; static inline int ad_sigma_delta_set_channel(struct ad_sigma_delta *sd,