Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1363760rwl; Fri, 7 Apr 2023 14:52:16 -0700 (PDT) X-Google-Smtp-Source: AKy350bP7/r2hqq8pOY46Qmt4lLTYEbLHX4gLFLuRMw2LSrT5YBIJWxQ9YKJZbBD8vm1GoCQRMy4 X-Received: by 2002:aa7:da8b:0:b0:502:9a5b:d25 with SMTP id q11-20020aa7da8b000000b005029a5b0d25mr4290219eds.2.1680904336113; Fri, 07 Apr 2023 14:52:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680904336; cv=none; d=google.com; s=arc-20160816; b=vJbvpk5kXVe2g4G4XkkOLlp7L/0v6OE4LCBed9BjkvklQn69KFp4hsoasa6egTl5kC DqqkCdWLe3WXQYeTSL/zzark8QLgXQaH/KsXJpSgH2K3CY4d3DMF3lHPer8kIJ+L/1A2 l/ECaAYc77yIM3OyhAq294Dsf4POftoj8afh2q8QLA1mDulRLoEg01moiIFKPQfnBV5v Nu6ntwn2OF7evgPZaUB2XdW8kxX+u9CDHi21IDlDinPmmSv9D3yUfQwEQkI9m+0agQ0G aBrffcI3bEi2ATz60wA3UrPZ611KBqho7gwSKfj86Z9dD711H2dhpPp2Tg10ejkWN/NH sZHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:user-agent:from :references:in-reply-to:mime-version:dkim-signature; bh=MZgziCzrlnZ1KMQngzHso1b66A2bwnxe6r27PGoDf30=; b=Yw/QYlgwqIZM90/6FQn+YpEGOGRJymDrKfS+pP9GBA4uln5ykQEj3B4CH3xwkw8KK0 HXRD7l9lZd7W7CihZPB0QAxJtLBAuy6SfvxTxvjf6TEf/6U6o7pXSQnhfwwDADlncDpz Pb1iMM5zJrXxWkZN+wmWAwaUI+GF0qi3u8fjE+tfohWGm6JpJcZZneVUwcJvcdPWxRfr CJ2dIwviHSonWyOB3CyKnyWh8gUGTi0+dit4bHDwXtOM0hoKUwetLOhyao/icEWLVT8r g5TWkTS2zRPcRw5Bpedrj3solsu25xg56Pj/Uv5JolCFZo1ieAVLvrTYtnJyI6Og0mI5 K6jA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=M1ElGxSd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ba12-20020a0564021acc00b004fe042c5a6esi3124540edb.453.2023.04.07.14.51.51; Fri, 07 Apr 2023 14:52:16 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=M1ElGxSd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229935AbjDGVq0 (ORCPT + 99 others); Fri, 7 Apr 2023 17:46:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43890 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229437AbjDGVqY (ORCPT ); Fri, 7 Apr 2023 17:46:24 -0400 Received: from mail-lj1-x22e.google.com (mail-lj1-x22e.google.com [IPv6:2a00:1450:4864:20::22e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0005AC178 for ; Fri, 7 Apr 2023 14:46:22 -0700 (PDT) Received: by mail-lj1-x22e.google.com with SMTP id e9so29347663ljq.4 for ; Fri, 07 Apr 2023 14:46:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1680903981; h=cc:to:subject:message-id:date:user-agent:from:references :in-reply-to:mime-version:from:to:cc:subject:date:message-id :reply-to; bh=MZgziCzrlnZ1KMQngzHso1b66A2bwnxe6r27PGoDf30=; b=M1ElGxSdslOWrChiUFsekSQMA8P7KQRV0fcTWd/bFeNsaSyG23XXQH+saSYXouolNo F1xvqZNMgC+qKsKO1kmV7pbZHeBmlWY1owLhjmw7XmxFyOEDeuM+U0KtmmOzXrfTn57k jeT7WMiLF9O8hKv9v2lvPBh1VX5cliB48mI7o= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680903981; h=cc:to:subject:message-id:date:user-agent:from:references :in-reply-to:mime-version:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=MZgziCzrlnZ1KMQngzHso1b66A2bwnxe6r27PGoDf30=; b=rs4cDkuZxm6gNBgPm8QuJUjOoMoLQqZxxyeklDqW/gQmYArmE1MjTKMli1iB2dPaQQ I9Cy5pTDu9iDLWOIiDVRh3+XmBXK6Hxi0lkvaiFJqstbryTA6Q/D/hmw4ks9JaO6uEpd nFx+uY/5TiL+p9D0XM6Sou1yfIFZirTxW/l1gm5efjIEWt7kXAC2xF/ZyjMqm5bBpuJO gSuXLrs4/x/RfWkoFzRmroALLz7vH1Nzd+8vSRHnQZ4LmbxOT2UtURGaNAG4BEfBizsi v71EWF07mrtI+nJiYtpx9XpzDrfdG3ThNdZZejUXh1ztaNOD6ozACbkIzpFpflUkkFs3 BhZA== X-Gm-Message-State: AAQBX9ccAnFunJ9BP8zpO6wqHZHXjMRsIJtz1BevggakWe4QLw2vHz/s 6ZBXqRe7VbatUiC89xaOSZvy3gd64x6u6VTRhJg4JA== X-Received: by 2002:a2e:8006:0:b0:2a6:de0:79af with SMTP id j6-20020a2e8006000000b002a60de079afmr929528ljg.10.1680903981221; Fri, 07 Apr 2023 14:46:21 -0700 (PDT) Received: from 753933720722 named unknown by gmailapi.google.com with HTTPREST; Fri, 7 Apr 2023 14:46:20 -0700 MIME-Version: 1.0 In-Reply-To: <20230329143317.RFC.v2.2.I30d8e1ca10cfbe5403884cdd192253a2e063eb9e@changeid> References: <20230329143317.RFC.v2.1.I4e9d433ea26360c06dd1381d091c82bb1a4ce843@changeid> <20230329143317.RFC.v2.2.I30d8e1ca10cfbe5403884cdd192253a2e063eb9e@changeid> From: Stephen Boyd User-Agent: alot/0.10 Date: Fri, 7 Apr 2023 14:46:20 -0700 Message-ID: Subject: Re: [RFC PATCH v2 2/2] regulator: core: Avoid lockdep reports when resolving supplies To: Douglas Anderson , Mark Brown Cc: Dmitry Osipenko , Lucas Stach , David Collins , David Collins , Liam Girdwood , linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Quoting Douglas Anderson (2023-03-29 14:33:54) > diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c > index 9a13240f3084..08726bc0da9d 100644 > --- a/drivers/regulator/core.c > +++ b/drivers/regulator/core.c > @@ -207,6 +207,78 @@ static void regulator_unlock(struct regulator_dev *rdev) > mutex_unlock(®ulator_nesting_mutex); > } > > +/** > + * regulator_lock_two - lock two regulators > + * @rdev1: first regulator > + * @rdev2: second regulator > + * @ww_ctx: w/w mutex acquire context > + * > + * Locks both rdevs using the regulator_ww_class. > + */ > +static void regulator_lock_two(struct regulator_dev *rdev1, > + struct regulator_dev *rdev2, > + struct ww_acquire_ctx *ww_ctx) > +{ > + struct regulator_dev *tmp; > + int ret; > + > + ww_acquire_init(ww_ctx, ®ulator_ww_class); > + > + /* Try to just grab both of them */ > + ret = regulator_lock_nested(rdev1, ww_ctx); > + WARN_ON(ret); > + ret = regulator_lock_nested(rdev2, ww_ctx); > + if (ret != -EDEADLOCK) { > + WARN_ON(ret); > + goto exit; > + } I think this would be clearer if we had two local variable pointers struct regulator_dev *held, *contended; held = rdev1; contended = rdev2; > + > + while (true) { > + /* > + * Start of loop: rdev1 was locked and rdev2 was contended. > + * Need to unlock rdev1, slowly lock rdev2, then try rdev1 > + * again. > + */ > + regulator_unlock(rdev1); regulator_unlock(held); > + > + ww_mutex_lock_slow(&rdev2->mutex, ww_ctx); > + rdev2->ref_cnt++; > + rdev2->mutex_owner = current; > + ret = regulator_lock_nested(rdev1, ww_ctx); ww_mutex_lock_slow(&contended->mutex, ww_ctx); contended->ref_cnt++; contended->mutex_owner = current; swap(held, contended); ret = regulator_lock_nested(contended, ww_ctx); if (ret != -EDEADLOCK) { > + WARN_ON(ret); > + break; > + } > + } > + > +exit: > + ww_acquire_done(ww_ctx); > +} > + > @@ -1627,8 +1699,8 @@ static int set_machine_constraints(struct regulator_dev *rdev) > > /** > * set_supply - set regulator supply regulator > - * @rdev: regulator name It certainly wasn't the name :) > - * @supply_rdev: supply regulator name > + * @rdev: regulator (locked) > + * @supply_rdev: supply regulator (locked)) > * > * Called by platform initialisation code to set the supply regulator for this > * regulator. This ensures that a regulators supply will also be enabled by the [...] > @@ -2190,7 +2263,9 @@ struct regulator *_regulator_get(struct device *dev, const char *id, > return regulator; > } > > + regulator_lock(rdev); > regulator = create_regulator(rdev, dev, id); > + regulator_unlock(rdev); I'm sad that we're now locking the entire time create_regulator() is called. Can that be avoided? I see that create_regulator() publishes the consumer on the consumer_list, but otherwise I don't think it needs to hold the regulator lock. It goes on to call debugfs code after allocating memory. After this patch, we're going to be holding the lock for that regulator across debugfs APIs. I suspect that may lead to more problems later on because the time we hold the lock is extremely wide now. Of course, we were already holding the child regulator's lock for the supply, because that's what this patch is fixing in regulator_resolve_supply(). I'm just nervous that we're holding the lock for a much wider time now. Maybe we can have create_regulator() return the regulator and add a new function like add_regulator_consumer() that does the list modification? Then we can make create_regulator() do everything without holding a lock and have a very short time where the new function locks two regulator locks and does the linkage.