Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp4584149pxa; Mon, 10 Aug 2020 12:45:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx7QkxsUHJAJZk4+HqX00OAhx/G7rz8TUFjJVsNs26AKTnVDxMDBvVJ9OuMNmutSYY2sQqq X-Received: by 2002:a05:6402:896:: with SMTP id e22mr23460916edy.210.1597088712725; Mon, 10 Aug 2020 12:45:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597088712; cv=none; d=google.com; s=arc-20160816; b=b3OkK2BEC/3qlCUbM5/r4Kn7WDqA+vJ6zb2L62+rvSSdXfwglB4gIcfdaf5F8Z8phy 7H9A+62vJk8fgUe5bYTsBdnxFdAMvaZ4VnRCPwGx31SfSOxUV4Ra4PDtbcXeisZNHrPw AeeKW/OB3nDpNt0J3KLnidunpfrT1evqYj9xRNGMOAA5jP0UNkntQ6uHdO2lP35BUZRI xhCP3bZ+EtL1vN+ct5ptif0Uc9Rg9i6SmAtx121/mU9zXk5rx9W/4jg5lUnTRTQBlSxn s+VUmTrwXV5+fSs/tjweZx3IAAKQJOyjJA+3IXRy0zgbulV+ayAW8ccYGbp8YipR63c+ eeuA== 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 :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=t3MbX65KIUWMPLc7RGhWJAsNuiP2t/VlQvqq1Z62b84=; b=yrKbMlm8kjWW++hv3tpfXhxeTCkCVBIONbhnGSHfrxw5NgbWM/4n8KRtYoCePS3alf i2ecxFM41QKYgUK9uuSd3sCBxmjM2qGbIGt8N0usfOVwvSGUaw8a8M94Ul6dALaKzul+ uZfzNH/YeUYxEAJPP9hVbas29K+RfRr8SzwHSxM5RFEwHxzXfn9DsZs46wPeBKAbI7RT VY4saf8LR22pWViUgl+EeQviHzQ9wtjl4RXuXEu8cR0DSa8Cbrnkr2NpEA1mAav9zdG7 m8ZYIB6WHAzedBdWCrJ5siZP68d2RzUPlbWXfZLjzUmPC/w8fmXGDz3MbXvjIdB+Nirl TKjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b="n6E7+/kr"; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p19si3283910ejf.476.2020.08.10.12.44.49; Mon, 10 Aug 2020 12:45:12 -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=@gmail.com header.s=20161025 header.b="n6E7+/kr"; 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; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728491AbgHJTl7 (ORCPT + 99 others); Mon, 10 Aug 2020 15:41:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46858 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728253AbgHJTl6 (ORCPT ); Mon, 10 Aug 2020 15:41:58 -0400 Received: from mail-lj1-x235.google.com (mail-lj1-x235.google.com [IPv6:2a00:1450:4864:20::235]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 98BDEC061756 for ; Mon, 10 Aug 2020 12:41:58 -0700 (PDT) Received: by mail-lj1-x235.google.com with SMTP id i10so10943132ljn.2 for ; Mon, 10 Aug 2020 12:41:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=t3MbX65KIUWMPLc7RGhWJAsNuiP2t/VlQvqq1Z62b84=; b=n6E7+/krC3iwvFn7bleMh855o86woO8XEPcDKwgjXoBCb29MYt2LjwOGWwb/CpckH4 2lCuILQcT9v16oxREuJaamyc6eUhEDIYAA3tuQ0JeKjDQvBTBtU54ZNfBpRudaaq8yRK UO3i+pG7losqM5/AW+n924V/PatY4TY3X86ZK4/1RmbrmuwDXkPDVmvnnP1bPPXhN2yV Tk7HTZutCu2/Zxl/tIhNmsEdOnH0TA9mDJcz/oOElYv2bhCJ9qh2JtOMMVjSR5ZkrOTK NqTvHZU7hk2LQ2wfQrdOgfwkWmaVzbjGXgsX2PXJ1a8X40sQO1yggXSnKGIk7O7cEqh/ 6jBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=t3MbX65KIUWMPLc7RGhWJAsNuiP2t/VlQvqq1Z62b84=; b=HLwvRcduhlxMkiEamGdiVePY9TCTRkinoOCkce2Lj6u4GOwwEWkjBmqcPYUhwIAjMz VDPcKgouewMbnEGd4deWvJqy8CYWwR1rjlx5mSr2/W6N9Ifx3jRTStOJ9O7uLJezmVDK jS9fHbDtpDfFBVEWAHvdN3ZcHr+i9zb7HN7ZUdez1l6yUgr/ZzL7GIkIYcbV0MYXpxjR ZaBKM1x7AygZvKCfLiERWqHPJ9MSaX+gHODf0m6TpSCCj78Uk/6Kskvr0i4to5cpNROV 5DU1r/vPo9FDUVnyNAYua7pCJO4TzeO52Qc1lVudFAU32VDaCcVb7HWKd60Lu5z01xyy 1v1g== X-Gm-Message-State: AOAM532pWgjptExqWaIZrZpEvmlLWMJIJXx8bhYjHdWPhpOcrG8Ti9EO LRen71/Q9ncTNl7wBnp2j5elGjHe X-Received: by 2002:a2e:5811:: with SMTP id m17mr1321925ljb.96.1597088516475; Mon, 10 Aug 2020 12:41:56 -0700 (PDT) Received: from [192.168.2.145] (109-252-170-211.dynamic.spd-mgts.ru. [109.252.170.211]) by smtp.googlemail.com with ESMTPSA id m64sm12006833lfd.0.2020.08.10.12.41.55 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 10 Aug 2020 12:41:55 -0700 (PDT) Subject: Re: regulator: deadlock vs memory reclaim To: =?UTF-8?B?TWljaGHFgiBNaXJvc8WCYXc=?= , Mark Brown Cc: Liam Girdwood , linux-kernel@vger.kernel.org References: <20200809222537.GA5522@qmqm.qmqm.pl> <20200810153928.GD6438@sirena.org.uk> <20200810160936.GA1100@qmqm.qmqm.pl> <20200810173136.GF6438@sirena.org.uk> <20200810192547.GB26750@qmqm.qmqm.pl> From: Dmitry Osipenko Message-ID: <08f030a2-3a6f-3ab4-1855-3016884db79d@gmail.com> Date: Mon, 10 Aug 2020 22:41:54 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20200810192547.GB26750@qmqm.qmqm.pl> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 10.08.2020 22:25, Michał Mirosław пишет: >>>>> regulator_lock_dependent() starts by taking regulator_list_mutex, The >>>>> same mutex covers eg. regulator initialization, including memory allocations >>>>> that happen there. This will deadlock when you have filesystem on eg. eMMC >>>>> (which uses a regulator to control module voltages) and you register >>>>> a new regulator (hotplug a device?) when under memory pressure. >>>> OK, that's very much a corner case, it only applies in the case of >>>> coupled regulators. The most obvious thing here would be to move the >>>> allocations on registration out of the locked region, we really only >>>> need this in the regulator_find_coupler() call I think. If the >>>> regulator isn't coupled we don't need to take the lock at all. >>> Currently, regulator_lock_dependent() is called by eg. regulator_enable() and >>> regulator_get_voltage(), so actually any regulator can deadlock this way. >> The initialization cases that are the trigger are only done for coupled >> regulators though AFAICT, otherwise we're not doing allocations with the >> lock held and should be able to progress. > > I caught a few lockdep complaints that suggest otherwise, but I'm still > looking into that. The problem looks obvious to me. The regulator_init_coupling() is protected with the list_mutex, the regulator_lock_dependent() also protected with the list_mutex. Hence if offending reclaim happens from init_coupling(), then there is a lockup. 1. mutex_lock(®ulator_list_mutex); 2. regulator_init_coupling() 3. kzalloc() 4. reclaim ... 5. regulator_get_voltage() 6. regulator_lock_dependent() 7. mutex_lock(®ulator_list_mutex); It should be enough just to keep the regulator_find_coupler() under lock, or even completely remove the locking around init_coupling(). I think it should be better to keep the find_coupler() protected. Michał, does this fix yours problem? --- >8 --- diff --git a/drivers/regulator/core.c b/drivers/regulator/core.c index 75ff7c563c5d..513f95c6f837 100644 --- a/drivers/regulator/core.c +++ b/drivers/regulator/core.c @@ -5040,7 +5040,10 @@ static int regulator_init_coupling(struct regulator_dev *rdev) if (!of_check_coupling_data(rdev)) return -EPERM; + mutex_lock(®ulator_list_mutex); rdev->coupling_desc.coupler = regulator_find_coupler(rdev); + mutex_unlock(®ulator_list_mutex); + if (IS_ERR(rdev->coupling_desc.coupler)) { err = PTR_ERR(rdev->coupling_desc.coupler); rdev_err(rdev, "failed to get coupler: %d\n", err); @@ -5248,9 +5251,7 @@ regulator_register(const struct regulator_desc *regulator_desc, if (ret < 0) goto wash; - mutex_lock(®ulator_list_mutex); ret = regulator_init_coupling(rdev); - mutex_unlock(®ulator_list_mutex); if (ret < 0) goto wash; --- >8 ---