From nobody Fri Dec 19 07:20:22 2025 Received: from mail-ej1-f53.google.com (mail-ej1-f53.google.com [209.85.218.53]) (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 0B27A1FE479 for ; Wed, 19 Feb 2025 16:08:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.218.53 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739981287; cv=none; b=Zo36zyo7VNrnRyf9Ci2yWrG4TaD8WCiTEi3HtlAlVFq2eOv8pXvFF3EVT75Xf2oUHD7/de12hSqrNI7UAMWUiIafK/CRt9DCk7rfYDhG5kRToLUS61YE8IwM44Tw2leisNr3rRJXSBKbdYdO+n6XuenSoipoawzBgywTv2LbjwQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1739981287; c=relaxed/simple; bh=dbGXzpAY3SGMwzX9P+QBAYN0/ZylTElV2Ah8d0DRt8s=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=saH75ACw+A1h3S9AdSdJ6zt5VR6jApMoHU/V1vpjFYKiXftMoiWDmc8+sSD8xqeEBE99rF832CBpXjukYKyQ+zccR6tOPTu97WCpPClEeOkHw0aZ8+YW+/Xq4v9EcXx1rQJkhPD72QT0FeC33XwkbsQg8z0NKp9PvI8MZyaf0rM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=tuxon.dev; spf=pass smtp.mailfrom=tuxon.dev; dkim=pass (2048-bit key) header.d=tuxon.dev header.i=@tuxon.dev header.b=m3wENOtF; arc=none smtp.client-ip=209.85.218.53 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=tuxon.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=tuxon.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=tuxon.dev header.i=@tuxon.dev header.b="m3wENOtF" Received: by mail-ej1-f53.google.com with SMTP id a640c23a62f3a-aaee2c5ee6eso1061605766b.1 for ; Wed, 19 Feb 2025 08:08:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tuxon.dev; s=google; t=1739981283; x=1740586083; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=ag0ceyd6lnvDklvR5kTzKXppBlKmljc5PPbiPVBs5+k=; b=m3wENOtFKSu+3/5/kD97o6tN2/1TUAwDKRGiY69MblWR7v0BCNQw8/B9eGuNtK3uQy k1t+2jlrt4j3qv6eRC6H9T4Gh8CJx5c4I3oz4dGUFgnFENOt9GDGVhw8JGv2BQe9UkxP FCOj0gemSr9xmpl78AkYOgxyeiizORIViRdkgD5aTcz10gFG5F+SnSYQuFiXpqJhWrzI /K3cAoT9FS/PJiNcmaLfqp3/qwlxrLDiG1kYI1sGpKx+/5PEBIfwZL+07NOEy2CFBqS/ Gp4EZLQzzwtTIs1OSgnABYzXYB7JdUX83Qz2/Snp3HhCzYPlfz4BsFntqXi2TFHZ3ClJ 8kxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1739981283; x=1740586083; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ag0ceyd6lnvDklvR5kTzKXppBlKmljc5PPbiPVBs5+k=; b=AzrLUtQSzW8Lo1e1gh3moyIYsUVoZoAuIRIfFOZU6fu3CemMwJA6+j0HDCXu5l7UIB mmGD2xtTFBpRGzlDT7nRsTPP1lVw5rxICYh9Q9dEK93VnGvjGnveCGIGMyHjuMV862bD WU8gfXoLoh8sHWbynuwWfun4cElak2uNSIDBiwp8RF9LZNorV44sFVBwhcOJFWggm74z 1YooDS1aS+g1rH3yf/iVYQWO5r1uI59QcBj0jvm3v3CmBiZWB5mURQm5IeaYb/n87ixG BTfAmPuuXMPjuRUt/tXeCX26LNsFVexohnLtwUJmyPP32q6lLsVOb1OOmboaeuHQEGyE OroA== X-Forwarded-Encrypted: i=1; AJvYcCXjMtll1dH1ZGdI7vltMrijEVgUYO1nN2WKMa940f7tlzZkJ+VSYAxrImkeC0yWx1N/VIRFMAaf7tbBc4k=@vger.kernel.org X-Gm-Message-State: AOJu0Yx1t8PAZ5iWY1MiLYLow0PwqixyoH8T/MVNEPktSH5KdRWj45f9 Nxsp+8JCug+TOpwmc1YcT1Fl3B3tlmACSo8nJ4r7CEIiopJZgTq1zBuiaOdWC5s= X-Gm-Gg: ASbGncvjv3b1IeANPDTXkarQgYEm1qPpKH7pwl9s6KdfyzEeMwTj33lFT6EagZiTEPw spj2fdYp91qbqwL3fsQnMdGGBcqfH7FJMqXiAyxVtHrM6h8e+YnA3CT3keac9Sa973wB8WzKQaW F+Fc9jh+mTIKyoy5dDvVEm3A/U59fGyY45KbwYxlnMay8O9aqXcsWAxzzgGb3OkJ7k7fXG/Wryg eR5UsvXY0Ejm4lj83CdkFGCNH3MKCnL5BsY45KEcgB8oMLqpQHo+QxZre7ZtXvtrkZRCnIZZkS+ u4wMjl81ZEthRRD/7w0G91EAaIyt8O6jBoFEpBHs29qD X-Google-Smtp-Source: AGHT+IFK03MU51YyMeqqHzkdzf8lpSKD/7+03XHiHb///Jp2uJuRaLvE73Rv2ADy8FE6aZ3jeb4UfQ== X-Received: by 2002:a17:907:7e9d:b0:aab:9430:40e9 with SMTP id a640c23a62f3a-abbcce74170mr348337366b.32.1739981282218; Wed, 19 Feb 2025 08:08:02 -0800 (PST) Received: from claudiu-X670E-Pro-RS.. ([82.78.167.25]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-abbc0d0b882sm327791066b.109.2025.02.19.08.08.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Feb 2025 08:08:01 -0800 (PST) From: Claudiu X-Google-Original-From: Claudiu To: yoshihiro.shimoda.uh@renesas.com, vkoul@kernel.org, kishon@kernel.org, horms+renesas@verge.net.au, fabrizio.castro@bp.renesas.com, robh@kernel.org Cc: claudiu.beznea@tuxon.dev, linux-renesas-soc@vger.kernel.org, linux-phy@lists.infradead.org, linux-kernel@vger.kernel.org, Claudiu Beznea , stable@vger.kernel.org Subject: [PATCH RFT 3/5] phy: renesas: rcar-gen3-usb2: Lock around hardware registers and driver data Date: Wed, 19 Feb 2025 18:07:46 +0200 Message-ID: <20250219160749.1750797-4-claudiu.beznea.uj@bp.renesas.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250219160749.1750797-1-claudiu.beznea.uj@bp.renesas.com> References: <20250219160749.1750797-1-claudiu.beznea.uj@bp.renesas.com> 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" From: Claudiu Beznea The phy-rcar-gen3-usb2 driver exposes four individual PHYs that are requested and configured by PHY users. The struct phy_ops APIs access the same set of registers to configure all PHYs. Additionally, PHY settings can be modified through sysfs or an IRQ handler. While some struct phy_ops APIs are protected by a driver-wide mutex, others rely on individual PHY-specific mutexes. This approach can lead to various issues, including: 1/ the IRQ handler may interrupt PHY settings in progress, racing with hardware configuration protected by a mutex lock 2/ due to msleep(20) in rcar_gen3_init_otg(), while a configuration thread suspends to wait for the delay, another thread may try to configure another PHY (with phy_init() + phy_power_on()); re-running the phy_init() goes to the exact same configuration code, re-running the same hardware configuration on the same set of registers (and bits) which might impact the result of the msleep for the 1st configuring thread 3/ sysfs can configure the hardware (though role_store()) and it can still race with the phy_init()/phy_power_on() APIs calling into the drivers struct phy_ops To address these issues, add a spinlock to protect hardware register access and driver private data structures (e.g., calls to rcar_gen3_is_any_rphy_initialized()). Checking driver-specific data remains necessary as all PHY instances share common settings. With this change, the existing mutex protection is removed and the cleanup.h helpers are used. While at it, to keep the code simpler, do not skip regulator_enable()/regulator_disable() APIs in rcar_gen3_phy_usb2_power_on()/rcar_gen3_phy_usb2_power_off() as the regulators enable/disable operations are reference counted anyway. Fixes: f3b5a8d9b50d ("phy: rcar-gen3-usb2: Add R-Car Gen3 USB2 PHY driver") Cc: stable@vger.kernel.org Signed-off-by: Claudiu Beznea --- drivers/phy/renesas/phy-rcar-gen3-usb2.c | 49 +++++++++++++----------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/drivers/phy/renesas/phy-rcar-gen3-usb2.c b/drivers/phy/renesas= /phy-rcar-gen3-usb2.c index 826c9c4dd4c0..5c0ceba09b67 100644 --- a/drivers/phy/renesas/phy-rcar-gen3-usb2.c +++ b/drivers/phy/renesas/phy-rcar-gen3-usb2.c @@ -9,6 +9,7 @@ * Copyright (C) 2014 Cogent Embedded, Inc. */ =20 +#include #include #include #include @@ -118,7 +119,7 @@ struct rcar_gen3_chan { struct regulator *vbus; struct reset_control *rstc; struct work_struct work; - struct mutex lock; /* protects rphys[...].powered */ + spinlock_t lock; /* protects access to hardware and driver data structure= . */ enum usb_dr_mode dr_mode; u32 obint_enable_bits; bool extcon_host; @@ -348,6 +349,8 @@ static ssize_t role_store(struct device *dev, struct de= vice_attribute *attr, bool is_b_device; enum phy_mode cur_mode, new_mode; =20 + guard(spinlock_irqsave)(&ch->lock); + if (!ch->is_otg_channel || !rcar_gen3_is_any_otg_rphy_initialized(ch)) return -EIO; =20 @@ -415,7 +418,7 @@ static void rcar_gen3_init_otg(struct rcar_gen3_chan *c= h) val =3D readl(usb2_base + USB2_ADPCTRL); writel(val | USB2_ADPCTRL_IDPULLUP, usb2_base + USB2_ADPCTRL); } - msleep(20); + mdelay(20); =20 writel(0xffffffff, usb2_base + USB2_OBINTSTA); writel(ch->obint_enable_bits, usb2_base + USB2_OBINTEN); @@ -436,12 +439,14 @@ static irqreturn_t rcar_gen3_phy_usb2_irq(int irq, vo= id *_ch) if (pm_runtime_suspended(dev)) goto rpm_put; =20 - status =3D readl(usb2_base + USB2_OBINTSTA); - if (status & ch->obint_enable_bits) { - dev_vdbg(dev, "%s: %08x\n", __func__, status); - writel(ch->obint_enable_bits, usb2_base + USB2_OBINTSTA); - rcar_gen3_device_recognition(ch); - ret =3D IRQ_HANDLED; + scoped_guard(spinlock, &ch->lock) { + status =3D readl(usb2_base + USB2_OBINTSTA); + if (status & ch->obint_enable_bits) { + dev_vdbg(dev, "%s: %08x\n", __func__, status); + writel(ch->obint_enable_bits, usb2_base + USB2_OBINTSTA); + rcar_gen3_device_recognition(ch); + ret =3D IRQ_HANDLED; + } } =20 rpm_put: @@ -456,6 +461,8 @@ static int rcar_gen3_phy_usb2_init(struct phy *p) void __iomem *usb2_base =3D channel->base; u32 val; =20 + guard(spinlock_irqsave)(&channel->lock); + /* Initialize USB2 part */ val =3D readl(usb2_base + USB2_INT_ENABLE); val |=3D USB2_INT_ENABLE_UCOM_INTEN | rphy->int_enable_bits; @@ -479,6 +486,8 @@ static int rcar_gen3_phy_usb2_exit(struct phy *p) void __iomem *usb2_base =3D channel->base; u32 val; =20 + guard(spinlock_irqsave)(&channel->lock); + rphy->initialized =3D false; =20 val =3D readl(usb2_base + USB2_INT_ENABLE); @@ -498,16 +507,17 @@ static int rcar_gen3_phy_usb2_power_on(struct phy *p) u32 val; int ret =3D 0; =20 - mutex_lock(&channel->lock); - if (!rcar_gen3_are_all_rphys_power_off(channel)) - goto out; - if (channel->vbus) { ret =3D regulator_enable(channel->vbus); if (ret) - goto out; + return ret; } =20 + guard(spinlock_irqsave)(&channel->lock); + + if (!rcar_gen3_are_all_rphys_power_off(channel)) + goto out; + val =3D readl(usb2_base + USB2_USBCTR); val |=3D USB2_USBCTR_PLL_RST; writel(val, usb2_base + USB2_USBCTR); @@ -517,7 +527,6 @@ static int rcar_gen3_phy_usb2_power_on(struct phy *p) out: /* The powered flag should be set for any other phys anyway */ rphy->powered =3D true; - mutex_unlock(&channel->lock); =20 return 0; } @@ -528,18 +537,12 @@ static int rcar_gen3_phy_usb2_power_off(struct phy *p) struct rcar_gen3_chan *channel =3D rphy->ch; int ret =3D 0; =20 - mutex_lock(&channel->lock); - rphy->powered =3D false; - - if (!rcar_gen3_are_all_rphys_power_off(channel)) - goto out; + scoped_guard(spinlock_irqsave, &channel->lock) + rphy->powered =3D false; =20 if (channel->vbus) ret =3D regulator_disable(channel->vbus); =20 -out: - mutex_unlock(&channel->lock); - return ret; } =20 @@ -750,7 +753,7 @@ static int rcar_gen3_phy_usb2_probe(struct platform_dev= ice *pdev) if (phy_data->no_adp_ctrl) channel->obint_enable_bits =3D USB2_OBINT_IDCHG_EN; =20 - mutex_init(&channel->lock); + spin_lock_init(&channel->lock); for (i =3D 0; i < NUM_OF_PHYS; i++) { channel->rphys[i].phy =3D devm_phy_create(dev, NULL, phy_data->phy_usb2_ops); --=20 2.43.0