From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f47.google.com (mail-wr1-f47.google.com [209.85.221.47]) (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 1F1D72FB985 for ; Mon, 1 Sep 2025 10:23:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756722227; cv=none; b=nNSqbAxgZKn+oib/TcrCDPONV9KYbnIY+gayT55Y79vM+ulCFzvXa2S8xGi83gb5kFZvH9YUINcjuuniy7VelKK0DWXzsJnTtCc26cD7ZNzmO2A+HL8c/9za24kl3OsO5sTBcs6zFTRkyDocnG9mnF4MMqRSFL1WF5ZhRNFheUs= 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=gGjFHVOS; arc=none smtp.client-ip=209.85.221.47 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="gGjFHVOS" Received: by mail-wr1-f47.google.com with SMTP id ffacd0b85a97d-3cdc54cabb1so1702760f8f.0 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=lists.linux.dev; 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=gGjFHVOS16aImnJ77ydVuUvSDjNIgH6gMr3Uf9UH6QvdZbwJVBa6NySgkkzMwmc8i2 cA60BpcMQBQMSxaKprw4EkmzsSM0B16ta928x3H+LsGV8ChdGOwcRLsou6Uots0o5FLj 86x6UMPJ15D5QDvwerYtxEuD5+gbw+/atgaNEtEhhyWa6v/I3eDUZ3cK4ndHxi53T7x0 2HNtJ7d+CFiv3MFrUMdjxY3JrUPT+z/xOik9ytFFdiuV8LvRhj7xylVLYjTzfDzMvUXi fIEpL0S4y+ykMcqzpyjMzbKyAvmY/bNSGa01vLq444ZGO/G7eU1ieplE4LI1egqrJ2O2 vtOA== 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=DYfzvqhY3h2vlNT70Cb/3Yl5p02hYKzYK5tCESrSuLc4nun4uol9YSHHMCrSzg4ES7 pIOGpgB06SC/TENeaZklzyLJCYswAvvjM8XjyQ7bBuFRiXB16doVlfNjoQi3ZFvWeayF /aVK2dy+iIxBYRrGWQPxlVPPfp/aIZk754VQXJ054y9qbJ46cwGoM1S3Lahn7gpBLvs7 KhL+xiJrNOvscBn00Ugiv3ETJh10ZSiFt2CZCXsyopTeCe73jz2P22uMyXr9d74lzDG2 gS3q84oiJPoS8O7WQ+4hj2lca4EzZXV1IIEcLxqruaxoONakYUiIFW5o3Hj8s+2h2DqL FcJw== X-Forwarded-Encrypted: i=1; AJvYcCW2f7/pArpQdCkFijHjJkl7+Ej/Tjgz9zMxHelZR26p3fACo3eFb+RZRPOYz3e7p4lxIvjPUjSSiAU619PA@lists.linux.dev X-Gm-Message-State: AOJu0YxwsMvTgJZN1KVG1h9HOiV196OGjkDXY5rprO0K2bR7WO0zL2lI dGW/rMra/yWt/qumJmiQ6HEFYh6Z1bmt/RU2ouI08ai+1RZQXzHfmrbNyFeuYl23WAQ= X-Gm-Gg: ASbGnctDliILmJFN5P/TQVmkvUUUCTyNHkyziogShUmdrZjL6XxwSbRRvYOedePNY8M 3EAe83gwNIdLBcF6kErh4FRzdKVlax6kjQNRXCBXqPEyaik+zn/1olLYtqmEI0X46ahmLxh1hxN kaMQ4H4wIayp1yWKzuUuBL8dAQs1AI2MFhSGsXTEZf8yv6w1uDLr2Xwvu/2Cs/l3uV2oA16wE+a s7ctXK6fLptPbru1cS+rQFwp0A96oArAhOSdN1kmJyJm3nbiXUx7AhR5k6ABdcvoKqMfI36qR+9 ieB+61A7/csVOXBQ7xH0CICcOCW8EyXLpaUsNO7Jl8FzNsPOJrpCtQTGOIW6M418RzAUwP3i0g+ jeEe0Lxg+GY+A1kbCKqNaTyDeSmI= 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-staging@lists.linux.dev 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