From patchwork Sat Sep 30 20:05:21 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Himanshi Jain X-Patchwork-Id: 9979585 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 63DFA60375 for ; Sat, 30 Sep 2017 20:05:28 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5407528DEB for ; Sat, 30 Sep 2017 20:05:28 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4901128E09; Sat, 30 Sep 2017 20:05:28 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DC34F28DEB for ; Sat, 30 Sep 2017 20:05:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751251AbdI3UF1 (ORCPT ); Sat, 30 Sep 2017 16:05:27 -0400 Received: from mail-pf0-f194.google.com ([209.85.192.194]:38507 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751217AbdI3UF0 (ORCPT ); Sat, 30 Sep 2017 16:05:26 -0400 Received: by mail-pf0-f194.google.com with SMTP id a7so1890042pfj.5 for ; Sat, 30 Sep 2017 13:05:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:mime-version:content-disposition :user-agent; bh=/Mf9i1jtM1sT6+9NWVaMzH1zejM/Udh29EjnLrAYZJk=; b=hzZPkrWgf+1NTz7wzmYTAne0Z8VH4nq/3r1YB6AIxC8syXCzRQl5Mo3HbDFN4zk659 wnbqLuyAXKD4BB6hes+OlKEv1ozcbt2ivXnkBVfYeqVKSpjpxj1jlB9lGIlu+0CrCrM4 3GSliP2EAn1B6Ns4Ap5jPputkA+3bt1JFWTIh8SXFxVzuI2gy6Lyg2/WUQlIRrHTZ13N qKjFsecInvOVUlBbvKjGAYQ2GsqzELQ7cptJbxe6xL4w3l4AMzRbvcWhKHXZY4EqiKV2 qqhb1M0n4KsE2O6bAl8Lw1q8a0r3gK5p/DBIrTXYa5L6Dd52qe3LesGFUykrk2qV+isl PhTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:user-agent; bh=/Mf9i1jtM1sT6+9NWVaMzH1zejM/Udh29EjnLrAYZJk=; b=ct5Q3s6pwcIMp9EUwcPCniC8GM5I1v0Jh4kL7HhbxDBGDMC64y7iV1P5Sfxm+Qr8dm sy25NNQHUClYXPc87kWHpNZT1bPzBJC51gJBNx3cL3UUATXmX53WQoCHhSvYuke+n3Xb YmDh7U5vorxvxKvF22y3Z7SEyRizJcm/YYsntbw3wg05xSPB0ObAyuMHxyksZB26I/e6 25rYITBtehrWu/KWrN2e97cTY027MWowLOQa2eAcNmDeKTCDQX9nnEbpYdYrBzRM35wv LxMJqg/3UuCk/F5wCdH9m/hNtTNHQs7cOAxjkkHZBy12myzf8tnODmO0g/44dAs3IqCo BrEw== X-Gm-Message-State: AHPjjUh8yS/DurG2HcJUCPPlGfnc3keCll/GuSL17nlgE/J/tw/gsBz0 Qk0ukBZ+c4RPVwZlN2CpqzM= X-Google-Smtp-Source: AOwi7QCyt9CDEAU2LsKfHmjGa1k+jU0gxMEHJzj/0JP5tfk96kAzYWz+eTNXJ3koE3I8agljewAFSg== X-Received: by 10.98.15.13 with SMTP id x13mr11045757pfi.249.1506801926157; Sat, 30 Sep 2017 13:05:26 -0700 (PDT) Received: from localhost ([106.201.8.116]) by smtp.gmail.com with ESMTPSA id w27sm1117145pfi.18.2017.09.30.13.05.24 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Sat, 30 Sep 2017 13:05:25 -0700 (PDT) Date: Sun, 1 Oct 2017 01:35:21 +0530 From: Himanshi Jain To: outreachy-kernel@googlegroups.com, jic23@kernel.org, knaack.h@gmx.de, lars@metafoo.de, pmeerw@pmeerw.net, linux-iio@vger.kernel.org, daniel.baluta@gmail.com, amsfield22@gmail.com, Michael.Hennerich@analog.com, gregkh@linuxfoundation.org Subject: [PATCH] staging: iio: ade7753: expanding buffer lock to cover both buffer and state protection Message-ID: <20170930200521.GA19705@himanshi-Inspiron-5558> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Dropping the extra lock (used for protecting the write frequency) by expanding the buffer lock to cover both buffer and state protection. Doing this by introducing a new function (__ade7753_spi_write_reg_16) making buffer changes without locking the state, to avoid nested locks while making device frequency changes. Signed-off-by: Himanshi Jain --- drivers/staging/iio/meter/ade7753.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/drivers/staging/iio/meter/ade7753.c b/drivers/staging/iio/meter/ade7753.c index b704b24..c44eb57 100644 --- a/drivers/staging/iio/meter/ade7753.c +++ b/drivers/staging/iio/meter/ade7753.c @@ -80,13 +80,11 @@ * @us: actual spi_device * @tx: transmit buffer * @rx: receive buffer - * @buf_lock: mutex to protect tx and rx - * @lock: protect sensor data + * @buf_lock: mutex to protect tx, rx and write frequency **/ struct ade7753_state { struct spi_device *us; struct mutex buf_lock; - struct mutex lock; /* protect sensor data */ u8 tx[ADE7753_MAX_TX] ____cacheline_aligned; u8 rx[ADE7753_MAX_RX]; }; @@ -109,6 +107,19 @@ static int ade7753_spi_write_reg_8(struct device *dev, return ret; } +static int __ade7753_spi_write_reg_16(struct device *dev, u8 reg_address, + u16 value) +{ + struct iio_dev *indio_dev = dev_to_iio_dev(dev); + struct ade7753_state *st = iio_priv(indio_dev); + + st->tx[0] = ADE7753_WRITE_REG(reg_address); + st->tx[1] = (value >> 8) & 0xFF; + st->tx[2] = value & 0xFF; + + return spi_write(st->us, st->tx, 3); +} + static int ade7753_spi_write_reg_16(struct device *dev, u8 reg_address, u16 value) { @@ -117,10 +128,7 @@ static int ade7753_spi_write_reg_16(struct device *dev, u8 reg_address, struct ade7753_state *st = iio_priv(indio_dev); mutex_lock(&st->buf_lock); - st->tx[0] = ADE7753_WRITE_REG(reg_address); - st->tx[1] = (value >> 8) & 0xFF; - st->tx[2] = value & 0xFF; - ret = spi_write(st->us, st->tx, 3); + ret = __ade7753_spi_write_reg_16(dev, reg_address, value); mutex_unlock(&st->buf_lock); return ret; @@ -485,7 +493,7 @@ static ssize_t ade7753_write_frequency(struct device *dev, if (!val) return -EINVAL; - mutex_lock(&st->lock); + mutex_lock(&st->buf_lock); t = 27900 / val; if (t > 0) @@ -503,10 +511,10 @@ static ssize_t ade7753_write_frequency(struct device *dev, reg &= ~(3 << 11); reg |= t << 11; - ret = ade7753_spi_write_reg_16(dev, ADE7753_MODE, reg); + ret = __ade7753_spi_write_reg_16(dev, ADE7753_MODE, reg); out: - mutex_unlock(&st->lock); + mutex_unlock(&st->buf_lock); return ret ? ret : len; } @@ -581,7 +589,6 @@ static int ade7753_probe(struct spi_device *spi) st = iio_priv(indio_dev); st->us = spi; mutex_init(&st->buf_lock); - mutex_init(&st->lock); indio_dev->name = spi->dev.driver->name; indio_dev->dev.parent = &spi->dev;