Received: by 2002:a05:7412:bbc7:b0:fc:a2b0:25d7 with SMTP id kh7csp2299153rdb; Mon, 5 Feb 2024 02:04:08 -0800 (PST) X-Google-Smtp-Source: AGHT+IEvKpYanfRIQYeFa4MV5hQgAI1nylq8V0anwcWHtxeFNiz7Qj0OP8M9dTBRq3seK2unhg2a X-Received: by 2002:a9d:74d2:0:b0:6dd:e799:1d27 with SMTP id a18-20020a9d74d2000000b006dde7991d27mr9274385otl.26.1707127447816; Mon, 05 Feb 2024 02:04:07 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707127447; cv=pass; d=google.com; s=arc-20160816; b=HEUiP68jivS5Nq3azxiwJEEuuBQnVXV28jNrWHo5E1WEojd8Tb6feOsZD4+VaK3UDF qXE7nDICwbBXVlkQVSmN0IizU6isVsfOelpfFqrne5U3WOeAlV+KdBhjU92N+828Vz7z d4RW0s4hx0d/LRKkmoFIuAPNOymYKcqJoacq0UpLHDoW2e8YBKhUuHHXMgIBxBVxFkz0 DHoA335D6VFhhCagHPOk45USLC6TFr3an9d9Wt9H4SAx62mU3eb8XA90bEygi/gePaiS oNq94AXBVAuNxU09gLH3mYbO+PjNzgobNr+KKaqkjaVP4dQzNxYOtGnWdOX6T9bBzR3Q Auvw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:references:from:to:cc:subject:message-id:date :content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:dkim-signature; bh=7V6z+qCUFyQSPXAeQtn+ZH50TYe1F6hK5wqF3ymIT7Y=; fh=1LPGx4TtJUslmi9k1ysEp72clc+OYuo+i+3d34Tf8SI=; b=r9NUBoQNoh7stubYj8skNYbohz7NRVPGG+eYri6P+LDdA6JEI1XASg3rayD4NnaXF6 yHUgdnVmgIcuv5+WdDLVOd2U3ZmV5AvQLSKNa7ZXPtsogUPo7+ZjW/TTeLJt9HV439TS GB471EhDrjcxVrVIH4f5mJa+SpJQcz5Sd90A9RIomT6cNPQzt7yB0tM/Bfe++2K3ZXMm bhdKgC5eKbPkfCFGTu7ByGaOzrMOBXpw0M06lMBV8RCKAupFu+6n5bLqpUPThmBTZHs0 euw1G/JXz0Bnbt710z84oXyFPXWRyw++fil4uJvRqa3hsi7uHjEgh7d6qiyPNGRYP+Ic OlFQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=GNdn0apP; arc=pass (i=1 spf=pass spfdomain=bootlin.com dkim=pass dkdomain=bootlin.com dmarc=pass fromdomain=bootlin.com); spf=pass (google.com: domain of linux-kernel+bounces-52377-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52377-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com X-Forwarded-Encrypted: i=1; AJvYcCVIM08sUjyU4n1DDg+8+AwWr7NJ1wbNqshnlEbqdmb7ihWF2p+zmIEObZlJVqDe59HXecQYqZdQAnmKKx4Qeec8jT1K0ShcS3f0DrspJw== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id t21-20020a05622a01d500b0042c0b96a355si5977852qtw.275.2024.02.05.02.04.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Feb 2024 02:04:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-52377-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=@bootlin.com header.s=gm1 header.b=GNdn0apP; arc=pass (i=1 spf=pass spfdomain=bootlin.com dkim=pass dkdomain=bootlin.com dmarc=pass fromdomain=bootlin.com); spf=pass (google.com: domain of linux-kernel+bounces-52377-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-52377-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com 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 7D0731C219A4 for ; Mon, 5 Feb 2024 10:04:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2C1CA13AF8; Mon, 5 Feb 2024 10:03:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b="GNdn0apP" Received: from relay4-d.mail.gandi.net (relay4-d.mail.gandi.net [217.70.183.196]) (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 BFB481426D; Mon, 5 Feb 2024 10:03:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.196 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707127436; cv=none; b=IaNMGgxk3Ya3ZlK0Vu/nWVkhJWxXEAeS1fCTzLeTz1rbMSgr8NxlKAJXnADt+oG9d/ftsqsLkoAfWZYlsRxekC2apS5X3oBrP28L6ftwyIpB3hq+awZzHLUjDLullNR2KK0tOvkTP47YggG158Th0BB/j7Ozfmu942FkMth+5dw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707127436; c=relaxed/simple; bh=IT/daI88i9axsf7RKk/UKd8fnOMZuN7sWR6JMAm9d4k=; h=Mime-Version:Content-Type:Date:Message-Id:Subject:Cc:To:From: References:In-Reply-To; b=HAd62Th0tbOvx8BUXGYlDpPl4CT17t2iLwu1R4e/jjKZv2gGaj+4OiBcuMHTjMxSPki/PzmuJzGZ+IOqWVOgYWfzIpJ0Ai/P8cs04ZM/MKXjEY1rpY59BIaXX0HbA7+jzB3xs2YZL4PrO4BBfAvpybJvnCyy4gaH9MNVosxA1kg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com; spf=pass smtp.mailfrom=bootlin.com; dkim=pass (2048-bit key) header.d=bootlin.com header.i=@bootlin.com header.b=GNdn0apP; arc=none smtp.client-ip=217.70.183.196 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=bootlin.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bootlin.com Received: by mail.gandi.net (Postfix) with ESMTPSA id 07126E0010; Mon, 5 Feb 2024 10:03:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1707127425; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7V6z+qCUFyQSPXAeQtn+ZH50TYe1F6hK5wqF3ymIT7Y=; b=GNdn0apP+a6+zq8nHAQPeB4B6Jo9GG5fo4Nx8u0WjY5bh+5stDhzdaJJ3xPGEx0J7g+7vp HWUGhpr6IK7147kxAg6U8Pv0dqEipiBBSo0riY8ZaN5EqaeZvvIEBIRekAhbRdENaVnZFh jkbRYKItksNsUfpZCRxbvC5t/R87IoA8JM45350Fu5oFApey8MNKDrX2qVYFGZK8XwWiQO OpB1NwYeukd50uxlw/SNosto75CXR+i8mnqKEnounJozCBubkRoHENJIFrwJNPvYkxjeH8 RrvEf8dNfEyQ624zf29VrP3zNkfYmK7gGyJgjrVI6pnPJ3xjmpHdG0N2Yln1mA== Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Mon, 05 Feb 2024 11:03:44 +0100 Message-Id: Subject: Re: [PATCH] spi: cadence-qspi: stop calling system-wide PM helpers for runtime PM Cc: "Mark Brown" , "Apurva Nandan" , "Dhruva Gole" , , , "Gregory CLEMENT" , "Vladimir Kondratiev" , "Thomas Petazzoni" , "Tawfik Bayouk" To: "Miquel Raynal" From: =?utf-8?q?Th=C3=A9o_Lebrun?= X-Mailer: aerc 0.15.2 References: <20240202-cdns-qspi-pm-fix-v1-1-3c8feb2bfdd8@bootlin.com> <20240205100312.6f0f40db@xps-13> In-Reply-To: <20240205100312.6f0f40db@xps-13> X-GND-Sasl: theo.lebrun@bootlin.com Hi, On Mon Feb 5, 2024 at 10:03 AM CET, Miquel Raynal wrote: > Hello Th=C3=A9o, > > theo.lebrun@bootlin.com wrote on Fri, 02 Feb 2024 18:29:40 +0100: > > > The ->runtime_suspend() and ->runtime_resume() callbacks are not > > expected to call spi_controller_suspend() and spi_controller_resume(). > > Remove calls to those in the cadence-qspi driver. > >=20 > > Those helpers have two roles currently: > > - They stop/start the queue, including dealing with the kworker. > > - They toggle the SPI controller SPI_CONTROLLER_SUSPENDED flag. It > > requires acquiring ctlr->bus_lock_mutex. > >=20 > > The cadence-qspi ->exec_op() implementation bumps the usage counter at > > its start. It might therefore run our ->runtime_resume() > > implementation. However, ctlr->bus_lock_mutex is acquired by > > spi_mem_exec_op() while ->exec_op() is being called. > >=20 > > Here is a brief call tree highlighting the issue: > >=20 > > spi_mem_exec_op() > > ... > > spi_mem_access_start() > > mutex_lock(&ctlr->bus_lock_mutex) > >=20 > > cqspi_exec_mem_op() > > pm_runtime_resume_and_get() > > cqspi_resume() > > spi_controller_resume() > > mutex_lock(&ctlr->bus_lock_mute= x) > > ... > >=20 > > spi_mem_access_end() > > mutex_unlock(&ctlr->bus_lock_mutex) > > ... > >=20 > > The fatal conclusion of this is a deadlock: we acquire a lock on each > > operation but while running the operation, we might want to runtime > > resume and acquire the same lock. > >=20 > > Anyway, those helpers (spi_controller_{suspend,resume}) are aimed at > > system-wide suspend and resume and should NOT be called at runtime > > suspend & resume. > >=20 > > Side note: the previous implementation had a second issue. It acquired = a > > pointer to both `struct cqspi_st` and `struct spi_controller` using > > dev_get_drvdata(). Neither embed the other. This lead to memory > > corruption that was being hidden inside the big cqspi->f_pdata array on > > my setup. It was working until I tried changing the array side to its > > theorical max of 4, which lead to the discovery of this gnarly bug. > >=20 > > Fixes: 0578a6dbfe75 ("spi: spi-cadence-quadspi: add runtime pm support"= ) > > Fixes: 2087e85bb66e ("spi: cadence-quadspi: fix suspend-resume implemen= tations") > > Your commit log makes total sense but I believe the diff is gonna break > again the suspend to RAM operation. This is only my understanding > right after quickly going through the whole story, so maybe I'm > totally off topic. The current ->runtime_suspend() implementation would indeed (probably) work for suspend-to-RAM if it wasn't for the wrong pointers to cqspi and spi_controller (see side note from commit message). I've not found a moment where `struct cqspi_st` embed `struct spi_controller` at its start, so I do not believe this has ever worked. It might be the result of a mistake while porting a patch from a branch that included other changes. > What happened if I understand the two commits blamed above: > > - There were PM hooks. > - Someone turned them into runtime PM hooks (breaking regular > suspend/resume). > - Someone else added the "missing" suspend/resume logic inside the > runtime PM hooks to fix suspend and resume. > - You are removing this logic because it leads to deadlocks. > > There was likely a misconception of what is expected in both cases > (quick and small power savings vs. full power cycle/loosing the whole > configuration). > > I would propose instead to create two distinct set of functions: > - One for runtime PM > - One for suspend/resume > This way the runtime PM no longer deadlocks and people using > suspend/resume won't get affected? I don't know if your runtime hooks > *will* always be called during a suspend/resume. I hope so, which would > make the split quite easy and without any code duplication. That does indeed sound like the right approach. Runtime hooks can be called from suspend/resume if needs be. Runtime PM then gets disabled at the late stage. I do not believe currently system-wide suspend can be working. spi_controller_{suspend,resume} are being called with a bogus pointer. This makes me ask: should the system-wide suspend/resume part be addressed with this patch or a follow-up? It feels like a separate concern to me. The nice thing is that I have easy access to J7200, which uses the same controller and supports suspend-to-RAM. That should make it a good test setup. Thanks, -- Th=C3=A9o Lebrun, Bootlin Embedded Linux and Kernel engineering https://bootlin.com