From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.48 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794846876; b=ey+kxtV37I6Hu4EYh6jFgKOwIX11WM78xC6TrBMsUD9B9a8csF2AjaYiKVW19Q8eyd38 lrTBXpZt5eEbeWH/Z3I2pFVnEASoacMXzZaRvyQMQspckhukJ4s7K39eHv0OBqFydKlon QvMxlNIwWdn1AHotoyPLFJJrQcNHLCf7ZoMPMEMhMUc0+qcj4oLVC6U9UttqOFjxS0Caf 7nB2Fq8aSIr2+dxOm0ubU2qCqTfIh4quiemyBVqEBLrYPgoIYJDW9OEnYHolfBhiQ7L8m oopkBbTwt+sOXNV5a1haZCV5d+yR7CsVX83DN49J5H5x9J+DZ2naaRap1gdNdEoqACQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794846876; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=C0jscx5DDVxF6lxNWbtWfTXvaQUi2T5SH8k3EmxiaYE=; b=n6emMun1tUpv/KbXMs0+qawknkYpDmUoQepgh0CaaIQXCH4XdR3Rd6mL7oJy3DIecOdZ Lw+x8RQdYG32fCRLsU33tyqFcspqIli6dWnm/zmNKhbwz56kg5EJ9lmNqxJOl1hqzBrWJ ICUu9owYyBNHHb1I6KhqugMYHWu+3e06bB2L8/uoR+bmjstY81ZrWUkHUWUFb+VZVaByg 3IVzzXXqW7YY91cXQMT7/95Ro9C7QEGemxbzHWa9QhTuAns3rF81MSy1yZp3k8QAoNy4l 2s3637ozyRoimr1amU1ivkq40DeNOmj4ktGU/nqFAUBn/9k1WquB2NIEStO+dpglfQg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Iv125PjC; arc=none smtp.remote-ip=209.85.221.48 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 360A360A260B for ; Mon, 16 Nov 2026 16:34:35 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 360A360A260B Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Iv125PjC Received: by mail-wr1-f48.google.com with SMTP id ffacd0b85a97d-49465892f7bso3039370f8f.1 for ; Mon, 16 Nov 2026 08:34:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1794846874; x=1795451674; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to:content-type; bh=C0jscx5DDVxF6lxNWbtWfTXvaQUi2T5SH8k3EmxiaYE=; b=Iv125PjCHcfaUs4zTX7yG/UQWTh75tR4MSfoDdDBQanb/vZO8y9DJAYMBcWPFm14zm MSjZk+MXCw8CgfgV/nfrFIKJqZVYt/8MdDpJ7Crr2lHSZFalNHMj+N0cP8/Vn3SdL1Ss EPBR4zwMyHMJ1r1xugfIZjGPLQU32FHF+sY0ShCxkDrn1Bd/Fmk3ZJUS56pWVCUdKwqB gwoW5DYwhiixWQhDM7iF6FhNZDi8V6wFcrWRgPIh3rl4Al/o77RNY6lD4pe+8X4QFy4u HIzpR/6J8FYus92XBNSCNQ/KVSZDKG4MLmEkqiFKX1Xcr+bWh9ZVpcnLR9o5KfUh/C5Y r8kw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794846874; x=1795451674; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to:content-type; bh=C0jscx5DDVxF6lxNWbtWfTXvaQUi2T5SH8k3EmxiaYE=; b=Qr6HPjNRxBZ/nP2n3IoQwlClo3goxtBfVoZJAujRWqcEh2qLSDocxPV1eZq2jwjPLN e9oqA9N+LO+HQAjODLwKn7seWHDuxeEPQu0MJc3QTWAMOMJUiRAbDYTSu6bZ48/HFqhR hfYsclCD3ZOycEtgzeEvf4rx2c+Hz03WvKFi3UytfifQI9vO/1H6Yo7n/3RnCjeFzg5A w9MFsMqZFXbqmDqMW5r5UtZa1/irCBGj0LJHZofgwV9zsV1F/pAw1AVWEh4RQO+xS1BF p+Su4K/P9hNDueXkmvjqJwbP1UEtwile6gP/5CjY5HIDw1KfrXPrxJWy6XAyJw4Z5dDX tHSQ== X-Forwarded-Encrypted: i=1; AL+hErGxA6OthloYo7sbuls3a1pk4pYLa7FPY0sayuKw3lmY5q7gdlIbT/d9ALhZtG+pkKMK7cCNP/U=@vger.kernel.org X-Gm-Message-State: APuU4Z+KTD05hwFdPK7Vgo71KwJt+yqEiKDYhiPAcHL3BZVIb7qXcdnF jmr4sPA8vg6IhfuCfVVTlxhjoimjhkv2l1dxe8ARc68l2WQzcRGFM1Nb X-Gm-Gg: AZFdTZdYbfg5Kg3bTn9V+I0zJSekFwjDIQrn0CVVaupPidIH+lj85+riHOq3y8S059F pR+w+BP4lUgX1VxGIlYykl56boTB72R+c4AbgH7JtR0VqDdzxsiCv9XhoOFsA47Fg3QI7egEyt/ VL4gTkU7TyJo35+BdQwhfQCNJRA2YdeBEsOt2eXx+kqWUyFPG4XYhfx29k2patSdeBxqRnVUGIB UXz2maJDdQPESpfyI4DuZJiRke0WVRMgTvLvohDSURS4ab4tYEs2cgZ3a0fBtjKuC5agf7takbe A5U/6doJnf/i3jfrSOKLPYO3sw5AxGFuzkRYnEseUMOix23Oge9k6CmWvCKxE0hMuhHMuZMmPVs 9mdm0ZXiumbZBm78OMy0sLcERHRoQjPZEmvvnDGc3Ks8jwXKK6oHGoKydDj6R9ppQwaJ2vP+91J kr4c0C2pfDWE3U/MLcTTg8gTsUlYpvF5RYM/qsBhnOw4jnr7JY7tmJwUt+nFFC1dbq2IPQ6Fjym TJ3oenVGnvLh8n6YDyWWTVv1mUCs2NkDig4telK2+CVFXzyEonUklAc+KJsznWt7P6ZaFdUQRes 31i4ObTnh6WwHEj8Fk+PoueRPRbHG0v7P9mkMI1+tIltsvrlpKdpkscEAQTNSbek+u8Wl19IBBg Wia/XuDSrGYJ0SeDxU1Td1S9MdIdxhb5GVQ== X-Received: by 2002:a05:6000:4204:b0:4a3:522:244b with SMTP id ffacd0b85a97d-4a305222665mr1439000f8f.11.1794846873561; Mon, 16 Nov 2026 08:34:33 -0800 (PST) Received: from Ansuel-XPS24.localdomain (93-34-92-103.ip49.fastwebnet.it. [93.34.92.103]) by smtp.googlemail.com with ESMTPSA id ffacd0b85a97d-49c5d223a29sm23840506f8f.8.2026.11.16.08.34.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 Nov 2026 08:34:33 -0800 (PST) From: Christian Marangi To: Maxime Chevallier , Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Simon Horman , Jonathan Corbet , Shuah Khan , Randy Dunlap , Christian Marangi , Lorenzo Bianconi , Heiner Kallweit , Russell King , Philipp Zabel , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , "Russell King (Oracle)" , netdev@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, llvm@lists.linux.dev Subject: [PATCH net-next v21 06/15] net: phylink: make phylink_validate state locked Date: Mon, 16 Nov 2026 17:33:49 +0100 Message-ID: <20261116163412.551438-7-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261116163412.551438-1-ansuelsmth@gmail.com> References: <20261116163412.551438-1-ansuelsmth@gmail.com> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit In preparation for support for PCS release/late attach, rework the phylink_validate functions to be locked under the state mutex. With the introduction of a notifier that can be called concurrently while a phylink_create is called or an interface mode validation is being run, on recalculating the supported interface and capabilities, an inconsistency can be introduced with 2 validation run at the same time. To handle this, make the phylink_validate lock under the phylink state mutex, add lockless variant for phylink_validate_mask and phylink_validate and add lockdep check to make sure the mutex is held in the inner validate functions. Function that already run phylink_validate under state mutex, are updated to use the lockless variant. Special care was needed for phy_get_rate_matching that required a lockless variant. This is introduced by adding an internal phy header. Signed-off-by: Christian Marangi --- drivers/net/phy/phy-internal.h | 22 ++++++++++++ drivers/net/phy/phy.c | 11 +++--- drivers/net/phy/phylink.c | 62 ++++++++++++++++++++++++++++------ 3 files changed, 79 insertions(+), 16 deletions(-) create mode 100644 drivers/net/phy/phy-internal.h diff --git a/drivers/net/phy/phy-internal.h b/drivers/net/phy/phy-internal.h new file mode 100644 index 000000000000..56d87495e1f0 --- /dev/null +++ b/drivers/net/phy/phy-internal.h @@ -0,0 +1,22 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#ifndef __PHY_INTERNAL_H +#define __PHY_INTERNAL_H + +#include +#include + +static inline int __phy_get_rate_matching(struct phy_device *phydev, + phy_interface_t iface) +{ + int ret = RATE_MATCH_NONE; + + lockdep_assert_held(&phydev->lock); + + if (phydev->drv->get_rate_matching) + return phydev->drv->get_rate_matching(phydev, iface); + + return ret; +} + +#endif /* __PHY_INTERNAL_H */ diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c index fce9bc7be330..e305bbff16ad 100644 --- a/drivers/net/phy/phy.c +++ b/drivers/net/phy/phy.c @@ -38,6 +38,7 @@ #include "phylib-internal.h" #include "phy-caps.h" +#include "phy-internal.h" #define PHY_STATE_TIME HZ @@ -146,13 +147,11 @@ EXPORT_SYMBOL(phy_print_status); int phy_get_rate_matching(struct phy_device *phydev, phy_interface_t iface) { - int ret = RATE_MATCH_NONE; + int ret; - if (phydev->drv->get_rate_matching) { - mutex_lock(&phydev->lock); - ret = phydev->drv->get_rate_matching(phydev, iface); - mutex_unlock(&phydev->lock); - } + mutex_lock(&phydev->lock); + ret = __phy_get_rate_matching(phydev, iface); + mutex_unlock(&phydev->lock); return ret; } diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c index 091c3a79c120..88280866c19a 100644 --- a/drivers/net/phy/phylink.c +++ b/drivers/net/phy/phylink.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -24,6 +25,7 @@ #include #include "phy-caps.h" +#include "phy-internal.h" #include "sfp.h" #include "swphy.h" @@ -550,6 +552,8 @@ static int phylink_validate_mac_and_pcs(struct phylink *pl, unsigned long capabilities; int ret; + lockdep_assert_held(&pl->state_mutex); + /* Get the PCS for this interface mode */ if (pl->mac_ops->mac_select_pcs) { pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface); @@ -628,13 +632,15 @@ static void phylink_validate_one(struct phylink *pl, struct phy_device *phy, __ETHTOOL_DECLARE_LINK_MODE_MASK(tmp_supported); struct phylink_link_state tmp_state; + lockdep_assert_held(&pl->state_mutex); + linkmode_copy(tmp_supported, supported); tmp_state = *state; tmp_state.interface = interface; if (phy) - tmp_state.rate_matching = phy_get_rate_matching(phy, interface); + tmp_state.rate_matching = __phy_get_rate_matching(phy, interface); if (!phylink_validate_mac_and_pcs(pl, tmp_supported, &tmp_state)) { phylink_dbg(pl, " interface %u (%s) rate match %s supports %*pbl\n", @@ -648,15 +654,17 @@ static void phylink_validate_one(struct phylink *pl, struct phy_device *phy, } } -static int phylink_validate_mask(struct phylink *pl, struct phy_device *phy, - unsigned long *supported, - struct phylink_link_state *state, - const unsigned long *interfaces) +static int __phylink_validate_mask(struct phylink *pl, struct phy_device *phy, + unsigned long *supported, + struct phylink_link_state *state, + const unsigned long *interfaces) { __ETHTOOL_DECLARE_LINK_MODE_MASK(all_adv) = { 0, }; __ETHTOOL_DECLARE_LINK_MODE_MASK(all_s) = { 0, }; int interface; + lockdep_assert_held(&pl->state_mutex); + for_each_set_bit(interface, interfaces, PHY_INTERFACE_MODE_MAX) phylink_validate_one(pl, phy, supported, state, interface, all_s, all_adv); @@ -667,14 +675,36 @@ static int phylink_validate_mask(struct phylink *pl, struct phy_device *phy, return phylink_is_empty_linkmode(supported) ? -EINVAL : 0; } -static int phylink_validate(struct phylink *pl, unsigned long *supported, - struct phylink_link_state *state) +static int phylink_validate_mask(struct phylink *pl, struct phy_device *phy, + unsigned long *supported, + struct phylink_link_state *state, + const unsigned long *interfaces) +{ + int ret; + + if (phy) + mutex_lock(&phy->lock); + + mutex_lock(&pl->state_mutex); + ret = __phylink_validate_mask(pl, phy, supported, state, interfaces); + mutex_unlock(&pl->state_mutex); + + if (phy) + mutex_unlock(&phy->lock); + + return ret; +} + +static int __phylink_validate(struct phylink *pl, unsigned long *supported, + struct phylink_link_state *state) { const unsigned long *interfaces = pl->supported_interfaces; + lockdep_assert_held(&pl->state_mutex); + if (state->interface == PHY_INTERFACE_MODE_NA) - return phylink_validate_mask(pl, NULL, supported, state, - interfaces); + return __phylink_validate_mask(pl, NULL, supported, state, + interfaces); if (!test_bit(state->interface, interfaces)) return -EINVAL; @@ -682,6 +712,18 @@ static int phylink_validate(struct phylink *pl, unsigned long *supported, return phylink_validate_mac_and_pcs(pl, supported, state); } +static int phylink_validate(struct phylink *pl, unsigned long *supported, + struct phylink_link_state *state) +{ + int ret; + + mutex_lock(&pl->state_mutex); + ret = __phylink_validate(pl, supported, state); + mutex_unlock(&pl->state_mutex); + + return ret; +} + static void phylink_fill_fixedlink_supported(unsigned long *supported) { linkmode_set_bit(ETHTOOL_LINK_MODE_Pause_BIT, supported); @@ -2117,7 +2159,7 @@ void phylink_update_mac_pause_capabilities(struct phylink *pl, unsigned long mac else if (pl->sfp_bus) linkmode_and(pl->supported, pl->supported, pl->sfp_support); - phylink_validate(pl, pl->supported, config); + __phylink_validate(pl, pl->supported, config); pause_state = config->pause; -- 2.55.0