Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp96651rdd; Mon, 8 Jan 2024 19:46:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IHZNV+p32FqkXwZjgByFOavnIAKbYubSEizO7OwD9UHU5hPMfmRnxfPhme+kANz6t8Vc+kR X-Received: by 2002:a05:6808:3993:b0:3bd:274a:4382 with SMTP id gq19-20020a056808399300b003bd274a4382mr4749394oib.15.1704771967336; Mon, 08 Jan 2024 19:46:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704771967; cv=none; d=google.com; s=arc-20160816; b=pMntgNdXItLwriVDIf3I37jPOCZzHGoABIPlImpOwUA6GPCTMRW0oPo8zpDACkHnpM M1qXGNlma7NMI6btuOqnxhUIh7PIaGhETOhIbENG4PiY52tlrRYrO6PPaJS6Rb5Tu5L6 fIv3OknGrW4+CXxaq1dITvJbyhxEkC6J163WbUALPbFuVy6BWAPCkQM9MuDIczzl+uZU 6/y3t+5HW/vwvR1NsIzGDSo6uBUPYX1XS4YvBbCJh4WFoPz/Plqq7lD1fUywoyW7d+IN fZ0UrkvbDYRTz2noUlSDitLosKwn81RXOtOeMqLfMPYr+JYraDlQ0hEPowDwNk3jRoy6 x94w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=fd7/YRbFPwHwO9y5he5hBNzYN/gDqY/82MzLlZm9KnI=; fh=QnWmdgWnF2GgV/PGFggZL9u84FX78U3MtN5WkeWiK/0=; b=U/CtYOktwQBQCNh4IPP2xubizM1rltlAFKck2hEsdzF6eYiBRKwVJB1OADl1BCvw98 j0Hk3kSFKx15SexwThPy489pJoQgXgpTvS/BR8edxlzAQJstcf2K83kQhi5enB0boDZX uPae3wjBtVRVVxtj+9WVUHLuvkWbP6yfjYmsiLYccomhIkYUCslnEYyx30B3FjXGbSU6 l8th4TYtGsrfM8XG8Q9kdTAHxz3nj6veK31droEMM1PIfpt1g7cU7QIhKwqi9Ke0Cmv5 FohLBEMlBI9EMOoTKAjZjHCP8qi3OtsxCWb8v1oDhzcBoFL77wspQWcdDC9HJdI5cZxK RXJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=YUX6y1tG; spf=pass (google.com: domain of linux-kernel+bounces-20337-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-20337-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id p20-20020a05622a00d400b0042800c9f9b4si1220831qtw.236.2024.01.08.19.46.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Jan 2024 19:46:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-20337-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=YUX6y1tG; spf=pass (google.com: domain of linux-kernel+bounces-20337-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-20337-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 193541C232A9 for ; Tue, 9 Jan 2024 03:46:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 502C753A8; Tue, 9 Jan 2024 03:45:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="YUX6y1tG" Received: from mail-lj1-f169.google.com (mail-lj1-f169.google.com [209.85.208.169]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E23FA4418 for ; Tue, 9 Jan 2024 03:45:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=chromium.org Received: by mail-lj1-f169.google.com with SMTP id 38308e7fff4ca-2cca5d81826so32166471fa.2 for ; Mon, 08 Jan 2024 19:45:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1704771955; x=1705376755; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=fd7/YRbFPwHwO9y5he5hBNzYN/gDqY/82MzLlZm9KnI=; b=YUX6y1tG7xv77dbnHj+tbsPeKpGIO+31YWU1RrmrxfEg41rO0kFYBwQjuYwEH6ApTD OXTteLB8dz+cu2W9Glu88ngzn7jSSIzj5/WQ5lQHBq41kHV0TOvh4rkcI4LqGR5PehIQ ivDfsmlmdHju4c5zpL+ZAlWdL+rkBn1cTL1n0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704771955; x=1705376755; 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=fd7/YRbFPwHwO9y5he5hBNzYN/gDqY/82MzLlZm9KnI=; b=M3sIsSAthrKv/S1VsJG9eDMv8giMg1pusCJTpBK7AJNkwHnbfbQRd7XD2spSf3bnz5 qcDi9kKa0V+HffrIY5A4ZcGxAXsu3I74MLc62CRZabgPXF/Ux1Vkk/9DxSoV7AY4KBzY 3P+m5rSWQPAlTUdQaZeOSUBZTEcMpehRUzWg7IRjphiEsNSJZmTq+4vaGDJbMNJkwzvv g9JTyO+XLrcdsaWTldAQKki5EPDd7TuVWNdSgt3tYiNPlWfZclxWNuqoMYwHbRMj/WHX gsHx3vQxwHHXR5NU1iqAJB3N1kwlNaSleKgrv5ELUY8woKMVOdaKXNDB8VqX6ksC74S4 u/Xw== X-Gm-Message-State: AOJu0YyfXrB/CMFsJ0HQoTeo9Wp8eycuFScgzLxDKPoM6Vi7QyxSga60 vRMNuoUMD7VpWwzljZNMYK4ENGEZh1trIKjpDou2TEbsmNUz2jZFP2SWaBo= X-Received: by 2002:a2e:7003:0:b0:2cd:417d:195e with SMTP id l3-20020a2e7003000000b002cd417d195emr2052291ljc.49.1704771954850; Mon, 08 Jan 2024 19:45:54 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20231219082726.844508-1-wenst@chromium.org> In-Reply-To: From: Chen-Yu Tsai Date: Tue, 9 Jan 2024 11:45:43 +0800 Message-ID: Subject: Re: [PATCH] thermal/core: Correctly free tz->tzp in thermal zone registration error path To: "Rafael J. Wysocki" Cc: Daniel Lezcano , Zhang Rui , Lukasz Luba , linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Dec 19, 2023 at 11:28=E2=80=AFPM Rafael J. Wysocki wrote: > > On Tue, Dec 19, 2023 at 9:27=E2=80=AFAM Chen-Yu Tsai = wrote: > > > > After commit 3d439b1a2ad3 ("thermal/core: Alloc-copy-free the thermal > > zone parameters structure"), the core now copies the thermal zone > > parameters structure, and frees it if an error happens during thermal > > zone device registration, or upon unregistration of the device. > > > > In the error path, if device_register() was called, then `tz` disappear= s > > before kfree(tz->tzp) happens, causing a NULL pointer deference crash. > > > > In my case, the error path was entered from the sbs power supply driver= , > > which through the power supply core registers a thermal zone *without > > trip points* for the battery temperature sensor. This combined with > > setting the default thermal governor to "power allocator", which > > *requires* trip_max, causes the thermal zone registration to error out. > > > > The error path should handle the two cases, one where device_register > > has not happened and the kobj hasn't been reference counted, and vice > > versa where it has. The original commit tried to cover the first case, > > but fails for the second. Fix this by adding kfree(tz->tzp) before > > put_device() to cover the second case, and check if `tz` is still valid > > before calling kfree(tz->tzp) to avoid crashing in the second case. > > > > Fixes: 3d439b1a2ad3 ("thermal/core: Alloc-copy-free the thermal zone pa= rameters structure") > > Signed-off-by: Chen-Yu Tsai > > --- > > This includes the minimal changes to fix the crash. I suppose some othe= r > > things in the thermal core could be reworked: > > - Don't use "power allocator" for thermal zones without trip points > > - Move some of the thermal zone cleanup code into the release function > > > > drivers/thermal/thermal_core.c | 6 +++++- > > 1 file changed, 5 insertions(+), 1 deletion(-) > > > > diff --git a/drivers/thermal/thermal_core.c b/drivers/thermal/thermal_c= ore.c > > index 2415dc50c31d..e47826d82062 100644 > > --- a/drivers/thermal/thermal_core.c > > +++ b/drivers/thermal/thermal_core.c > > @@ -1392,12 +1392,16 @@ thermal_zone_device_register_with_trips(const c= har *type, struct thermal_trip *t > > unregister: > > device_del(&tz->device); > > release_device: > > + /* Free tz->tzp before tz goes away. */ > > + kfree(tz->tzp); > > put_device(&tz->device); > > tz =3D NULL; > > remove_id: > > ida_free(&thermal_tz_ida, id); > > free_tzp: > > - kfree(tz->tzp); > > + /* If we arrived here before device_register() was called. */ > > + if (tz) > > + kfree(tz->tzp); > > free_tz: > > kfree(tz); > > return ERR_PTR(result); > > -- > > Can you please test linux-next from today? The issue addressed by > your patch should be fixed there. Sorry for the very late reply. Yes it does. Thanks. ChenYu