From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 18E0A18BBAE; Mon, 4 Aug 2025 00:30:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754267409; cv=none; b=JvtuZyql4zNy4P9ZaMTSxQzzPjPPCOpotv8u/bp/sH9Rt/kfGX44h9VuUNhkg32psjjdqSMTjYjnkRkIyUdxsRg4Y/TOPuowncv6HnoL3QBvIhD+obYLF25fy5rGDPEMktbGhqV9em8YywrTYeRNZ7IXdQq8kzec9h8bmAax2RI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1754267409; c=relaxed/simple; bh=9ZdAOwpzD822lRR4Y0xRjpvvwB7yueTga6TbSxVKsLg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UXhQHctbIjK9tPMpDJwTa0/UMVWnPVoSOuw/umiDof8sIBSV3sT/Ah+iCxoJZjPPC38jBVO/C1B3Fl8LsR9EclaEm/Ns9YE1m75xkcainRjiyiMCrnKAV6/bpeGsK/vy0frOnjv2isvi/3R4XFypSjsNMRMuAdbIAgdf88FE5V4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=L2EUFCqU; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="L2EUFCqU" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 52AA2C4CEEB; Mon, 4 Aug 2025 00:30:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1754267409; bh=9ZdAOwpzD822lRR4Y0xRjpvvwB7yueTga6TbSxVKsLg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L2EUFCqU24VSgIgYHzJhXHKTvFXkyXepve3G5whLSTe1C+HkCXqaorUK5HpS3yzox PDBjkNPneHCCBhSlLW3nudf/LPM98U13uEveZiDISLJ6jR49MXeKp/LxdPXRh16+0G 95lHEgGEbZr9PnCd5yH/HLPqOrei2FMiDMGCYQszSZdZgy58qPSyjwvROjsKN6dJgy pKfZFTv14KErMJ8vt4kP8NQdzZAVU3ofvQFz+sx+aGlp9ICLY5l/aM6vjCT5YY3H9M iDIpcj7wMDxlEfWV1gnQaUm5p454Wh8Tb2acWaxsEafJG6aqeiUyZ9827PfgIrHMk0 wOj4I5hwhAEkg== From: Sasha Levin To: patches@lists.linux.dev, stable@vger.kernel.org Cc: Ulf Hansson , Avri Altman , Ricky Wu , Sasha Levin , nathan@kernel.org, zhoubinbin@loongson.cn, viro@zeniv.linux.org.uk, u.kleine-koenig@baylibre.com Subject: [PATCH AUTOSEL 6.15 55/80] mmc: rtsx_usb_sdmmc: Fix error-path in sd_set_power_mode() Date: Sun, 3 Aug 2025 20:27:22 -0400 Message-Id: <20250804002747.3617039-55-sashal@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250804002747.3617039-1-sashal@kernel.org> References: <20250804002747.3617039-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.15.9 Content-Transfer-Encoding: 8bit From: Ulf Hansson [ Upstream commit 47a255f7d2eabee06cfbf5b1c2379749442fd01d ] In the error path of sd_set_power_mode() we don't update host->power_mode, which could lead to an imbalance of the runtime PM usage count. Fix this by always updating host->power_mode. Reviewed-by: Avri Altman Signed-off-by: Ulf Hansson Acked-by: Ricky Wu Link: https://lore.kernel.org/r/20250610111633.504366-2-ulf.hansson@linaro.org Signed-off-by: Sasha Levin --- LLM Generated explanations, may be completely bogus: **YES** This commit should be backported to stable kernel trees. Here's my extensive analysis: ## Bug Analysis The commit fixes a **runtime PM reference counting bug** in the `sd_set_power_mode()` function. The issue is in the error handling path: ### Original Code Problem: ```c if (power_mode == MMC_POWER_OFF) { err = sd_power_off(host); pm_runtime_put_noidle(sdmmc_dev(host)); } else { pm_runtime_get_noresume(sdmmc_dev(host)); err = sd_power_on(host); } if (!err) host->power_mode = power_mode; // Only updated on success ``` ### The Bug: 1. When transitioning to `MMC_POWER_OFF`, the code calls `pm_runtime_put_noidle()` **regardless** of whether `sd_power_off()` succeeded 2. When transitioning to `MMC_POWER_ON`, the code calls `pm_runtime_get_noresume()` **before** calling `sd_power_on()` 3. However, `host->power_mode` is only updated if there's no error This creates a **runtime PM reference count imbalance** because: - If `sd_power_off()` fails, the runtime PM reference is still decremented, but `host->power_mode` remains at its previous value (likely `MMC_POWER_ON`) - If `sd_power_on()` fails, the runtime PM reference is still incremented, but `host->power_mode` remains at its previous value (likely `MMC_POWER_OFF`) - The early return check `if (power_mode == host->power_mode)` at the beginning of the function will prevent future attempts to fix the power state, leaving the runtime PM count permanently imbalanced ## Why This Is Suitable for Stable: 1. **Fixes a real bug**: Runtime PM reference count imbalances can lead to: - Device staying powered when it should be suspended - Device being suspended when it should be active - Power management failures - Potential system hangs or crashes 2. **Minimal change**: The fix is a simple one-liner that always updates `host->power_mode` regardless of error status 3. **Low regression risk**: - The change doesn't introduce new functionality - It doesn't alter the control flow - It simply ensures state consistency 4. **Clear bug fix**: This is not a feature addition or optimization - it's fixing incorrect error handling that violates runtime PM rules 5. **Affects users**: This bug can manifest in real-world scenarios when SD card power operations fail, particularly with flaky hardware or during system stress 6. **Subsystem contained**: The fix is limited to a single MMC host driver and doesn't affect core kernel infrastructure The commit follows stable kernel rules by being a minimal, targeted fix for a real bug that can affect system stability and power management. drivers/mmc/host/rtsx_usb_sdmmc.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/drivers/mmc/host/rtsx_usb_sdmmc.c b/drivers/mmc/host/rtsx_usb_sdmmc.c index d229c2b83ea9..8c35cb85a9c0 100644 --- a/drivers/mmc/host/rtsx_usb_sdmmc.c +++ b/drivers/mmc/host/rtsx_usb_sdmmc.c @@ -1029,9 +1029,7 @@ static int sd_set_power_mode(struct rtsx_usb_sdmmc *host, err = sd_power_on(host); } - if (!err) - host->power_mode = power_mode; - + host->power_mode = power_mode; return err; } -- 2.39.5