From nobody Wed Dec 31 12:05:48 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 593ADC4332F for ; Fri, 3 Nov 2023 07:42:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346208AbjKCHmz (ORCPT ); Fri, 3 Nov 2023 03:42:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51100 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233433AbjKCHmx (ORCPT ); Fri, 3 Nov 2023 03:42:53 -0400 Received: from APC01-TYZ-obe.outbound.protection.outlook.com (mail-tyzapc01on2094.outbound.protection.outlook.com [40.107.117.94]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1A7D81A6 for ; Fri, 3 Nov 2023 00:42:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=JyEXn7nMRXc3innWyS9dW+DnwKJV4jFJ0Q7HeJcg4oU6nP6QhoFvI1nuXdx2SnsY1n7EUE3moHwa1S7GoTP0kh5/Co4UD/SaoIcEAyM/lumTiu8RfREmgrBs0RyLo7SVCPTpNyoM0WG8nAJMO4+DdczP8MNsLKco5JxO/U6amR7x0ZfiTkSdAZZ6Oi6DXbLWG1kDmCW5NItMtq/S2NJJcKel436iLvu86uaKcgEf+9ZQYJHN+/x+rhjOUrbPEpKMZxLsioVOqlYKV50Nw8tWhftAqBUZXrOayKcmPldIAJef7MGTJoNzRZkGFoUqmr5t/DU2dGQr7dpeMk3oZyRSKg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=TbCTJz+/AMiDihj13TXUqJyyrAGtZ7GIungA37tS0KA=; b=YrQpJhhgCAtEujGw05uoDV47az0aJjMGPmqyMRF2XX8tXRHmPUXGGucUs6pISZ1sy0e/TZUFuwYigF7bxiD73eYUg0rN7BTzgmbE6zkuaihszguygoCtBzFLhKZywtUPVXqvSeFGm5UKLjLHvyxWC6oc+4iuZUEi4X0S+CcIUU9sf8a/ZnAuSFJO+BktPmcQfPSu7KwlId73BFrDRG11jxnRn9j/iq/4GbVPvFs8Ml3gjxw0GQhjrfwOm6XTRiluVqT5KxpR5VYyNp9QTy8P9niCumkIrhzDAg6lpr3AWRqiTS5jmbzoKnMv6JwjY1QnHfjFLR2rugR88s0F5qsQZA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=vivo.com; dmarc=pass action=none header.from=vivo.com; dkim=pass header.d=vivo.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=vivo.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=TbCTJz+/AMiDihj13TXUqJyyrAGtZ7GIungA37tS0KA=; b=XRe0JmP2paoGEs4auGabIJzfQmpaH0D5CTXVsMTgD5QiVFH+ICvJWbVGXpccwMUQ2gINk8kQgdV5zLTOVA9/hQEoxEvZeUOgbl9hGxQWY3temYmvM+HiOjTsssbLkr/c6F8VEg+Ywex0P0tp8GXvN3r1gIfZf4Thg2EDNWP3cWbw2YruGC3F4zX4uDt84ydvrK3tjlZ52N4vjItdlrUTOqKoqJZWwiFurUpn6Bioi+/wxa0rhFW0xm7KcHfQyNXPs+NEYRQVXNi6rsIiLLUdtIJcxfdfRRm4Sej4qo/KuSYbzSq4ic0/dyJETwkvWRJsTF6WmTFGpCTxHWtduHAdKg== Authentication-Results: dkim=none (message not signed) header.d=none;dmarc=none action=none header.from=vivo.com; Received: from PUZPR06MB4742.apcprd06.prod.outlook.com (2603:1096:301:b5::13) by KL1PR06MB6178.apcprd06.prod.outlook.com (2603:1096:820:d8::6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6954.21; Fri, 3 Nov 2023 07:42:43 +0000 Received: from PUZPR06MB4742.apcprd06.prod.outlook.com ([fe80::7ce2:1ea:7294:50fd]) by PUZPR06MB4742.apcprd06.prod.outlook.com ([fe80::7ce2:1ea:7294:50fd%7]) with mapi id 15.20.6954.021; Fri, 3 Nov 2023 07:42:43 +0000 From: Rui Zhang To: Mark Brown Cc: Douglas Anderson , Rui Zhang , opensource.kernel@vivo.com, Liam Girdwood , linux-kernel@vger.kernel.org Subject: [PATCH] regulator: core: Only increment use_count when enable_count changes Date: Fri, 3 Nov 2023 15:42:31 +0800 Message-Id: <20231103074231.8031-1-zr.zhang@vivo.com> X-Mailer: git-send-email 2.30.2 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: SI1PR02CA0024.apcprd02.prod.outlook.com (2603:1096:4:1f4::12) To PUZPR06MB4742.apcprd06.prod.outlook.com (2603:1096:301:b5::13) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: PUZPR06MB4742:EE_|KL1PR06MB6178:EE_ X-MS-Office365-Filtering-Correlation-Id: 21498d60-8597-4ab5-ad2d-08dbdc4076bc X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: MPX6Zjm1tQZwBIOMIz6WHuxMu+KG40ba7yTZshpMiNRxbqzI74Vxs6A7l/b6VqsVz9g4NRw5VDL5achOxbzwC0vtMsWmCQo/T1JmU2lXSR0bfwDQEqEdEd5aDTHsXRLK5hWk+mLfRq26Hzkcil6c96I6y3JvXW1rGfjThuzUE2LaT7oblK5gQe3AmoJ5L2d9ym9xzy5znIfN1jFOjuTOM2aHEVEcm4vajvGbYBr6oEusILdwtWUX1TEhsIYkWq/d/opJMZbtrYvbF+9L7QWKJci8EJMYkKBfKgyloCodx9nFbucX/UiUlQhAcEIjTFQyXbsvyuDqrKT8WUKAjd/B0GAIxtnxDU10917Q+RD4lNfSQ4XZRF596NU7Vh3cZrn9P9PKHUOHGZzDoB8dscTMfQ9XLbWQjjJtSSIc6LqSd6CqZS9gicAh9yv0mkMT2npUosRUf2Mcuu3bZ+f8Ov3wrkZJEvF5ZJJm5Gmr0cjomM3fSZPfC0M4mQdugyrPP8kMqfm/9Xkelk8jIvJFxEtjQkEjNQAI9l7x17or578C3zOvvrsQOSdyXk6YiSH2Kw0hhvgCO1SgIh4L2di9xsw2kRBMYfCz4kPzcXlKx8FUUffHyXlbb9MWAhucZjIKGYFk X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:PUZPR06MB4742.apcprd06.prod.outlook.com;PTR:;CAT:NONE;SFS:(13230031)(39860400002)(136003)(346002)(376002)(366004)(396003)(230922051799003)(186009)(64100799003)(1800799009)(451199024)(2616005)(6512007)(26005)(38100700002)(86362001)(38350700005)(2906002)(83380400001)(478600001)(4326008)(6506007)(1076003)(52116002)(6666004)(8676002)(54906003)(66556008)(66476007)(6916009)(36756003)(316002)(8936002)(6486002)(41300700001)(5660300002)(66946007);DIR:OUT;SFP:1102; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: =?us-ascii?Q?zsrzu0DNz2BL9vFzPpPNEYCVg1a8pdvUaOAxP1soslOAIL8bduLwBaRLx2gx?= =?us-ascii?Q?bJwnpdk+sg9hJgCSLDsqROlICYfLr/w7ok50WyUSbmOtQMft1Yx9SxxltjkL?= =?us-ascii?Q?i2aoWyJIU26r4l+vaPFXujb36/3Mny4+5Lc2YhvI80Mw2N1wiuCrf6q0esCA?= =?us-ascii?Q?osNP10z2N6mAKJiSVs31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nSk?= =?us-ascii?Q?winZOZ90VXEBRDqPSVgiyDFcTL4pBWSWqHBiY3E1aD5OX6TR9jIXOx9Cq0p/?= =?us-ascii?Q?VBF5EsRYEgdFnuEJZNMSAxbTRrqSQz4uh+ecNuEClc9eZT1jcmg5u5tp6G8L?= =?us-ascii?Q?+c0A+5mdo2fAW9GXd7YjZRFJ1r6B0QdSFkiw3Mz3SA7QaZ+23rfYdL5KkQyH?= =?us-ascii?Q?5DPvlNTXoxEQWgk/WoGRC+Dufm4KrHSZwNdpQXVSKbzHFjlxJ6W27u8tHFA4?= =?us-ascii?Q?4Gej6vtlxN6QwFOl5YbKcnhGSVqU337YvvSgV3DxGDGDIzi0Ur3GBZg+CwDb?= =?us-ascii?Q?1FOxqtlgrOJlQnupJWsZdj52RpO3vRSersm/kgyuD5Z1J3jGEg6n43ecN8UV?= =?us-ascii?Q?wJ6LKE3EmqM4K2nle0qrNpwtWiQRp6fJflX9Gji/GcFIhfNaoDYXmlDDpmrl?= =?us-ascii?Q?rE0z0fd/H598nzvDSxrYcRun3qxiURVU0Wl9Cc2Z1wgMTKw/ZNxYDnjHmXU0?= =?us-ascii?Q?RJlnv6ELsl7Ro9LrqHqPMSatQwvgUzyAqf40Z8g1?= X-OriginatorOrg: vivo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 21498d60-8597-4ab5-ad2d-08dbdc4076bc X-MS-Exchange-CrossTenant-AuthSource: PUZPR06MB4742.apcprd06.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Nov 2023 07:42:43.7237 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 923e42dc-48d5-4cbe-b582-1a797a6412ed X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: qFfCgKl96FBXV3oGSZTFTH0Qi85tD2ScOWbiJrzQl2eRexcnnykzl2seeAk4ntuJ3Vet0VSVbHEoePWSG0mnXQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: KL1PR06MB6178 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The use_count of a regulator should only be incremented when the enable_count changes from 0 to 1. Similarly, the use_count should only be decremented when the enable_count changes from 1 to 0. In the previous implementation, use_count was sometimes decremented to 0 when some consumer called unbalanced disable, leading to unexpected disable even the regulator is enabled by other consumers. With this change, the use_count accurately reflects the number of users which the regulator is enabled. Signed-off-by: Rui Zhang --- drivers/regulator/core.c | 56 +++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c index 3137e40fcd3e..a7b3e548ea5a 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -2918,7 +2918,8 @@ static int _regulator_enable(struct regulator *regula= tor) /* Fallthrough on positive return values - already enabled */ } =20 - rdev->use_count++; + if (regulator->enable_count =3D=3D 1) + rdev->use_count++; =20 return 0; =20 @@ -2993,37 +2994,40 @@ static int _regulator_disable(struct regulator *reg= ulator) =20 lockdep_assert_held_once(&rdev->mutex.base); =20 - if (WARN(rdev->use_count <=3D 0, + if (WARN(regulator->enable_count =3D=3D 0, "unbalanced disables for %s\n", rdev_get_name(rdev))) return -EIO; =20 - /* are we the last user and permitted to disable ? */ - if (rdev->use_count =3D=3D 1 && - (rdev->constraints && !rdev->constraints->always_on)) { - - /* we are last user */ - if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) { - ret =3D _notifier_call_chain(rdev, - REGULATOR_EVENT_PRE_DISABLE, - NULL); - if (ret & NOTIFY_STOP_MASK) - return -EINVAL; - - ret =3D _regulator_do_disable(rdev); - if (ret < 0) { - rdev_err(rdev, "failed to disable: %pe\n", ERR_PTR(ret)); - _notifier_call_chain(rdev, - REGULATOR_EVENT_ABORT_DISABLE, + if (regulator->enable_count =3D=3D 1) { + /* disabling last enable_count from this regulator */ + /* are we the last user and permitted to disable ? */ + if (rdev->use_count =3D=3D 1 && + (rdev->constraints && !rdev->constraints->always_on)) { + + /* we are last user */ + if (regulator_ops_is_valid(rdev, REGULATOR_CHANGE_STATUS)) { + ret =3D _notifier_call_chain(rdev, + REGULATOR_EVENT_PRE_DISABLE, + NULL); + if (ret & NOTIFY_STOP_MASK) + return -EINVAL; + + ret =3D _regulator_do_disable(rdev); + if (ret < 0) { + rdev_err(rdev, "failed to disable: %pe\n", ERR_PTR(ret)); + _notifier_call_chain(rdev, + REGULATOR_EVENT_ABORT_DISABLE, + NULL); + return ret; + } + _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE, NULL); - return ret; } - _notifier_call_chain(rdev, REGULATOR_EVENT_DISABLE, - NULL); - } =20 - rdev->use_count =3D 0; - } else if (rdev->use_count > 1) { - rdev->use_count--; + rdev->use_count =3D 0; + } else if (rdev->use_count > 1) { + rdev->use_count--; + } } =20 if (ret =3D=3D 0) base-commit: 8af4b4efdac959c02e2a82b4e2fbb8f2c0b57c1e --=20 2.30.2