Received: by 2002:a05:7412:d8a:b0:e2:908c:2ebd with SMTP id b10csp448298rdg; Thu, 12 Oct 2023 10:04:15 -0700 (PDT) X-Google-Smtp-Source: AGHT+IExZ+wMePTb6PAV23AqZNIz6u4lPzSy51Yw2CrmLF8BxK83V3z6A46iA0L03tgkCDzNum+o X-Received: by 2002:a17:902:b48b:b0:1c4:4462:f1bd with SMTP id y11-20020a170902b48b00b001c44462f1bdmr19089329plr.35.1697130254685; Thu, 12 Oct 2023 10:04:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1697130254; cv=none; d=google.com; s=arc-20160816; b=lC6M+zi+c9oTeP46VoJ0vtgplwWTQ73Rme2+1EfMUNehuVn4YofndfAx1u0/79UxqY kFVSrYLU0Tt+B6QPkg4prWsUutev/X+ldntJBw+3Xc1zqODLxqpLXGu4aDDMg5sbUr5h HMcyw0/YGLAI0XDuKPBLjjAB1s930xjyqpk8lnZwtTODMRlZ/KWtvOzFhLWTTPMgHs/G OyNcrDkxwAYoOm+DpPDIsppDHVc00R36iQDqdT3b4DlXXWoAzCULY/7mK+1p+vKND5E1 GXt+lfoPc5hAFo/B60OieGWi9lwcUegkXUcG1yOQRKcBTzo3sVGSq4opUjqndLnMek1z uM7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=eAFpZB6C/R9Z7HVePlWiPU/s8tuE0qyIl7iNjFhUV08=; fh=IGJkLmLJMMxHCbrtx5Rf70iFDA+RDRvcQWFob4hJIIM=; b=GHKef1Np2QEtxtBzQXwNkYI+zAPLiC8meQGL6sr8FvtbCYXLuEwlrJzinlKXc4YTCE /ri/XiBt6VOcVM1YvkwTNnV2awNdFIl13RTtX3EWKepOhkejm6SaqSbWwrKXP6BW2GWT 6QE6Y3C2AYqh9nQWUQ8ndIXOXG2tR/qZzlfaXJ9k68dsRABs3EPex5f0O9DDLXxQfK0D 3ktmQkldsnOG/BX0NEEzFX+NYsdoO4unHymeNyu6gKpj24/kK3bl5gwmTxfi+33s8OE5 XTPOXEhSi5UUQxMYURq0rUpkgaXApqbix6HRLK48R883Bon2OziOliOd7npwdh9kCkAb XSgg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id r24-20020a170902be1800b001c9bdaa81e3si2426094pls.633.2023.10.12.10.04.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Oct 2023 10:04:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id A946783C716B; Thu, 12 Oct 2023 10:04:10 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1379555AbjJLRDn convert rfc822-to-8bit (ORCPT + 99 others); Thu, 12 Oct 2023 13:03:43 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1378915AbjJLRDm (ORCPT ); Thu, 12 Oct 2023 13:03:42 -0400 Received: from mail-ot1-f47.google.com (mail-ot1-f47.google.com [209.85.210.47]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 00399C0; Thu, 12 Oct 2023 10:03:40 -0700 (PDT) Received: by mail-ot1-f47.google.com with SMTP id 46e09a7af769-6bc57401cb9so256583a34.0; Thu, 12 Oct 2023 10:03:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697130220; x=1697735020; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WQ+0aHw6sQMmsRhISm2t/oJY7lh7N5E9AbFnFFga21k=; b=JvV+IFeMmEvHin7JS3QYivIRwrYZIiZyCrPHWe/1Fh5j3xmRF5FFCKeM5dgTuHXnp0 q6ZGC7kAlU9jIifPM0P5DCN5qyoTroJzy5oN6jDW9j9Ga5s0XsT1hk0y6eWHzxk5PtOR TkUUyF0Cl3nlW541Mmv/md/xbuqKtFfBNTyiedpwYZGkrVYXHOLX1izozaHN5LiqES/s ELfNdeHIBsX2QapkzTVaGUWjUlxAU19MggRJj0pY3W88AYUwuLOjPat7jA2/WIr0kSjb 3xAFhIOfeLs6fQUhHTO6TPNE2TC1c72Gyudoy2FXu8oMzVcCavpwM89WjnwZfbgT8gNg FqjQ== X-Gm-Message-State: AOJu0YzbSAh1jjhqmyEtkz42xJ/2Gd88+L7BjeHdl1MltVMa5wnfW85W 1VXEY3f/RYGnyAGrx9wfDGYYmiepd88mGBvzf8U= X-Received: by 2002:a4a:df07:0:b0:57b:73f6:6f80 with SMTP id i7-20020a4adf07000000b0057b73f66f80mr24752122oou.0.1697130220076; Thu, 12 Oct 2023 10:03:40 -0700 (PDT) MIME-Version: 1.0 References: <20230916113327.85693-1-bo.ye@mediatek.com> In-Reply-To: From: "Rafael J. Wysocki" Date: Thu, 12 Oct 2023 19:03:28 +0200 Message-ID: Subject: Re: [PATCH] Subject: thermal: Fix potential race condition in suspend/resume To: Daniel Lezcano Cc: Bo Ye , "Rafael J. Wysocki" , Amit Kucheria , Zhang Rui , Matthias Brugger , AngeloGioacchino Del Regno , yugang.wang@mediatek.com, yongdong.zhang@mediatek.com, browse.zhang@mediatek.com, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI, 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 morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Thu, 12 Oct 2023 10:04:10 -0700 (PDT) On Thu, Oct 12, 2023 at 5:39 PM Daniel Lezcano wrote: > > On 16/09/2023 13:33, Bo Ye wrote: > > From: "yugang.wang" > > > > Body: > > This patch fixes a race condition during system resume. It occurs if > > the system is exiting a suspend state and a user is trying to > > register/unregister a thermal zone concurrently. The root cause is > > that both actions access the `thermal_tz_list`. > > I'm not sure the tasks are already thawed during POST_RESTORE, so no > user can unload a driver and then reaching the race window. Yes, they are. > Is that an observed issue? Good question, but the patch looks correct to me. > > In detail: > > > > 1. At PM_POST_SUSPEND during the resume, the system reads all thermal > > zones in `thermal_tz_list`, then resets and updates their > > temperatures. > > 2. When registering/unregistering a thermal zone, the > > `thermal_tz_list` gets manipulated. > > > > These two actions might occur concurrently, causing a race condition. > > To solve this issue, we introduce a mutex lock to protect > > `thermal_tz_list` from being modified while it's being read and > > updated during the resume from suspend. > > > > Kernel oops excerpt related to this fix: > > > > [ 5201.869845] [T316822] pc: [0xffffffeb7d4876f0] mutex_lock+0x34/0x170 > > [ 5201.869856] [T316822] lr: [0xffffffeb7ca98a84] thermal_pm_notify+0xd4/0x26c > > [... cut for brevity ...] > > [ 5201.871061] [T316822] suspend_prepare+0x150/0x470 > > [ 5201.871067] [T316822] enter_state+0x84/0x6f4 > > [ 5201.871076] [T316822] state_store+0x15c/0x1e8 > > > > Change-Id: Ifdbdecba17093f91eab7e36ce04b46d311ca6568 > > Signed-off-by: yugang.wang > > Signed-off-by: Bo Ye > > --- > > drivers/thermal/thermal_core.c | 2 ++ > > 1 file changed, 2 insertions(+) > > > > diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_core.c > > index 8717a3343512..a7a18ed57b6d 100644 > > --- a/drivers/thermal/thermal_core.c > > +++ b/drivers/thermal/thermal_core.c > > @@ -1529,12 +1529,14 @@ static int thermal_pm_notify(struct notifier_block *nb, > > case PM_POST_HIBERNATION: > > case PM_POST_RESTORE: > > case PM_POST_SUSPEND: > > + mutex_lock(&thermal_list_lock); > > atomic_set(&in_suspend, 0); > > list_for_each_entry(tz, &thermal_tz_list, node) { > > thermal_zone_device_init(tz); > > thermal_zone_device_update(tz, > > THERMAL_EVENT_UNSPECIFIED); > > } > > + mutex_unlock(&thermal_list_lock); > > break; > > default: > > break; > > --