From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) (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 17DD01F37C5 for ; Mon, 1 Sep 2025 10:23:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.51 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756722227; cv=none; b=mlc5w6lMGCImbvivza//8PweXfvVpJhwA3X4x/NgmvzR4UIGY4G//hM20y4mVINlzcMqoGP83KRrFrMP7cC6L3aTryca9YF1NIDXOYat4YEujjw05SrjexneYwyYqX5QgIVsbQtePCxF93Bp6rP/nogcz50/Bd24Ibyf0k0/uT4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756722227; c=relaxed/simple; bh=iN7KWIHWLryomzRy86DkhEHpxBZtcIx3XkEBd/1FjSw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=uN7ilEPgvnou6alHRxxuGL32VgwopqPECAVtbHB/4eJogDSqhWgmtdLWVLfVp1A25lwizVtl2nAkASROY8b1UykBkYuaT9cm28OD2PQw9SXh0Y6NQDjNqw6p1/umVCrUdexi0l08x9tJ+iM3lz+gWmoHPNd3GcZ4jGpyUQzbpGg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=a5gUYk4F; arc=none smtp.client-ip=209.85.221.51 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="a5gUYk4F" Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-3cbb3ff70a0so2560652f8f.2 for ; Mon, 01 Sep 2025 03:23:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1756722224; x=1757327024; 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=FupNSd4N71G5RaqRUXbWr5mmoLboe7U184QjCzCsDcw=; b=a5gUYk4FN/Cc6f6d5FGj5fp+8JLAKSprkaYVD+MA4lTXnQeusO2dSl8Qse6CivjXvP MxnAkBgwlDwpS2LOUSNrV8nMLOxa9/B5QMR6Rv0dnmr2emX+dytnZSaywWiIzs74/scl abXnmJfwMlxxP9Y1BJaxh4rDhSGv5FVaUUCpZlKEieIchf269kzI7njHxumWrS2V1JA/ q2djFxpuw1+G0fwIcisBl0bFtlx1YfcPzSetDyAGNEM8+2zGxDz7wm3G6F3QaaA4ja6w 7cH3rt/BezXGtgxC3EW+pXi/rnHnW8lsndGW5pLThBu1pOdpPIpKAOwA5lRaV/jQgPUr Ojhg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756722224; x=1757327024; 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=FupNSd4N71G5RaqRUXbWr5mmoLboe7U184QjCzCsDcw=; b=dHgiqzy072cbu3hqxVb4iV3xi5GpZAWkU4NwuzjNa9THWIgKB8CxEZHbehMrI515lc mtihbGALmNPs7LnxLDle9NQBSRuPLw2OQ/2YiViyBYI387K8BM1hj76sIJsp5t8ljPOs zSbuaIDtdj5LJAXWeph5SB0UadveGJYPEUtlt/sIF402ipb1gIClMDLj8aUfQ9oX+v7D BfOpQzXPfzC1aPXi8QfrpLx4exvYrUUf68H7po4/qeblw+VAJHzwO9LD3c4GXnCeodu5 3m/MacW8m+z6cxMfSpkvRt2WUxCwKWmQezd11E4yNwh4gH6j90XjJy5dIUvCmQxvgBLb 9UZQ== X-Forwarded-Encrypted: i=1; AJvYcCUtrOhP8MMAWn8AksgINnTvkG9iD3HM8bZ3YvJD48/jPbTlJ+dbgyFfe1hQ8qy5kyQ7nHL1qz9EbniLfhI=@vger.kernel.org X-Gm-Message-State: AOJu0Yz7eRadRhT9oc7JcBFVo+QOBgyT+Tfx6ao7ptpvZAyqNFLRvBF4 t9m7VBbgjRV89rPWp5kTxkM22q9LCfTcE2bgcVvMoZwsLoUud8Cw4gzPEijvb0/BRFTA5WAMyyF wBRl/ X-Gm-Gg: ASbGncuNCmtH6FljC8qY7MGr2Iefl7F9oUa9fDus0wALGmkk3awoUM7M+gdwiVEGkG6 pZZ1FRcnTMEAEWsLCJHlq68GPEuPS5PJc92Luf+WZH/1XT1QWTeFvvSj85cIQfNlr3a1Bg2szz/ I18WRvAlOmaQrolxBZ+ThEcIlJ5ARdIKu1tLdUE4FVxZ/nlDzSFS6o/dmN/en4AAPv+nNxz5IFq i6txkdL3Tj1vt+2sKTpJ9VHY0vQvylishdl1n7vxmIDw7Hi7kKFUO2/mvje2P7pB7l+iyafhnMe mMUH3MrXEMuz8QDGrxC1AMPZKmHkh3qQT57RMh/vJI9V5l+9rk2VJZVDLPzW7K8iKh7iAtwbhcJ 2OTMCN5O1+vPdiQKn7pny8KkHJJM= X-Google-Smtp-Source: AGHT+IEv84aTwWbs3yO63r/JRI24Eo/yePwO1sRQK5IAZ7R3Bl3405JKkyTVYcyfa6amc+95TrXJqw== X-Received: by 2002:a05:6000:65b:b0:3d4:a64:6754 with SMTP id ffacd0b85a97d-3d40a646bc2mr3954157f8f.62.1756722224384; Mon, 01 Sep 2025 03:23:44 -0700 (PDT) Received: from localhost ([196.207.164.177]) by smtp.gmail.com with UTF8SMTPSA id 5b1f17b1804b1-45b7e7d141esm155678035e9.2.2025.09.01.03.23.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 01 Sep 2025 03:23:43 -0700 (PDT) Date: Mon, 1 Sep 2025 13:23:40 +0300 From: Dan Carpenter To: Mohammad Amin Hosseini Cc: linux-iio@vger.kernel.org, linux-staging@lists.linux.dev, linux-kernel@vger.kernel.org, gregkh@linuxfoundation.org, jic23@kernel.org, lars@metafoo.de, Michael.Hennerich@analog.com, dlechner@baylibre.com, nuno.sa@analog.com, andy@kernel.org Subject: Re: [PATCH v2] staging: iio: adc: ad7816: add mutex to serialize SPI/GPIO operations Message-ID: References: <20250901065445.8787-1-moahmmad.hosseinii@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20250901065445.8787-1-moahmmad.hosseinii@gmail.com> On Mon, Sep 01, 2025 at 10:24:45AM +0330, Mohammad Amin Hosseini wrote: > From: mohammad amin hosseini > > The ad7816 driver was accessing SPI and GPIO lines without > synchronization, which could lead to race conditions when accessed > concurrently from multiple contexts. This might result in corrupted > readings or inconsistent GPIO states. > > Introduce an io_lock mutex in the driver structure to serialize: > - SPI transactions in ad7816_spi_read() and ad7816_spi_write() > - GPIO pin toggling sequences > - Updates to device state via sysfs store functions (mode, channel, oti) > > The mutex ensures proper mutual exclusion and prevents race > conditions under concurrent access. > The commit message says "might result", which basically implies this is from reviewing the code and that we don't have a stack trace? Is that correct? Are you using some kind of static checker to find racy code? What kind of heuristic is it using to find buggy code? > Changes in v2: > - Fixed mismatch between From: and Signed-off-by lines > > Signed-off-by: mohammad amin hosseini Please add a Fixes tag. > @@ -200,7 +204,9 @@ static ssize_t ad7816_store_channel(struct device *dev, > return -EINVAL; > } > > + mutex_lock(&chip->io_lock); > chip->channel_id = data; > + mutex_unlock(&chip->io_lock); > > return len; > } > @@ -322,7 +328,9 @@ static inline ssize_t ad7816_set_oti(struct device *dev, > if (ret) > return -EIO; > > + mutex_lock(&chip->io_lock); > chip->oti_data[chip->channel_id] = data; > + mutex_unlock(&chip->io_lock); > > return len; > } I'm not really knowledgeable to review the others, if they are required or how the locking is supposed to work. But these aren't correct because we're only locking around the writers and not the readers so it could still race. regards, dan carpenter