Received: by 10.223.176.5 with SMTP id f5csp1445382wra; Sun, 28 Jan 2018 01:10:45 -0800 (PST) X-Google-Smtp-Source: AH8x227heToMvLW3f/HLmGVvMQqz/NbRv6BhD7lzRwKyX34jCwavWO6iVNkny1Dx5oJ8wO3fjo5U X-Received: by 2002:a17:902:46a:: with SMTP id 97-v6mr18333924ple.96.1517130645121; Sun, 28 Jan 2018 01:10:45 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517130645; cv=none; d=google.com; s=arc-20160816; b=drd41KR7oi1q3mE+c/eFG6kXuk7ofksZ2WcXkkSvE5hpNyEVMngX1bHfXsxlUHK4xY RBtx67lTDdNwpWZJ/opxcLrxvnrXaYuD5HZ6swHlen8lWuDPK+iGqRkOA0aCiQHhKaUs 2nQH5q7Ri49lze6WJ274Py4ICMwIL2Ndr40S0nOR6pjUKkItPiq97Rs37i5FVBu7d1KP ACiL+KUfMy5WEN0gUFpRn/NXXD1EC/sbIU+DjCe6fO7ACV8SN1AUOoDB81v2+o8SBcCI g4eJPUPW97RbdhSG1Y7FfKFWqwssoLFvwVY7lTYdQWeDTtdD13sgpQtbnPiVhC/z+HDY 5IIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:date:cc:to:from:subject:message-id :dkim-signature:arc-authentication-results; bh=diQeDvuYUNJjAe/fWZxd2EpKPt4z81V46os1OSFpK4s=; b=yALbWhE0J6iB8vW0NkL5Vm+HFLTZGiRqV79buCzc74OwTMZv1ANuSh1nQwxtQDkJhP lfIeEvaBTQ8XWMggFLkK10nzvhoayQZ7uF+JkfvMfLtx/tT7yeBiFIBrHYfzXhXUJc73 a5iS3SZEjGFI5eojCQ8uPpRUt91cZWNQQLucBYdknTjMRFDkfnzzFDR3yINhMtkcMgLO NJx6Zt1NVD40CcSwEP85BkqPB6qpPujznFK86V3HfmAl/lColPXJT/BO7jpy18SxwW7U oz02K4i1IjW0QzvV6ZcyVW9TwB7Yg6h+y679gSOrr8ceYdSsb/LIdpk8jL4lJIG/G/Zy qJ3A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=X8nYTVMo; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t18-v6si6613288plo.493.2018.01.28.01.10.30; Sun, 28 Jan 2018 01:10:45 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=X8nYTVMo; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751397AbeA1JJ7 (ORCPT + 99 others); Sun, 28 Jan 2018 04:09:59 -0500 Received: from mail-pg0-f65.google.com ([74.125.83.65]:40620 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750819AbeA1JJz (ORCPT ); Sun, 28 Jan 2018 04:09:55 -0500 Received: by mail-pg0-f65.google.com with SMTP id g16so2312099pgn.7; Sun, 28 Jan 2018 01:09:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:subject:from:to:cc:date:in-reply-to:references :mime-version:content-transfer-encoding; bh=diQeDvuYUNJjAe/fWZxd2EpKPt4z81V46os1OSFpK4s=; b=X8nYTVMot1Y7+CbhvMuSZ+62bPIsqDCcWeQ16Nr39tyviuzFws9jeBYkACV/mQxJlg 7OYy0uLfuWBeeHZ+AMxI3vYandScdr3Q9w0e64AddvI/cRhfGnjFj+yCVBhMOKtSLDEC kzf8upQMHOseliyA40j6+WaccKjyKVIrCS+OF4wwP8VvCFNl5CAnQDMrCCcLhy1PhGio I7ANminRxaAwoJ99LBXsGwG3IfWgm6zZS2DovJ8tBWnZWraWTnaOD6qK0aAqmdL4VtCn v9WGOCjo5LRR/6mEaB9ux3y3C8U21jfwd5nYujlIhEEIV7mvqNZwZY1EtU+mPOIjSKKp u0Sw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:cc:date:in-reply-to :references:mime-version:content-transfer-encoding; bh=diQeDvuYUNJjAe/fWZxd2EpKPt4z81V46os1OSFpK4s=; b=U65+8BiKS5h69t3XzdJ/+R0q5Xa1oRvCQijHDEMTt/ittRjDx+1flJJJ0Bl0kon0IN baAkKCBiheoi2Qi1Mq8TSKfxP8diUE/NoCghmr6bcqOXdFtoPXHaa+hbaDbGVkM3rgjN PIdGhahkkfg5MUZmcVHqZeF1I505aoc6xekgN7Opw9hmAlPq1tJUXg/di/hDT45B8Y2X oprQgy76z6pEuGtLvoLz1rUC8ogXM2+HRZhMbjpCG88OQPaz7dmPvvKd7tdrjlZNIKJC 1UU0jg7EjclLr+IsZsY4qwhNlFD/Oqs+lB/gdlPJGSnlv0X28STxf27fOL/jRtdlEu6A 7a8Q== X-Gm-Message-State: AKwxytcdNL65dl3iVP5r+dFrUz/JGKpz1KWgWFoT4rYmGebymwYDPP+V lH9yDV3vuHBepn0oT81jk1c= X-Received: by 10.99.148.17 with SMTP id m17mr18447573pge.367.1517130594471; Sun, 28 Jan 2018 01:09:54 -0800 (PST) Received: from [10.0.2.15] ([103.212.140.130]) by smtp.googlemail.com with ESMTPSA id y5sm26727625pfd.163.2018.01.28.01.09.50 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 28 Jan 2018 01:09:53 -0800 (PST) Message-ID: <1517130583.2100.11.camel@gmail.com> Subject: Re: [PATCH v2] Staging: iio: ade7758: expand buf_lock to cover both buffer and state protection From: Shreeya Patel To: Jonathan Cameron Cc: lars@metafoo.de, Michael.Hennerich@analog.com, knaack.h@gmx.de, pmeerw@pmeerw.net, gregkh@linuxfoundation.org, linux-iio@vger.kernel.org, devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org Date: Sun, 28 Jan 2018 14:39:43 +0530 In-Reply-To: <20180128083140.6388275c@archlinux> References: <1516898408-8129-1-git-send-email-shreeya.patel23498@gmail.com> <20180128083140.6388275c@archlinux> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.18.5.2-0ubuntu3.2 Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sun, 2018-01-28 at 08:31 +0000, Jonathan Cameron wrote: > On Thu, 25 Jan 2018 22:10:08 +0530 > Shreeya Patel wrote: > > > > > iio_dev->mlock is to be used only by the IIO core for protecting > > device mode changes between INDIO_DIRECT and INDIO_BUFFER. > > > > This patch replaces the use of mlock with the already established > > buf_lock mutex. > > > > Introducing 'unlocked' __ade7758_spi_write_reg_8 and > > __ade7758_spi_read_reg_8 functions to be used by > > ade7758_write_samp_freq > > and ade7758_read_samp_freq which avoids nested locks and maintains > > atomicity between bus and device frequency changes. > > > > Signed-off-by: Shreeya Patel > Hi Shreeya, > > This is now technically correct which is great. > I would make one minor change to make it slightly easier to read. > > The read / write frequency functions now require the buf_lock to > be held.  That's not obvious so I would avoid this but moving > the locking inside the functions where it is then clear that > they are taking the unlocked forms of the register read/ write. > > This would also then make it clear why the normal locked form > of _read_reg_8 is fine in the read_freq case but not the > write_freq case.  (Hence just use the normal locked form > for the read and don't explicitly take the locks when > reading the frequency - leave it to the register read function) > Hi, I have introduced unlocked form of the _read_reg_8 also, which is wrong on my side. I should have discarded the mlocks in the read_freq case first, then there would have been no need of having unlocked form of the _read_reg_8 :( Please discard this patch and I'll send two patches in which first I'll remove the mlocks from the read case and then I'll have only the unlocked form of the _write_reg_8. In this way, there won't be any useless code in the file for the read case. Sorry, it took me a bit more time to understand. Thanks   > Thanks, > > Jonathan > > > > --- > > > > Changes in v2 > >   -Add static keyword to newly introduced functions and remove some > > added comments which are not required. > > > > > >  drivers/staging/iio/meter/ade7758.h      |  2 +- > >  drivers/staging/iio/meter/ade7758_core.c | 47 > > +++++++++++++++++++++++--------- > >  2 files changed, 35 insertions(+), 14 deletions(-) > > > > diff --git a/drivers/staging/iio/meter/ade7758.h > > b/drivers/staging/iio/meter/ade7758.h > > index 6ae78d8..2de81b5 100644 > > --- a/drivers/staging/iio/meter/ade7758.h > > +++ b/drivers/staging/iio/meter/ade7758.h > > @@ -111,7 +111,7 @@ > >   * @trig: data ready trigger registered with iio > >   * @tx: transmit buffer > >   * @rx: receive buffer > > - * @buf_lock: mutex to protect tx and rx > > + * @buf_lock: mutex to protect tx, rx, read and > > write frequency > >   **/ > >  struct ade7758_state { > >   struct spi_device *us; > > diff --git a/drivers/staging/iio/meter/ade7758_core.c > > b/drivers/staging/iio/meter/ade7758_core.c > > index 7b7ffe5..fed4684 100644 > > --- a/drivers/staging/iio/meter/ade7758_core.c > > +++ b/drivers/staging/iio/meter/ade7758_core.c > > @@ -24,17 +24,25 @@ > >  #include "meter.h" > >  #include "ade7758.h" > >   > > -int ade7758_spi_write_reg_8(struct device *dev, u8 reg_address, u8 > > val) > > +static int __ade7758_spi_write_reg_8(struct device *dev, u8 > > reg_address, u8 val) > >  { > > - int ret; > >   struct iio_dev *indio_dev = dev_to_iio_dev(dev); > >   struct ade7758_state *st = iio_priv(indio_dev); > >   > > - mutex_lock(&st->buf_lock); > >   st->tx[0] = ADE7758_WRITE_REG(reg_address); > >   st->tx[1] = val; > >   > > - ret = spi_write(st->us, st->tx, 2); > > + return spi_write(st->us, st->tx, 2); > > +} > > + > > +int ade7758_spi_write_reg_8(struct device *dev, u8 reg_address, u8 > > val) > > +{ > > + int ret; > > + struct iio_dev *indio_dev = dev_to_iio_dev(dev); > > + struct ade7758_state *st = iio_priv(indio_dev); > > + > > + mutex_lock(&st->buf_lock); > > + ret = __ade7758_spi_write_reg_8(dev, reg_address, val); > >   mutex_unlock(&st->buf_lock); > >   > >   return ret; > > @@ -91,7 +99,7 @@ static int ade7758_spi_write_reg_24(struct device > > *dev, u8 reg_address, > >   return ret; > >  } > >   > > -int ade7758_spi_read_reg_8(struct device *dev, u8 reg_address, u8 > > *val) > > +static int __ade7758_spi_read_reg_8(struct device *dev, u8 > > reg_address, u8 *val) > >  { > >   struct iio_dev *indio_dev = dev_to_iio_dev(dev); > >   struct ade7758_state *st = iio_priv(indio_dev); > > @@ -111,7 +119,6 @@ int ade7758_spi_read_reg_8(struct device *dev, > > u8 reg_address, u8 *val) > >   }, > >   }; > >   > > - mutex_lock(&st->buf_lock); > >   st->tx[0] = ADE7758_READ_REG(reg_address); > >   st->tx[1] = 0; > >   > > @@ -124,7 +131,19 @@ int ade7758_spi_read_reg_8(struct device *dev, > > u8 reg_address, u8 *val) > >   *val = st->rx[0]; > >   > >  error_ret: > > + return ret; > > +} > > + > > +int ade7758_spi_read_reg_8(struct device *dev, u8 reg_address, u8 > > *val) > > +{ > > + struct iio_dev *indio_dev = dev_to_iio_dev(dev); > > + struct ade7758_state *st = iio_priv(indio_dev); > > + int ret; > > + > > + mutex_lock(&st->buf_lock); > > + ret = __ade7758_spi_read_reg_8(dev, reg_address, val); > >   mutex_unlock(&st->buf_lock); > > + > >   return ret; > >  } > >   > > @@ -470,7 +489,7 @@ static int ade7758_read_samp_freq(struct device > > *dev, int *val) > >   int ret; > >   u8 t; > >   > > - ret = ade7758_spi_read_reg_8(dev, ADE7758_WAVMODE, &t); > > + ret = __ade7758_spi_read_reg_8(dev, ADE7758_WAVMODE, &t); > >   if (ret) > >   return ret; > >   > > @@ -503,14 +522,14 @@ static int ade7758_write_samp_freq(struct > > device *dev, int val) > >   goto out; > >   } > >   > > - ret = ade7758_spi_read_reg_8(dev, ADE7758_WAVMODE, ®); > > + ret = __ade7758_spi_read_reg_8(dev, ADE7758_WAVMODE, > > ®); > >   if (ret) > >   goto out; > >   > >   reg &= ~(5 << 3); > >   reg |= t << 5; > >   > > - ret = ade7758_spi_write_reg_8(dev, ADE7758_WAVMODE, reg); > > + ret = __ade7758_spi_write_reg_8(dev, ADE7758_WAVMODE, > > reg); > >   > >  out: > >   return ret; > > @@ -523,12 +542,13 @@ static int ade7758_read_raw(struct iio_dev > > *indio_dev, > >       long mask) > >  { > >   int ret; > > + struct ade7758_state *st = iio_priv(indio_dev); > >   > >   switch (mask) { > >   case IIO_CHAN_INFO_SAMP_FREQ: > > - mutex_lock(&indio_dev->mlock); > > + mutex_lock(&st->buf_lock); > >   ret = ade7758_read_samp_freq(&indio_dev->dev, > > val); > > - mutex_unlock(&indio_dev->mlock); > > + mutex_unlock(&st->buf_lock); > >   return ret; > >   default: > >   return -EINVAL; > > @@ -542,14 +562,15 @@ static int ade7758_write_raw(struct iio_dev > > *indio_dev, > >        int val, int val2, long mask) > >  { > >   int ret; > > + struct ade7758_state *st = iio_priv(indio_dev); > >   > >   switch (mask) { > >   case IIO_CHAN_INFO_SAMP_FREQ: > >   if (val2) > >   return -EINVAL; > > - mutex_lock(&indio_dev->mlock); > > + mutex_lock(&st->buf_lock); > >   ret = ade7758_write_samp_freq(&indio_dev->dev, > > val); > > - mutex_unlock(&indio_dev->mlock); > > + mutex_unlock(&st->buf_lock); > >   return ret; > >   default: > >   return -EINVAL;