Received: by 2002:a25:e7d8:0:0:0:0:0 with SMTP id e207csp1627178ybh; Sun, 15 Mar 2020 07:16:46 -0700 (PDT) X-Google-Smtp-Source: ADFU+vvn/MuW9x0/C8+9EWv4zhe4hjgRlGE5TQe+19dy28QBM+64vkVSktL9h1cLdpGfgC8aUJB8 X-Received: by 2002:aca:cf8a:: with SMTP id f132mr13511642oig.151.1584281806677; Sun, 15 Mar 2020 07:16:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1584281806; cv=none; d=google.com; s=arc-20160816; b=PGawgIr/PBxIwXdvjLzn6twv5v43IylJOyFA32rciuPt6oKQEJ2OV6rMWHQ71JW/Yr yFCf9zE1kwb5nwmBYTFw7VpIxSlpieMwqYEfc4PPahXnRZd/K4JEdYVK+wVebnue2XK8 HlSfb83aRwGwUAImHBSjD+vywS56F66FipM0arF445eiXLadxBxcjVCEKYNoGcFN7iun CufK++VuB41SqhWjquZhfMR1Ymw64b2M53uLH/iEfQpCnLz8gMa2AYeCkcWXyuh1rQ8V 40Kcyy8Wyo0Z/kyiG18I3mU/WhDMTPkvVTX69A3dhV1Ho1e9fAo14eVh+b/HuTJYnM0o DJ2A== 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; bh=lk/lC3Fr1Jk+Nnn63wrS3JukQGpABR2vEv2TSnQlBfM=; b=jtMZ0Qk9OQfEPRomfJ9Hh9k+Ybo23b2cZ8KM5MsgXhH1t+bTOnDJjlTGKyMEA9967J s4umUSzEpco3hOXEoEQJ43upYYLXb8wE1wBAxNPH5yv9RvLmMRglzRmA9WGpkOu+ZV4R IhEpZEc1ZoaugnQuUcyisYRh2+QeXY/dzDiyCjtkR+K0em3z44RnqpLKGa9ITHaHOM7w gR0e8Cr7HbK/iBGh0qjtlHr6EpM6nVU8hCMIk5QR02XUnoV5RPgL2VdsZBrYAmxhjKhW vJbAXjHRaRxKE14MQ1zUXyXOnfjlyhGe0QjYP0YETm3C3U5nPpgIRBaZ8V75WuFGhdTv H/TQ== 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 a11si7787871oib.154.2020.03.15.07.16.34; Sun, 15 Mar 2020 07:16:46 -0700 (PDT) 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 S1728745AbgCOOP6 (ORCPT + 99 others); Sun, 15 Mar 2020 10:15:58 -0400 Received: from saturn.retrosnub.co.uk ([46.235.226.198]:53810 "EHLO saturn.retrosnub.co.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727778AbgCOOP6 (ORCPT ); Sun, 15 Mar 2020 10:15:58 -0400 Received: from archlinux (cpc149474-cmbg20-2-0-cust94.5-4.cable.virginm.net [82.4.196.95]) by saturn.retrosnub.co.uk (Postfix; Retrosnub mail submission) with ESMTPSA id 13E439E77F2; Sun, 15 Mar 2020 14:15:55 +0000 (GMT) Date: Sun, 15 Mar 2020 14:15:53 +0000 From: Jonathan Cameron To: Rohit Sarkar Cc: linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v5] iio: adc: max1363: replace uses of mlock Message-ID: <20200315141553.3efc968c@archlinux> In-Reply-To: <5e653c4a.1c69fb81.e805f.b62a@mx.google.com> References: <5e653c4a.1c69fb81.e805f.b62a@mx.google.com> X-Mailer: Claws Mail 3.17.4 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 9 Mar 2020 00:11:06 +0530 Rohit Sarkar wrote: > Replace usage indio_dev's mlock with either local lock or > iio_device_claim_direct_mode. > > Signed-off-by: Rohit Sarkar Nearly there. A few minor issues around handling failure to claim direct mode (which will happen if the buffer is enabled). Jonathan > --- > Changelog v4 -> v5 > Use local lock too at places where driver state needs to be protected. > > Changelog v3 -> v4 > * Fix indentation > > Changelog v2 -> v3 > * use iio_device_claim_direct when switching modes > * replace mlock usage in max1363_write_event_config > > Changelog v1 -> v2 > * Fix indentation > > drivers/iio/adc/max1363.c | 30 +++++++++++++++++++++--------- > 1 file changed, 21 insertions(+), 9 deletions(-) > > diff --git a/drivers/iio/adc/max1363.c b/drivers/iio/adc/max1363.c > index 5c2cc61b666e..856f523e7018 100644 > --- a/drivers/iio/adc/max1363.c > +++ b/drivers/iio/adc/max1363.c > @@ -150,6 +150,7 @@ struct max1363_chip_info { > * @current_mode: the scan mode of this chip > * @requestedmask: a valid requested set of channels > * @reg: supply regulator > + * @lock lock to ensure state is consistent > * @monitor_on: whether monitor mode is enabled > * @monitor_speed: parameter corresponding to device monitor speed setting > * @mask_high: bitmask for enabled high thresholds > @@ -169,6 +170,7 @@ struct max1363_state { > const struct max1363_mode *current_mode; > u32 requestedmask; > struct regulator *reg; > + struct mutex lock; > > /* Using monitor modes and buffer at the same time is > currently not supported */ > @@ -364,7 +366,11 @@ static int max1363_read_single_chan(struct iio_dev *indio_dev, > struct max1363_state *st = iio_priv(indio_dev); > struct i2c_client *client = st->client; > > - mutex_lock(&indio_dev->mlock); > + ret = iio_device_claim_direct_mode(indio_dev); If this fails, no locks are held, so return directly here. You don't want to call iio_device_release_direct_mode in that case. General rule of thumb in kernel is that a function returning an error should have no 'side effects' - it should be as if it never ran. > + mutex_lock(&st->lock); > + > + if (ret < 0) > + goto error_ret; > /* > * If monitor mode is enabled, the method for reading a single > * channel will have to be rather different and has not yet > @@ -372,7 +378,7 @@ static int max1363_read_single_chan(struct iio_dev *indio_dev, > * > * Also, cannot read directly if buffered capture enabled. > */ > - if (st->monitor_on || iio_buffer_enabled(indio_dev)) { > + if (st->monitor_on) { > ret = -EBUSY; > goto error_ret; > } > @@ -404,8 +410,10 @@ static int max1363_read_single_chan(struct iio_dev *indio_dev, > data = rxbuf[0]; > } > *val = data; > + > error_ret: > - mutex_unlock(&indio_dev->mlock); > + mutex_unlock(&st->lock); > + iio_device_release_direct_mode(indio_dev); > return ret; > > } > @@ -705,9 +713,9 @@ static ssize_t max1363_monitor_store_freq(struct device *dev, > if (!found) > return -EINVAL; > > - mutex_lock(&indio_dev->mlock); > + mutex_lock(&st->lock); > st->monitor_speed = i; > - mutex_unlock(&indio_dev->mlock); > + mutex_unlock(&st->lock); > > return 0; > } > @@ -810,12 +818,12 @@ static int max1363_read_event_config(struct iio_dev *indio_dev, > int val; > int number = chan->channel; > > - mutex_lock(&indio_dev->mlock); > + mutex_lock(&st->lock); > if (dir == IIO_EV_DIR_FALLING) > val = (1 << number) & st->mask_low; > else > val = (1 << number) & st->mask_high; > - mutex_unlock(&indio_dev->mlock); > + mutex_unlock(&st->lock); > > return val; > } > @@ -962,7 +970,9 @@ static int max1363_write_event_config(struct iio_dev *indio_dev, > u16 unifiedmask; > int number = chan->channel; > > - mutex_lock(&indio_dev->mlock); > + iio_device_claim_direct_mode(indio_dev); You have to check the result claim_direct_mode. It will fail if we are in buffered mode. If that happens just return -EBUSY and don't do anything else. > + mutex_lock(&st->lock); > + > unifiedmask = st->mask_low | st->mask_high; > if (dir == IIO_EV_DIR_FALLING) { > > @@ -989,7 +999,8 @@ static int max1363_write_event_config(struct iio_dev *indio_dev, > > max1363_monitor_mode_update(st, !!(st->mask_high | st->mask_low)); > error_ret: > - mutex_unlock(&indio_dev->mlock); > + mutex_unlock(&st->lock); > + iio_device_release_direct_mode(indio_dev); > > return ret; > } > @@ -1587,6 +1598,7 @@ static int max1363_probe(struct i2c_client *client, > > st = iio_priv(indio_dev); > > + mutex_init(&st->lock); > st->reg = devm_regulator_get(&client->dev, "vcc"); > if (IS_ERR(st->reg)) { > ret = PTR_ERR(st->reg);