From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id ADEC1EB64DD for ; Thu, 15 Jun 2023 11:37:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343855AbjFOLhC (ORCPT ); Thu, 15 Jun 2023 07:37:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52288 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237737AbjFOLhA (ORCPT ); Thu, 15 Jun 2023 07:37:00 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 05433213B; Thu, 15 Jun 2023 04:37:00 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 8E1D462679; Thu, 15 Jun 2023 11:36:59 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0ADDEC433CB; Thu, 15 Jun 2023 11:36:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1686829019; bh=PsRJJ5ZxMa7cs8GT5ohChX59MmmNPMIucMA3Mt/cmNs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=b+dFyxZFki1qcqGCC/x/ffevhvmqiWYCY5WLWxCOwMPvpKhF5u4xFjHE2bar81a0k W6lHy3CaVdo2bZ65Dg4hsJ1mUw26q9VNahSuIJ4NjK7OA78gY+JHAXvVisBB6EkNRm ZXmVd5KNLMG0MRSnwFuVQPtN8tVO1w+YtK2gsgv7jUv5oawB3sqSfSr7pEBlmRjJVA 6THa5kbhQ/Ky5ecUOP5X61Hi/d89Krus9/iHRNfOeCaNKjVpfavrKBWVHv1zdRceKv SlLSrJz9Hx2yJwPiPO1BtE0UuX0wAi38sNqvxWhT7kwbvjkqZsDoix3Xy0vd9Wrk/B Plah/IwhYI8gA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Hans de Goede , Orson Zhai , Chunyan Zhang , Baolin Wang , Sebastian Reichel , Sasha Levin , sre@kernel.org, linux-pm@vger.kernel.org Subject: [PATCH AUTOSEL 6.3 02/37] power: supply: sc27xx: Fix external_power_changed race Date: Thu, 15 Jun 2023 07:36:19 -0400 Message-Id: <20230615113654.648702-2-sashal@kernel.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230615113654.648702-1-sashal@kernel.org> References: <20230615113654.648702-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.3.8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Hans de Goede [ Upstream commit 4d5c129d6c8993fe96e9ae712141eedcb9ca68c2 ] sc27xx_fgu_external_power_changed() dereferences data->battery, which gets sets in ab8500_btemp_probe() like this: data->battery = devm_power_supply_register(dev, &sc27xx_fgu_desc, &fgu_cfg); As soon as devm_power_supply_register() has called device_add() the external_power_changed callback can get called. So there is a window where sc27xx_fgu_external_power_changed() may get called while data->battery has not been set yet leading to a NULL pointer dereference. Fixing this is easy. The external_power_changed callback gets passed the power_supply which will eventually get stored in data->battery, so sc27xx_fgu_external_power_changed() can simply directly use the passed in psy argument which is always valid. After this change sc27xx_fgu_external_power_changed() is reduced to just "power_supply_changed(psy);" and it has the same prototype. While at it simply replace it with making the external_power_changed callback directly point to power_supply_changed. Cc: Orson Zhai Cc: Chunyan Zhang Signed-off-by: Hans de Goede Reviewed-by: Baolin Wang Signed-off-by: Sebastian Reichel Signed-off-by: Sasha Levin --- drivers/power/supply/sc27xx_fuel_gauge.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/drivers/power/supply/sc27xx_fuel_gauge.c b/drivers/power/supply/sc27xx_fuel_gauge.c index 632977f84b954..bd23c4d9fed43 100644 --- a/drivers/power/supply/sc27xx_fuel_gauge.c +++ b/drivers/power/supply/sc27xx_fuel_gauge.c @@ -733,13 +733,6 @@ static int sc27xx_fgu_set_property(struct power_supply *psy, return ret; } -static void sc27xx_fgu_external_power_changed(struct power_supply *psy) -{ - struct sc27xx_fgu_data *data = power_supply_get_drvdata(psy); - - power_supply_changed(data->battery); -} - static int sc27xx_fgu_property_is_writeable(struct power_supply *psy, enum power_supply_property psp) { @@ -774,7 +767,7 @@ static const struct power_supply_desc sc27xx_fgu_desc = { .num_properties = ARRAY_SIZE(sc27xx_fgu_props), .get_property = sc27xx_fgu_get_property, .set_property = sc27xx_fgu_set_property, - .external_power_changed = sc27xx_fgu_external_power_changed, + .external_power_changed = power_supply_changed, .property_is_writeable = sc27xx_fgu_property_is_writeable, .no_thermal = true, }; -- 2.39.2