Received: by 10.223.176.5 with SMTP id f5csp2530585wra; Thu, 1 Feb 2018 01:54:15 -0800 (PST) X-Google-Smtp-Source: AH8x227VAeHCyPWRqCdbvLDp4VnsYWIQbhEDqSvHfDXx1MGp4hVk0+YIqjKzpDhRRkEm9dBx7Z77 X-Received: by 2002:a17:902:d83:: with SMTP id 3-v6mr7199736plv.82.1517478855602; Thu, 01 Feb 2018 01:54:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1517478855; cv=none; d=google.com; s=arc-20160816; b=e376xEVRCpn8GFAOy6FoX6Jpb1NQYqUtjia3jD2tOeaXfqmt5EHl/H47tm5qSUPcpA b3eQpHGme+853XqcXQNt5KEVudTk3jbdUKx/H1FwQuBt2Yf5NRBcCGMecsh4b0E1mwxt k0i3uBK15+xWNZrHQorh17+drA4axUPYL43KHGhxzqsM5nEO+BtmEmfq/gkVNrAxYAGf zWKWJDMPNkpQr7yvVMqgWu3Orm+mET9yGJ6LdxSmplXs7WlVaUe8xL++UIo4NgGhYzZZ VgYIP44VoPWtn3EYMeyyigJkjKBWDgLy6TCqzx2svYUF2wCSYUIm8lbiZ9B4M+YCyH64 NNNw== 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:message-id:subject:cc:to:from:date :arc-authentication-results; bh=59p7ieuMGw5DGsE3jhBqI/KkvpWwM115mHGoFtLbdPo=; b=XxiGIRq/rAH6cDerdpBxu1EEj7xO+6aCJvW8z6KpvBckRo4rsUZqIp2BYjZrIlRj7q g48ET3R5u5WN0N9hWPJPjukLzatJ6KP7XdDtcY+YD/HWMyTwGrEGXMm+V+MtVrVt6d2f I9v0Jys7WyweWCImLlkqhirrqIFnZSEkgcdVpGIy5nzeWt66fBjo+SEfbW3c5sA/ZupR jxOFA5NN6VEHncTHU+M51KHvCalHWL31X9FS0Dt/ayax4lLPegTjY42xPas/VxOEgXnj XVxWrv2gockEpEbUIAoKlfR9o9NfdDuvAJhZJG0CUzCAJWytg1zGQQsD+bdajLMVmWvb jLKA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f3-v6si5882839plo.610.2018.02.01.01.54.00; Thu, 01 Feb 2018 01:54:15 -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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751936AbeBAJxa (ORCPT + 99 others); Thu, 1 Feb 2018 04:53:30 -0500 Received: from szxga04-in.huawei.com ([45.249.212.190]:5162 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1751520AbeBAJx2 (ORCPT ); Thu, 1 Feb 2018 04:53:28 -0500 Received: from DGGEMS410-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 8564C9B6741FF; Thu, 1 Feb 2018 17:53:13 +0800 (CST) Received: from localhost (10.202.226.42) by DGGEMS410-HUB.china.huawei.com (10.3.19.210) with Microsoft SMTP Server (TLS) id 14.3.361.1; Thu, 1 Feb 2018 17:53:13 +0800 Date: Thu, 1 Feb 2018 09:53:03 +0000 From: Jonathan Cameron To: Shreeya Patel CC: , , , , , , , , Subject: Re: [PATCH v3] Staging: iio: ade7758: Expand buf_lock to cover both buffer and state protection Message-ID: <20180201095303.000066b2@huawei.com> In-Reply-To: <1517335217-6163-1-git-send-email-shreeya.patel23498@gmail.com> References: <1517335217-6163-1-git-send-email-shreeya.patel23498@gmail.com> X-Mailer: Claws Mail 3.16.0 (GTK+ 2.24.31; i686-w64-mingw32) MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Originating-IP: [10.202.226.42] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 30 Jan 2018 23:30:17 +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' forms of read and write registers. The > read/write frequency functions now require buf_lock to be held. > That's not obvious so 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. > > Signed-off-by: Shreeya Patel Good, this should now work nicely. I have suggested a further improvement inline which I think will improve code clarity by moving the lock to where it is obvious what it is protecting. Thanks, Jonathan > --- > > Changes in v2 > -Add static keyword to newly introduced functions and remove some > added comments which are not required. > > Changes in v3 > -Remove some useless mlocks and send it as another patch. > Also make the necessary change in the current patch associated with > the new patch with commit id 88eba33. Make commit message more > appropriate. > > > drivers/staging/iio/meter/ade7758.h | 2 +- > drivers/staging/iio/meter/ade7758_core.c | 40 ++++++++++++++++++++++++-------- > 2 files changed, 31 insertions(+), 11 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 227dbfc..38e5d67 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; > } > > @@ -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; > @@ -540,14 +559,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); Hmm. There is a trade off here between complexity of patch (this is the minimal version on that side) and neatness of the result. It isn't readily apparent that ade7758_write_samp_freq requires the locks to be taken so I think we should really move them inside the function. (it's the only function that does other than the obvious unlocked variants you introduce). That would mean changing the function definition to take indio_dev instead of the device pointer. I think that would be a nice further improvement to the patch. Thanks, Jonathan > + mutex_unlock(&st->buf_lock); > return ret; > default: > return -EINVAL;