From patchwork Sat Apr 3 14:59:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qingfang Deng X-Patchwork-Id: 1461993 X-Patchwork-Delegate: gch981213@gmail.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.openwrt.org (client-ip=2001:8b0:10b:1:d65d:64ff:fe57:4e05; helo=desiato.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=desiato.20200630 header.b=aoX4QNA/; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20161025 header.b=MUzIRHnn; dkim-atps=neutral Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4FCKqs4zgbz9sRR for ; Sun, 4 Apr 2021 02:01:13 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:To:From: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References: List-Owner; bh=0PQkbPoYVCyAqgu/hlkb6fADlxQ6dgu7UluAYBu+zpQ=; b=aoX4QNA/DPNm5D kZd+wyVBg83JpqcsM5JKsOMRbglT5IbMAB/aqUPHkgV5fw0uXvb/rcZM80JP01KiaVFzvqbFq0ltw H61JbfUFW402fMFcZhO7o1Bhd04MNI1JvsnPqu/lNsxNpUU2wMdsIGjYR32x6RXmh5O6VYkdct2oQ rR5g7c1ajdpEsY5CrrKzkH+SuSU20SZbFsG0ye+6o21aWO2b5lK1COz1SRoi/ol0AUKDM2evJXNwO e8rgmT56/ND5rhx5W10C1fE1pHvx0twqZbCaRcR74asbkV23VNhN/alfZGckw3+Bhcx8Ht5li12iR DdVxRDXIW2/Am+kBqDDA==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lShkK-00Ekj5-Nc; Sat, 03 Apr 2021 14:59:32 +0000 Received: from mail-pg1-x534.google.com ([2607:f8b0:4864:20::534]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lShkA-00Eki4-Ne for openwrt-devel@lists.openwrt.org; Sat, 03 Apr 2021 14:59:25 +0000 Received: by mail-pg1-x534.google.com with SMTP id p12so1302654pgj.10 for ; Sat, 03 Apr 2021 07:59:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=U9XNwF32pxa2eN/vwMolduYybOpeQcmMEmJza5FVQHA=; b=MUzIRHnnyXtqkHv5rJSKd4+dObU4aIiG8MJqJLRQ5VOZJqsZ4hcmz5c/gZO5TvlnRK xHib2Oz7FxIYor7qV3LUtBvyNEU1g3dSabQqw6AppRE41G/2ADm12uWKPEqxn9K6PtSG jaSiafckBK6xl65zc4pkea1rNsMrakhr0SlO09WGe7lzNU9cyl/Nxl8DraTujgfHV5An IGAxbQzQbJ2NdoptNl5l1AYGhrvyGZjGQF8QEUpr0ZZCPV8EtUrV8W/RlnAtCGbnhIIR cywjrpdk/c7ihKTKEdLG2h0lwLXPQpkfGizg1ytLxVzzw46H0VLn8CqZfrc/YSgWxxNH AZ+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=U9XNwF32pxa2eN/vwMolduYybOpeQcmMEmJza5FVQHA=; b=cIUloWfuUL/7f0GwEvSS2IFaUZt1uUBz4TB3oJ2vuIix1TGjRa5z8GBu1hscPW5KFm doF33QwijPaeHMgZce+/WxjfobJntmjM7ahQQCwDNgyidROK61Mx0mx+YQbWEmFZ7GVC SRhyWTNiarvfQFVKZeuVTaxM6KudXztQaY770UcRPmm7kCDUdsFaOPUSLrTKITKJkqqG lgfYMfL6p2zOigV8h8M0bSLOw9EOE9ItLRMUyvOuidtstPPhUv2WpZ7ZU3BsYkFKzgmB beoYGafRBHKuVMBtNf1UVg8j58NgsRyXQbvmUVQ01lsqaDZDKkWxbQJaacaRpfBOvLyx a6qA== X-Gm-Message-State: AOAM530ZRGIRlVtxchKinHd913A8ArF8xYjuq2VJu7dCSzy3irE3XbCl qAWQJ/gBe/a2GHrKMTc1ZHPukMVQwWAxkw== X-Google-Smtp-Source: ABdhPJzwrQDJXzCT2/WccQRJKzWGPp+wbC84J+1LMGH1h6TFaK1gcJQKSBW1xM68ar9PRy6LAlD0tg== X-Received: by 2002:a65:6559:: with SMTP id a25mr16113405pgw.106.1617461960398; Sat, 03 Apr 2021 07:59:20 -0700 (PDT) Received: from localhost.localdomain ([138.197.212.246]) by smtp.gmail.com with ESMTPSA id i21sm995510pjv.22.2021.04.03.07.59.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 03 Apr 2021 07:59:19 -0700 (PDT) From: DENG Qingfang To: openwrt-devel@lists.openwrt.org Subject: [PATCH] ramips: mt7530 swconfig: fix race condition in register access Date: Sat, 3 Apr 2021 22:59:15 +0800 Message-Id: <20210403145915.385580-1-dqfext@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210403_155922_932709_19179471 X-CRM114-Status: GOOD ( 10.10 ) X-Spam-Score: -0.2 (/) X-Spam-Report: Spam detection software, running on the system "desiato.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: The mt7530_{r, w}32 operation over MDIO uses 3 mdiobus operations and does not hold a lock, which causes a race condition when multiple threads try to access a register, they may get unexpected results [...] Content analysis details: (-0.2 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:534 listed in] [list.dnswl.org] 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [dqfext[at]gmail.com] -0.0 SPF_PASS SPF: sender matches SPF record -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org The mt7530_{r,w}32 operation over MDIO uses 3 mdiobus operations and does not hold a lock, which causes a race condition when multiple threads try to access a register, they may get unexpected results. To avoid this, handle the MDIO lock manually, and use the unlocked __mdiobus_{read,write} in the critical section. This fixes the "Ghost VLAN" artifact[1] in MT7530/7621 when the VLAN operation and the swconfig LED link status poll race between each other. [1] https://forum.openwrt.org/t/mysterious-vlan-ids-on-mt7621-device/64495 Signed-off-by: DENG Qingfang --- Please backport this to 21.02/19.07, thanks! .../files/drivers/net/ethernet/ralink/mt7530.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/target/linux/ramips/files/drivers/net/ethernet/ralink/mt7530.c b/target/linux/ramips/files/drivers/net/ethernet/ralink/mt7530.c index be1b8a6d8e..b4632d3990 100644 --- a/target/linux/ramips/files/drivers/net/ethernet/ralink/mt7530.c +++ b/target/linux/ramips/files/drivers/net/ethernet/ralink/mt7530.c @@ -290,9 +290,11 @@ mt7530_r32(struct mt7530_priv *priv, u32 reg) if (priv->bus) { u16 high, low; - mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff); - low = mdiobus_read(priv->bus, 0x1f, (reg >> 2) & 0xf); - high = mdiobus_read(priv->bus, 0x1f, 0x10); + mutex_lock(&priv->bus->mdio_lock); + __mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff); + low = __mdiobus_read(priv->bus, 0x1f, (reg >> 2) & 0xf); + high = __mdiobus_read(priv->bus, 0x1f, 0x10); + mutex_unlock(&priv->bus->mdio_lock); return (high << 16) | (low & 0xffff); } @@ -307,9 +309,11 @@ static void mt7530_w32(struct mt7530_priv *priv, u32 reg, u32 val) { if (priv->bus) { - mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff); - mdiobus_write(priv->bus, 0x1f, (reg >> 2) & 0xf, val & 0xffff); - mdiobus_write(priv->bus, 0x1f, 0x10, val >> 16); + mutex_lock(&priv->bus->mdio_lock); + __mdiobus_write(priv->bus, 0x1f, 0x1f, (reg >> 6) & 0x3ff); + __mdiobus_write(priv->bus, 0x1f, (reg >> 2) & 0xf, val & 0xffff); + __mdiobus_write(priv->bus, 0x1f, 0x10, val >> 16); + mutex_unlock(&priv->bus->mdio_lock); return; }