From nobody Fri Sep 25 14:31:21 2026 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (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 51DD4443C0B for ; Fri, 11 Sep 2026 11:10:37 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789125044; cv=none; b=CXLSEVLjvoSrY6z1a2MCeNF1WizQJpihtQYfOkg0Uu+Kn0CqjgafPg9LCyzX93VfiznAfOaeWkIPcr4Cv/0NDfWuO9jPzdDpB6rckqW51QpOKQtYLOeHQE4XaEL/XiedTOgokijM705DBwY75YAWA65PKqxqM1MpZqYfYpPWucY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789125044; c=relaxed/simple; bh=nkt9eX0UhBpFnXXxyb40A2jA3fGy9p1WHRIW/5hqH4U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mKZLRqkX2D4rJXc5+NbS0UXAmOHLuCnk4HxQPuGjLgvffGyjaeBEJY7PnLfCcOvPYMxQ2BXUyIpMIPoD+Y1WfXQARZ0plzlLckPYAr55N2dZFoagLVuI23U9x7nG/7v82xSgdA8X9eWtcP4IStluwG20hqvT7FHZA3vDv8xq11g= 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=dm6M1h0Z; arc=none smtp.client-ip=209.85.128.50 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="dm6M1h0Z" Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-49e651e83dfso3653975e9.0 for ; Fri, 11 Sep 2026 04:10:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1789125034; x=1789729834; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=buEE5TDzC5FsioP8qx8e+5Y4xF8w0Ds3BqUa5qHn1HE=; b=dm6M1h0ZIXunFxt01MMVbCItktIlWZ1Djun8hj2gMyjEFq9C0gI1e8bZoeo6p34DGe XS1tqguS5wLIpOrl+vgmJ+UCboR6oIRzQq+re4k+tGsSonvh/A+C36ZZozV3wPlHfAVb 6UdhJ8/vopy7Dd+f/su51rCnGnDRMhKxtu/Ec1Vs3EiZSGFp3kJWS8Uy/lYJ8iRKG23e 3feHMZg2atTDsrNaUFmVDySEGXItfKbClY0A+vxFPLh5nDS5VaZQp72cea3D27KpoQne 18Y5NQhUdRpHiZ+7wZHcmJffv1PTxewkJ4M0Lf4Zt8iy+/bw9uOW+UjDlhvkmkorVZmt OE0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789125034; x=1789729834; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=buEE5TDzC5FsioP8qx8e+5Y4xF8w0Ds3BqUa5qHn1HE=; b=nFP0QICm9zuDuZvo/HHv0jnBZMFUfJyAKkNOHtCdYSszeGD5l5g/XHRC7dlzF0sYST 4qRZe1CpoxyjChyS3uZkEx7HVj2lYTovViLZZpxl7GnVVFoYSOD34t7OY1JxYgLZIV6M tKqv5Zv+50wuIa2CDOgHUx0FHUypVFjS8ClNoaT9jN5AqER8eWIvHVcHiMIWIDNqfxHN UjZL8CPo3hRNRd+S9GxS036F+1DG5gcKvSpRKIoU7EI7VXO+Zo2IathoSx3bJjEH63Jp XDVkFs4YKbKcRWZ4ocfRucuOKCBy3aJV1JT/VhuCpDifhsGjfULV06Av11iJ9qQcw/5X fGYg== X-Forwarded-Encrypted: i=1; AKwUvBzXtMEKSsboQxu82I+eVIbdMLWQc3iHNLPe94DB0CB52gpAzeJPgXEouP+037v+FQ3jDBIKGvETfSbafmE=@vger.kernel.org X-Gm-Message-State: AFuF++k0/2EPp6HTNp9EMbq86i/i/C5qijvdehQrtWXvC8cWZde1e/Uj GXL0uZTLok3YeHhELO+LV49Bb2fewAZha2ftOD6/WB1X46myt5AHROC4 X-Gm-Gg: AYBFou3YkwGqIc7F2pDzbRKikc3e9wR9IjkV3wb1basCaQm19GmZFyKSXRg+X1bRpcF UWtA8iyWRTHDiLCVU0Vbd2zONJXwIYA+C0kBLsN8DOx3cNqYVBIHz+6vpbXHLMgsfGQwG27d7NN 5jsHpz4k45PykNmYiw03/JwA4aiPItBixL1xx9xwIcpS6WW0gU3e6aBf45tlX73DPhAVX16OnB+ sMMMfINZIZ9qldWPg92y6Dm/ALtZ6QIOY78RRD31nr2NkZVHcpYm0nHRiZpTHDlymChH6jIu4tv i8/Qv3UZDl3F42OwVjAGAOJVErL7PyJy9BRF7iGv2fkxe3TdatVxBp/En6H6mLf5Bzt54cVVIa8 4JuLXe5HXRwMaEdxb8tJYg+3MgDE1Y/YuhRxTkjShaJjclsT2MARPEovnocjjoRK35C94Syg179 IJJBkcLLyNBk6EMfeU32tz2wdMS81EuaKk4mh6FbfumsJqE1V0cUTRou6s7/3kfoYgPtveHvBnn Z/UGvXxf3991lV7G1U= X-Received: by 2002:a05:600c:354a:b0:49d:2536:402e with SMTP id 5b1f17b1804b1-49e61a005b6mr41951185e9.30.1789125034386; Fri, 11 Sep 2026 04:10:34 -0700 (PDT) Received: from fedora ([154.182.82.204]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49e60ac411asm66109065e9.8.2026.09.11.04.10.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Sep 2026 04:10:33 -0700 (PDT) From: Abdelnasser Hussein To: jic23@kernel.org, gregkh@linuxfoundation.org, nuno.sa@analog.com, Michael.Hennerich@analog.com Cc: dlechner@baylibre.com, andy@kernel.org, linux@analog.com, linux-iio@vger.kernel.org, linux-staging@lists.linux.dev, linux-kernel@vger.kernel.org, joshua.crofts1@gmail.com, Abdelnasser Hussein Subject: [PATCH v5 1/2] staging: iio: adc: ad7816: Serialize SPI read operations Date: Fri, 11 Sep 2026 14:04:55 +0300 Message-ID: <20260911110456.24873-2-abdelnasserhussein11@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260911110456.24873-1-abdelnasserhussein11@gmail.com> References: <20260911110456.24873-1-abdelnasserhussein11@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The ad7816_spi_read() function performs a sequence of GPIO state changes followed by an SPI transfer. If multiple read operations occur simultaneously, the GPIO state could be changed by one thread while another is in the middle of a read. This leads to a race condition where the sensor state is disrupted, resulting in corrupted data being read. Introduce a mutex to serialize the read sequence, ensuring that the GPIO toggling and the SPI transfer are treated as a single atomic operation. Signed-off-by: Abdelnasser Hussein --- drivers/staging/iio/adc/ad7816.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/drivers/staging/iio/adc/ad7816.c b/drivers/staging/iio/adc/ad7= 816.c index 0e32a2295990..d0355763aa8f 100644 --- a/drivers/staging/iio/adc/ad7816.c +++ b/drivers/staging/iio/adc/ad7816.c @@ -5,15 +5,17 @@ * Copyright 2010 Analog Devices Inc. */ =20 -#include -#include +#include #include +#include +#include #include -#include -#include #include -#include #include +#include +#include +#include +#include =20 #include #include @@ -50,6 +52,7 @@ struct ad7816_chip_info { u8 oti_data[AD7816_CS_MAX + 1]; u8 channel_id; /* 0 always be temperature */ u8 mode; + struct mutex lock; /* protect device state during SPI transfers */ }; =20 enum ad7816_type { @@ -67,6 +70,8 @@ static int ad7816_spi_read(struct ad7816_chip_info *chip,= u16 *data) int ret; __be16 buf; =20 + guard(mutex)(&chip->lock); + gpiod_set_value(chip->rdwr_pin, 1); gpiod_set_value(chip->rdwr_pin, 0); ret =3D spi_write(spi_dev, &chip->channel_id, sizeof(chip->channel_id)); @@ -91,7 +96,7 @@ static int ad7816_spi_read(struct ad7816_chip_info *chip,= u16 *data) =20 gpiod_set_value(chip->rdwr_pin, 0); gpiod_set_value(chip->rdwr_pin, 1); - ret =3D spi_read(spi_dev, &buf, sizeof(*data)); + ret =3D spi_read(spi_dev, &buf, sizeof(buf)); if (ret < 0) { dev_err(&spi_dev->dev, "SPI data read error\n"); return ret; @@ -360,6 +365,10 @@ static int ad7816_probe(struct spi_device *spi_dev) return -ENOMEM; chip =3D iio_priv(indio_dev); =20 + ret =3D devm_mutex_init(&spi_dev->dev, &chip->lock); + if (ret) + return ret; + chip->spi_dev =3D spi_dev; for (i =3D 0; i <=3D AD7816_CS_MAX; i++) chip->oti_data[i] =3D 203; --=20 2.54.0 From nobody Fri Sep 25 14:31:21 2026 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (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 64E6E427A03 for ; Fri, 11 Sep 2026 11:10:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.45 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789125062; cv=none; b=dory/1p0IeYUvBj8QQvwf/d/42Zg7GYNn5KXhuri/Jg3HtXeJVuxDHZUhldSNFS1dhXfr8g3IWZBctkf/CRHWLzRrrZSa3FwKnUaUICzUjvreufRjaCtFpFabR1zEhlw4UfHkBVbbuBE6Pg1y+FyN/9ThnrmJ4nZtojjGl2PtkI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789125062; c=relaxed/simple; bh=pJ4vj8BCmfF1V31tX/wZS7//jdxk6ExCWx5LYoQItFw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=QkY1q7L7xAe5Rjnrxy9tBFhOsNZD0rzGEQ31MvYIVPyAfhZ7+IAn+c8e3dvwOqtSQsl5MTe7ZKNE7gS3RSBLhKNKSDfDcxJUfe9NPp9GUyg6gbtlpl5nzT0I9L1TEioiF8kkYZIYYViY5zch89HzBrssEYMDIzrDaE7KBAa+XQE= 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=caRQwJYU; arc=none smtp.client-ip=209.85.128.45 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="caRQwJYU" Received: by mail-wm1-f45.google.com with SMTP id 5b1f17b1804b1-49a97714f5dso7192815e9.0 for ; Fri, 11 Sep 2026 04:10:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1789125049; x=1789729849; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=xWttpW5LjqZG6rptcoUTqVDa/t+6fdXdX2MvAK+JxuA=; b=caRQwJYU8TP/h0sqp5TVcK8EWyqETVDBiR+DJYpnFf9pHNpFpg1V3c4TY8IbMfE8IM zeVKPCHP3/jZEKnH2icgkHNqTmeKFv7UWMbrlpHlFaa/iQ28XM9lJZTEYjjw0bjQ7jXS hS9F1LQW1kV/ocjhUdwMbHfkTYyY/fyunl3GvmxIdjcYoi6JPoH/FwaW5Ddp3/iqmIqs dGG6qvHrEaQGOrrlffTE1Te41+9oDY/ePcGnAYuPt/3ikBiFzXE3JP2qaWNcy3vvrM05 32hHOV9RJUqvozSnwxIGsIQgzFYq9gBzrpl7u7U16RUj7/i2T3yTWUevkf/zZoZSnnUa dyPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1789125049; x=1789729849; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=xWttpW5LjqZG6rptcoUTqVDa/t+6fdXdX2MvAK+JxuA=; b=UDi3tIF8AlhJlN5IZoS/wVDC05n3zCHiKV3ZTRTebfFS/wWyeElHY567fEZDAljsOA wqMKCx+wiEsZwbOWlRg1HOXmnp2dpVxXrwhkTWXpfl65hecZ1Lz7IP4QaEjdC9XI6PtO Cj9KghbAy/yKbV+HvxgHVuApJxpNk0d5lmFhgUnfS5KLlyrWIf9HXo2VisCOUHqjydyl 976qKzRnHXLSVkdczAEKvPNamDF3ghNd25wppHgyByp29AibTqMo61bDvme1noei9GHF UAshRz7g5ZSnG802f8DulKFZH7Mti7RGANY6BJUTOJuieH98s/e8yeEboNeeCxY1fWv+ 55bg== X-Forwarded-Encrypted: i=1; AKwUvByPOVTQnn4A++bpy7guG0ImJGREMDeT3jLx6rIMat5qP0N2B+HjD+Lg2e0XiOSi+Il9uWxI4gV8Q2MIWQc=@vger.kernel.org X-Gm-Message-State: AFuF++mbDx/CHC5CbmX/iNmeXykN2iIFYarHUQeB53tKDF4diovRPiCN uq8zHCFwYu4XpJrm1XwISgrx38s9Cg0Nz2/qrjvXPjQ0MSd35OudFXWgoPGPeA== X-Gm-Gg: AYBFou39x3hca1nC91Y4EABdSgu/ehwnsWiEC2NVnJ9q3dY6QjcWO7FxAvO62DLiOEq UNebVyHtUNqp3FrJ4Jzad5twBo2Ojc9y0ZwHZfiMjyST82jCxzcDwUlm5l3EfFi+tLXSoXvEtu4 TuwAWwavi+fP210gSoshLqLvzvR3F3B2Z9Iqo2Jh8Ks1iD2Nm6xm+JgDZIVM/Oog/T7py0dFDlo hYH5t6iqOeyuv/d5RhU+DVVZHwBqReByCN4p31yJwnQUiFD0o/p2tGKku7e+KVg7VxmxzHIeNeK Em7nfr0FmdzkFleoxpfiGq+edt93/DpzU/Brir+Y6YdNYiuVo9yez82gNzf/RZteNuZMWoh+02T 0+EhCLMt2E+tfZ210T4quouZwVH2pBtFYwu+Ep1vQPXLqpAiK//xeQm6DujF2XzKFqHovKbQ+SO uymYvuxLufRtLIQ682dPzGGxDX/54AXUeUdfsvOh/wnTvk5cV/zZBGtTipOTf5drd72Ci/2OoGK 5zzG1lBxY2rAIbZtic= X-Received: by 2002:a05:600c:3b07:b0:49c:fa20:cbff with SMTP id 5b1f17b1804b1-49e619c18a3mr42351175e9.22.1789125048251; Fri, 11 Sep 2026 04:10:48 -0700 (PDT) Received: from fedora ([154.182.82.204]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-49e60ac411asm66109065e9.8.2026.09.11.04.10.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Sep 2026 04:10:47 -0700 (PDT) From: Abdelnasser Hussein To: jic23@kernel.org, gregkh@linuxfoundation.org, nuno.sa@analog.com, Michael.Hennerich@analog.com Cc: dlechner@baylibre.com, andy@kernel.org, linux@analog.com, linux-iio@vger.kernel.org, linux-staging@lists.linux.dev, linux-kernel@vger.kernel.org, joshua.crofts1@gmail.com, Abdelnasser Hussein Subject: [PATCH v5 2/2] staging: iio: adc: ad7816: Use DMA-safe buffer for SPI read Date: Fri, 11 Sep 2026 14:04:56 +0300 Message-ID: <20260911110456.24873-3-abdelnasserhussein11@gmail.com> X-Mailer: git-send-email 2.54.0 In-Reply-To: <20260911110456.24873-1-abdelnasserhussein11@gmail.com> References: <20260911110456.24873-1-abdelnasserhussein11@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Stack buffers are unsafe for DMA with VMAP_STACK enabled because they are not physically contiguous and can share cache lines. This causes data corruption. Fix this by using a dedicated rx_buf aligned with IIO_DMA_MINALIGN in the device state structure to ensure cache coherency. Fixes: 7024425db64a ("staging: iio: adc: new driver for AD7816 devices") Signed-off-by: Abdelnasser Hussein --- drivers/staging/iio/adc/ad7816.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/drivers/staging/iio/adc/ad7816.c b/drivers/staging/iio/adc/ad7= 816.c index d0355763aa8f..19da164945c7 100644 --- a/drivers/staging/iio/adc/ad7816.c +++ b/drivers/staging/iio/adc/ad7816.c @@ -53,6 +53,7 @@ struct ad7816_chip_info { u8 channel_id; /* 0 always be temperature */ u8 mode; struct mutex lock; /* protect device state during SPI transfers */ + __be16 rx_buf __aligned(IIO_DMA_MINALIGN); }; =20 enum ad7816_type { @@ -68,7 +69,6 @@ static int ad7816_spi_read(struct ad7816_chip_info *chip,= u16 *data) { struct spi_device *spi_dev =3D chip->spi_dev; int ret; - __be16 buf; =20 guard(mutex)(&chip->lock); =20 @@ -96,13 +96,13 @@ static int ad7816_spi_read(struct ad7816_chip_info *chi= p, u16 *data) =20 gpiod_set_value(chip->rdwr_pin, 0); gpiod_set_value(chip->rdwr_pin, 1); - ret =3D spi_read(spi_dev, &buf, sizeof(buf)); + ret =3D spi_read(spi_dev, &chip->rx_buf, sizeof(chip->rx_buf)); if (ret < 0) { dev_err(&spi_dev->dev, "SPI data read error\n"); return ret; } =20 - *data =3D be16_to_cpu(buf); + *data =3D be16_to_cpu(chip->rx_buf); =20 return ret; } --=20 2.54.0