Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp1580051ybl; Tue, 3 Dec 2019 09:19:06 -0800 (PST) X-Google-Smtp-Source: APXvYqy1XwcpgLS+sno/QTzcRYvZa7FX3DjU+kll5l+BAfGHBzj2esYf//iuldkIxmEOvJUOY2gh X-Received: by 2002:a9d:6396:: with SMTP id w22mr4087559otk.364.1575393546342; Tue, 03 Dec 2019 09:19:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575393546; cv=none; d=google.com; s=arc-20160816; b=hiGBPaDcFjHQ0ENH0Kz2TrGXlIuxSrROcwhjmHWgZmwd9w279J8dWusbAsHSv2MYjg L/k92QOQTo2pPGmhNloB7/NKV6N5bRjQrxKIZEXouJgQ4JDINM5DV5J0v+oleUWnGWJb Tn4fKTEB68wBWggsOZE1NbO6wPbWFwvphoKQAoWvtHyKN6jNFZKyDlPTfIClqlivDfwd ETosZ1z5uGRC0zHf21XelH4xH6218Bvv83h3VX5ge99MOaDMYOof8xRryd7ZZmv/v2Xl EocBdGBhKk3w1xQ6V+CUBlgwvU29e1QZKCjJS7Da30jBgYV3BfWmGD9/Sew8XJ/6vNpw bzbg== 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:autocrypt:from:references:cc:to:subject:dkim-signature; bh=2kGOYlyefG/izQLdNSUWcwNaTHuNdC1HIARqidaNj9A=; b=L1640VbixvL99tgT+P5PLeG9GuiPQx9jMRXiZL4JX/IHe0RGqt2iCbFnncSyQ1NPQi E92jdPSiJMdgxwRRdxrKwgqnZ0FLA7iLniG/dt4r3KPJ5oCFXpRXbfoCIZDREnKVM7Tm t14zgUfZ/CllIHZTIZXsKJiC3JLWjlDW/fIzlvfSndb6opMdNegLXNtn00TfBnF5Xj7Y KxBSe3EeGMVdlzDQg1nIz0ohdZDEq5XSXU7T7s08TMCpo/xfQBpqpj3NhCnvYnaz9/VC etpxdmaMfGvl+RVt3PUEKgjSvEO0QEMCaXOrj7mhbOh8JT9ko2A7DJECC/rU80frMv1v 0SeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=boyfZ+PK; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f16si1661080otp.198.2019.12.03.09.18.53; Tue, 03 Dec 2019 09:19:06 -0800 (PST) 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; dkim=pass header.i=@linaro.org header.s=google header.b=boyfZ+PK; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727004AbfLCRQB (ORCPT + 99 others); Tue, 3 Dec 2019 12:16:01 -0500 Received: from mail-wr1-f65.google.com ([209.85.221.65]:43841 "EHLO mail-wr1-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726822AbfLCRQB (ORCPT ); Tue, 3 Dec 2019 12:16:01 -0500 Received: by mail-wr1-f65.google.com with SMTP id d16so28511wre.10 for ; Tue, 03 Dec 2019 09:15:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=subject:to:cc:references:from:autocrypt:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=2kGOYlyefG/izQLdNSUWcwNaTHuNdC1HIARqidaNj9A=; b=boyfZ+PKhHVqhkFL8qNRre0PYLPtdfZD3ljOxOD2BfzPbKg8IpytJpbRbI1Fgb1V5v gCeD31m2pdxjueZ0l5W2nYSxl9bwklNFa5TRzxX0IQsPbhBjkCl0pMa/WtZcfwv/Mtet y9DOsPYcgMGD+iSp7t4f6mW/84zF4ZFx0xhO0PophHN2JOgqPXaoIR5Hv4mMbZ8iRLoq dr6lYpPxiZY7GQZPevS4C+Mj/7ufu/NaL6XjaAmXuUdrmj1isYIStJUNlbhsjv7a70IS NEIVGGG1SG2zeg+yYko1tBfEgjMkmXHkRgMtsmbXtMqyAblEXXV9KOLI3H3kc7fQNA3W 46DA== 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:autocrypt :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=2kGOYlyefG/izQLdNSUWcwNaTHuNdC1HIARqidaNj9A=; b=YtKe5zIcDSzSVHuia4ZDe9SUxqxJbj3TEF72N+hBDtGmyhfTs9tk8XNNxrE8OkGBQN eP+3YkhUxorMnnJ5g4o+iB3J0ubFLzsUbPts0WDUloi/hZAo41pZNTnTHAwkvuysNd0u wiab31HZJdCRqh2ULnCa3Q7Q1wYggyp6Z13+kC+2bs3B+lH+iym+r+cN7QSgB0mk7ub8 3h/5cb914Zx/BJCwV5iPp1QrQQ3aSlI0qMaEOtt6+7Y14Org2f1qlbbWOOomvNzo1coL RAiXuUIW87mSiYplZPBAdq4d0jk67k4FYfYn8Q/aOLh3v7sFaLye5VvGNL2h+y71vJy4 VNtQ== X-Gm-Message-State: APjAAAWPLFn43TaU30zjjDU7koNVVkV5RH50BzMGAEzM3Ha9JHfwcxfQ UOtVQJEzcZjkNb0v3CTu5RuQuaWwD6E= X-Received: by 2002:a05:6000:1241:: with SMTP id j1mr6812625wrx.26.1575393357375; Tue, 03 Dec 2019 09:15:57 -0800 (PST) Received: from ?IPv6:2a01:e34:ed2f:f020:b5a4:1c65:b54:363a? ([2a01:e34:ed2f:f020:b5a4:1c65:b54:363a]) by smtp.googlemail.com with ESMTPSA id b17sm4274624wrp.49.2019.12.03.09.15.55 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 03 Dec 2019 09:15:56 -0800 (PST) Subject: Re: [PATCH v2] thermal: Fix deadlock in thermal thermal_zone_device_check To: Wei Wang Cc: wei.vince.wang@gmail.com, Zhang Rui , Eduardo Valentin , Amit Kucheria , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org References: <20191112203154.101534-1-wvw@google.com> <20191112204223.115589-1-wvw@google.com> From: Daniel Lezcano Autocrypt: addr=daniel.lezcano@linaro.org; prefer-encrypt=mutual; keydata= xsFNBFv/yykBEADDdW8RZu7iZILSf3zxq5y8YdaeyZjI/MaqgnvG/c3WjFaunoTMspeusiFE sXvtg3ehTOoyD0oFjKkHaia1Zpa1m/gnNdT/WvTveLfGA1gH+yGes2Sr53Ht8hWYZFYMZc8V 2pbSKh8wepq4g8r5YI1XUy9YbcTdj5mVrTklyGWA49NOeJz2QbfytMT3DJmk40LqwK6CCSU0 9Ed8n0a+vevmQoRZJEd3Y1qXn2XHys0F6OHCC+VLENqNNZXdZE9E+b3FFW0lk49oLTzLRNIq 0wHeR1H54RffhLQAor2+4kSSu8mW5qB0n5Eb/zXJZZ/bRiXmT8kNg85UdYhvf03ZAsp3qxcr xMfMsC7m3+ADOtW90rNNLZnRvjhsYNrGIKH8Ub0UKXFXibHbafSuq7RqyRQzt01Ud8CAtq+w P9EftUysLtovGpLSpGDO5zQ++4ZGVygdYFr318aGDqCljKAKZ9hYgRimPBToDedho1S1uE6F 6YiBFnI3ry9+/KUnEP6L8Sfezwy7fp2JUNkUr41QF76nz43tl7oersrLxHzj2dYfWUAZWXva wW4IKF5sOPFMMgxoOJovSWqwh1b7hqI+nDlD3mmVMd20VyE9W7AgTIsvDxWUnMPvww5iExlY eIC0Wj9K4UqSYBOHcUPrVOKTc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18:15:55 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.1 MIME-Version: 1.0 In-Reply-To: <20191112204223.115589-1-wvw@google.com> 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 On 12/11/2019 21:42, Wei Wang wrote: > commit [2] changed cancel_delayed_work to cancel_delayed_work_sync to > avoid a use-after-free issue. However, cancel_delayed_work_sync could be > called insides the WQ causing deadlock [1]. > > [1] > [54109.642398] c0 1162 kworker/u17:1 D 0 11030 2 0x00000000 > [54109.642437] c0 1162 Workqueue: thermal_passive_wq thermal_zone_device_check > [54109.642447] c0 1162 Call trace: > [54109.642456] c0 1162 __switch_to+0x138/0x158 > [54109.642467] c0 1162 __schedule+0xba4/0x1434 > [54109.642480] c0 1162 schedule_timeout+0xa0/0xb28 > [54109.642492] c0 1162 wait_for_common+0x138/0x2e8 > [54109.642511] c0 1162 flush_work+0x348/0x40c > [54109.642522] c0 1162 __cancel_work_timer+0x180/0x218 > [54109.642544] c0 1162 handle_thermal_trip+0x2c4/0x5a4 > [54109.642553] c0 1162 thermal_zone_device_update+0x1b4/0x25c > [54109.642563] c0 1162 thermal_zone_device_check+0x18/0x24 > [54109.642574] c0 1162 process_one_work+0x3cc/0x69c > [54109.642583] c0 1162 worker_thread+0x49c/0x7c0 > [54109.642593] c0 1162 kthread+0x17c/0x1b0 > [54109.642602] c0 1162 ret_from_fork+0x10/0x18 > [54109.643051] c0 1162 kworker/u17:2 D 0 16245 2 0x00000000 > [54109.643067] c0 1162 Workqueue: thermal_passive_wq thermal_zone_device_check > [54109.643077] c0 1162 Call trace: > [54109.643085] c0 1162 __switch_to+0x138/0x158 > [54109.643095] c0 1162 __schedule+0xba4/0x1434 > [54109.643104] c0 1162 schedule_timeout+0xa0/0xb28 > [54109.643114] c0 1162 wait_for_common+0x138/0x2e8 > [54109.643122] c0 1162 flush_work+0x348/0x40c > [54109.643131] c0 1162 __cancel_work_timer+0x180/0x218 > [54109.643141] c0 1162 handle_thermal_trip+0x2c4/0x5a4 > [54109.643150] c0 1162 thermal_zone_device_update+0x1b4/0x25c > [54109.643159] c0 1162 thermal_zone_device_check+0x18/0x24 > [54109.643167] c0 1162 process_one_work+0x3cc/0x69c > [54109.643177] c0 1162 worker_thread+0x49c/0x7c0 > [54109.643186] c0 1162 kthread+0x17c/0x1b0 > [54109.643195] c0 1162 ret_from_fork+0x10/0x18 > [54109.644500] c0 1162 cat D 0 7766 1 0x00000001 > [54109.644515] c0 1162 Call trace: > [54109.644524] c0 1162 __switch_to+0x138/0x158 > [54109.644536] c0 1162 __schedule+0xba4/0x1434 > [54109.644546] c0 1162 schedule_preempt_disabled+0x80/0xb0 > [54109.644555] c0 1162 __mutex_lock+0x3a8/0x7f0 > [54109.644563] c0 1162 __mutex_lock_slowpath+0x14/0x20 > [54109.644575] c0 1162 thermal_zone_get_temp+0x84/0x360 > [54109.644586] c0 1162 temp_show+0x30/0x78 > [54109.644609] c0 1162 dev_attr_show+0x5c/0xf0 > [54109.644628] c0 1162 sysfs_kf_seq_show+0xcc/0x1a4 > [54109.644636] c0 1162 kernfs_seq_show+0x48/0x88 > [54109.644656] c0 1162 seq_read+0x1f4/0x73c > [54109.644664] c0 1162 kernfs_fop_read+0x84/0x318 > [54109.644683] c0 1162 __vfs_read+0x50/0x1bc > [54109.644692] c0 1162 vfs_read+0xa4/0x140 > [54109.644701] c0 1162 SyS_read+0xbc/0x144 > [54109.644708] c0 1162 el0_svc_naked+0x34/0x38 > [54109.845800] c0 1162 D 720.000s 1->7766->7766 cat [panic] > > Fixes commit 1851799e1d29 ("thermal: Fix use-after-free when > unregistering thermal zone device") [2] It does not fix the problem actually. It is preferable to revert the commit 1851799e1d29. The cancel delayed work sync takes the mutex while a cat /sys/class/thermal/thermal_zone0/temp happens which takes the mutex also and at the same moment a thermal_zone_device_update() call is done in the workqueue context. This one blocks because cancel_delayed_work_sync is owning the lock which in turn waits for the work to end. IMO, that deserves a deeper investigation with the mutex. Probably the fix would be to refcount the thermal zone device and see if we can get rid of the mutex in some places. If the refcounting is used, the function thermal_zone_device_unregister() will be called with the guarantee nobody will use it and the mutex can be removed. > Signed-off-by: Wei Wang > --- > drivers/thermal/thermal_core.c | 4 ++-- > 1 file changed, 2 insertions(+), 2 deletions(-) > > diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c > index d4481cc8958f..c28271817e43 100644 > --- a/drivers/thermal/thermal_core.c > +++ b/drivers/thermal/thermal_core.c > @@ -304,7 +304,7 @@ static void thermal_zone_device_set_polling(struct thermal_zone_device *tz, > &tz->poll_queue, > msecs_to_jiffies(delay)); > else > - cancel_delayed_work_sync(&tz->poll_queue); > + cancel_delayed_work(&tz->poll_queue); > } > > static void monitor_thermal_zone(struct thermal_zone_device *tz) > @@ -1414,7 +1414,7 @@ void thermal_zone_device_unregister(struct thermal_zone_device *tz) > > mutex_unlock(&thermal_list_lock); > > - thermal_zone_device_set_polling(tz, 0); > + cancel_delayed_work_sync(&tz->poll_queue); > > thermal_set_governor(tz, NULL); > > -- Linaro.org │ Open source software for ARM SoCs Follow Linaro: Facebook | Twitter | Blog