Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp3856764pxa; Sun, 9 Aug 2020 14:18:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyTUe9uV9xc2QeEOi48pN65wrC0Gliu6honWL4ZlMVS3tuyl9n6O8oq1cfWb4o92sMsuxhT X-Received: by 2002:aa7:d410:: with SMTP id z16mr18002620edq.287.1597007935288; Sun, 09 Aug 2020 14:18:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597007935; cv=none; d=google.com; s=arc-20160816; b=HG8/J7bVmtdXjswyjEEpULKTY93KgCepzNQFuM4Uht6Nb4JZFRdYUzJd5Rlg/C2FKh 7Iogq1VX1Cbxd9SYbvjtKzbq2i2U7i4C0Y2rbLyv6jw5KqkmHQUdJvCx7Pk2Qd8xeXnE eHHShvleREWREDW14drGZeu484mw+3xxF6uL53L8iknyQomG/3WqxkKo+6KhvCeePSyJ pkwuiQ1oS7H0+WC3AY0dImWAGz7kboIYzp1FIi5CvDicH5YLIQYcnCz8NY98c4WmFup4 CcrMMPlHKmn57tAIFpaCn6NV81PHGCF/gUcR9MdRUEKDnE183HGkZX5kdan664tjvjqm cUoQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:content-transfer-encoding :mime-version:subject:from:message-id:date:dkim-signature; bh=Sq7+8ZhhGviWpjawz5/AoSWM32DfGgXVCy+kpnU4u9E=; b=ccCmZx77xGUBml0ULWnDREFqj+ttCgzDzY4SosneY6k4jtdW8EoDY6U9EbfH7NKS6t NyOPPGBgqA2KBEwnP3sRHSck7aYfCtdna1JgokeYDovpcZH1mzk+IxyKDHHgmoMIiObn RGXkYziUGNPmOCZn8Lv5sTcqDjHXSBlxVZtRNw6EfwaqE625C+5FCBs22paejB84R3Jd g2SPxNBki8D4OwL2aXybYij8fjd/EqRmjXtqaM3y6MoxKnA9Sf6JbmqXe1yk0kB0jhnV 7yqGHtOHT59W063bFOX7P0AFCwT1vtmohS7DX5X9MxfU4N+uJkDT8A92+v0wh+mNH0Cw CZ+w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rere.qmqm.pl header.s=1 header.b=gkAmEj+C; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id lo3si9608583ejb.219.2020.08.09.14.18.31; Sun, 09 Aug 2020 14:18:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@rere.qmqm.pl header.s=1 header.b=gkAmEj+C; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726338AbgHIVQ6 (ORCPT + 99 others); Sun, 9 Aug 2020 17:16:58 -0400 Received: from rere.qmqm.pl ([91.227.64.183]:46410 "EHLO rere.qmqm.pl" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726299AbgHIVQ6 (ORCPT ); Sun, 9 Aug 2020 17:16:58 -0400 Received: from remote.user (localhost [127.0.0.1]) by rere.qmqm.pl (Postfix) with ESMTPSA id 4BPsNm4rQzz8y; Sun, 9 Aug 2020 23:16:56 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=rere.qmqm.pl; s=1; t=1597007817; bh=FGArzQHPDlg14jyAN9XMnsdCLbXsrN+ddGAJOjuKbns=; h=Date:From:Subject:To:Cc:From; b=gkAmEj+CedVDWJbR7xk/YxWSoq0UT/RpHe72jiC+y65pO/VC16m0wwu5o1zTT/A0i aGJcRwcQcmOdtV+fr0VEbgRE5DN3+8/ADqipGqCEiNmbp3nF3F8YCA/7mwGupI3Ee5 2+eHTIXNp0rjcdHp+APWzmSake4RLXU1htMFzo0nh3O04dlWfg34J7NMJebRtUi5Jf CRcVjKLhJgezGD78zk75OoxrMXgeIU1vIQ7SIsCc0ob3fb29ZU76rPqSVu8mDi6CmJ e7qasE4PjOmp9k6DzVNdkJqTa4oR+5Ai+hQF+D48TZ2psIfixy9yMi0gKKulqHBNqL MWc0kMcy5QNXw== X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.102.4 at mail Date: Sun, 09 Aug 2020 23:16:53 +0200 Message-Id: From: =?UTF-8?q?Micha=C5=82=20Miros=C5=82aw?= Subject: [PATCH] regulator: simplify locking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To: Dmitry Osipenko , Liam Girdwood , Mark Brown Cc: linux-kernel@vger.kernel.org Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Simplify regulator locking by removing locking around locking. rdev->ref is now accessed only when the lock is taken. The code still smells fishy, but now its obvious why. Fixes: f8702f9e4aa7 ("regulator: core: Use ww_mutex for regulators locking") Signed-off-by: Michał Mirosław --- drivers/regulator/core.c | 37 ++++++-------------------------- include/linux/regulator/driver.h | 1 - 2 files changed, 6 insertions(+), 32 deletions(-) diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c index 9e18997777d3..b0662927487c 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -45,7 +45,6 @@ pr_debug("%s: " fmt, rdev_get_name(rdev), ##__VA_ARGS__) static DEFINE_WW_CLASS(regulator_ww_class); -static DEFINE_MUTEX(regulator_nesting_mutex); static DEFINE_MUTEX(regulator_list_mutex); static LIST_HEAD(regulator_map_list); static LIST_HEAD(regulator_ena_gpio_list); @@ -150,32 +149,13 @@ static bool regulator_ops_is_valid(struct regulator_dev *rdev, int ops) static inline int regulator_lock_nested(struct regulator_dev *rdev, struct ww_acquire_ctx *ww_ctx) { - bool lock = false; int ret = 0; - mutex_lock(®ulator_nesting_mutex); + if (ww_ctx || !mutex_trylock_recursive(&rdev->mutex.base)) + ret = ww_mutex_lock(&rdev->mutex, ww_ctx); - if (ww_ctx || !ww_mutex_trylock(&rdev->mutex)) { - if (rdev->mutex_owner == current) - rdev->ref_cnt++; - else - lock = true; - - if (lock) { - mutex_unlock(®ulator_nesting_mutex); - ret = ww_mutex_lock(&rdev->mutex, ww_ctx); - mutex_lock(®ulator_nesting_mutex); - } - } else { - lock = true; - } - - if (lock && ret != -EDEADLK) { + if (ret != -EDEADLK) rdev->ref_cnt++; - rdev->mutex_owner = current; - } - - mutex_unlock(®ulator_nesting_mutex); return ret; } @@ -205,16 +185,11 @@ EXPORT_SYMBOL_GPL(regulator_lock); */ void regulator_unlock(struct regulator_dev *rdev) { - mutex_lock(®ulator_nesting_mutex); + if (WARN_ON_ONCE(rdev->ref_cnt <= 0)) + return; - if (--rdev->ref_cnt == 0) { - rdev->mutex_owner = NULL; + if (--rdev->ref_cnt == 0) ww_mutex_unlock(&rdev->mutex); - } - - WARN_ON_ONCE(rdev->ref_cnt < 0); - - mutex_unlock(®ulator_nesting_mutex); } EXPORT_SYMBOL_GPL(regulator_unlock); diff --git a/include/linux/regulator/driver.h b/include/linux/regulator/driver.h index 8539f34ae42b..2fe5b1bcbe2f 100644 --- a/include/linux/regulator/driver.h +++ b/include/linux/regulator/driver.h @@ -448,7 +448,6 @@ struct regulator_dev { struct blocking_notifier_head notifier; struct ww_mutex mutex; /* consumer lock */ - struct task_struct *mutex_owner; int ref_cnt; struct module *owner; struct device dev; -- 2.20.1