Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp1308316rdf; Sat, 4 Nov 2023 15:18:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHFGoS+Oga0bKuxm0V5PutXDUFDHUFp2xP5LjRZAXBnRP+AqHuNRrpPhpteu4T5kPIq2hmt X-Received: by 2002:a05:6870:a249:b0:1e9:9bad:8989 with SMTP id g9-20020a056870a24900b001e99bad8989mr29825873oai.25.1699136294168; Sat, 04 Nov 2023 15:18:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1699136294; cv=none; d=google.com; s=arc-20160816; b=TNaTHLkeGYa1dFYMzV4c3u+XRGbZL3bgkpDxon7++hvomnz5Pyf66Ca+bwKtoScSpy 6/+dHS+i3YiWsqUDWK22T3LzrILjZgU2qZ+kHdAxjtKa9jdzLQojZIAKAJQ9YIMNTrN1 S7fdZ7EKk35krhjpHX62dUd3pQ9b6G62DfM6BjedhR5Ql8uLJHQ5x2X3SxELDKxhHfp2 gOt2abHyAlbFi77HrA5cczGTqb04L+hlmJvl3tFIcieR4w2oTcrcwhOt5n2HTlUz45ak 0aevbVmqsUlxAXS/QF4yYykUpMgluPfFyBfdeHpC0c+VNoeX2iVdH+WCz1pp13N3Eb4P XQvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature:dkim-signature; bh=At+tJFFiyF2DkxJG9q1c0HZvQCm06K2RkCZSbLHv040=; fh=HdeQp8alQCrKsNyOQI+lAAGGPvHwOY/Ys++T1IQuW5M=; b=dOBZzhp2zxDN5Aw5jMvYkdMiqghgZyKV3Q7A7FOCfisiqSwQ4jiQb/4DIcfnFOzc2O nAWuGMMdymWE7g5Wr7OYCtCgcZdmVKSjR4H6SJKKMtjRQineIFRIOgJfQ9SNJcTTiaNo Pd2G9NtiFQMtu5aiwY4R/8bOVCB1UaB2/a1X2n2uPNnuRoeTFXm+irkAYyU0HuhRY/nN Cz4PVleLX2Qs3T0pudO/Qnh+Ey7o/WSPqim3Bt4q1oKvX5oZRR4iMHfPt9AdGeFKleAw alH0CV3hotVNLNLMnikSBsmxL2+Vqa91cCU6SF2jlmGeJLM1VnAos5lCTa6mXSACZ9GN KxYA== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=sx3kdbRM; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=qJ+A+j9F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alu.unizg.hr Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id k68-20020a633d47000000b005b8afa325b8si4558836pga.410.2023.11.04.15.18.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Nov 2023 15:18:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=sx3kdbRM; dkim=fail header.i=@alu.unizg.hr header.s=mail header.b=qJ+A+j9F; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alu.unizg.hr Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 683B78065013; Sat, 4 Nov 2023 15:17:22 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229749AbjKDWQl (ORCPT + 99 others); Sat, 4 Nov 2023 18:16:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48342 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229577AbjKDWQk (ORCPT ); Sat, 4 Nov 2023 18:16:40 -0400 Received: from domac.alu.hr (domac.alu.unizg.hr [161.53.235.3]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F0B3ED6B; Sat, 4 Nov 2023 15:16:35 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by domac.alu.hr (Postfix) with ESMTP id 8334960171; Sat, 4 Nov 2023 23:16:32 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1699136192; bh=Hf4CXzUfv9fx8mOpSveNe9niH2S13EeNEu1vdpWzmCo=; h=From:To:Cc:Subject:Date:From; b=sx3kdbRMPgDtFlX2/Ou+TXxRm8K5TEOBroZO+qgEoDn6RBWXD0ZxZ4TYyixpB8vNT XLHh4WNPAj7a9gHQxbIAUfkSqWJQn05AmM8w+BXyqe9nqeXIJb2TMvO6yBkdbhZXUn uirVcqu8+6Vwj4U6feoSxc+9pAM+08/9VfhwoL54l0s9bJTiukQwpW3E9DN62hKaPp Ua3ItDu0KyNLPAgeV1yuZzxcwXSpWO1/ZIgDCqC9v3zfbm3Q3Cbu1KLPdilHvsKLx3 unCiGVTF3Rrm0UYdnV8Rqd5V/S6O7O+FZTB232ztAd0aGbgDIc8kL98+IwHeTnWRuT ljto1dpX8c8XA== X-Virus-Scanned: Debian amavisd-new at domac.alu.hr Received: from domac.alu.hr ([127.0.0.1]) by localhost (domac.alu.hr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id eKzzxBjA1i8i; Sat, 4 Nov 2023 23:16:29 +0100 (CET) Received: from defiant.home (78-2-200-71.adsl.net.t-com.hr [78.2.200.71]) by domac.alu.hr (Postfix) with ESMTPSA id 12E076016E; Sat, 4 Nov 2023 23:16:29 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=alu.unizg.hr; s=mail; t=1699136189; bh=Hf4CXzUfv9fx8mOpSveNe9niH2S13EeNEu1vdpWzmCo=; h=From:To:Cc:Subject:Date:From; b=qJ+A+j9FxjiPw0m4CqrRVByS1TH3g9S0ucRu/Rj5rOENtz7M7JKfiASP+xiWxeFpX JZaC8Iq1Ko5YtLdJjyCEljsaA2+jRrxoIqicUvq2yd472uoP1ZBoFDz9lu47jZ24au ztmLfr4arGi2G8z7Se6BdUyNdlnnZphKuzdfEM1n6zWZlhCbr4jCEy4RGokLiuq9LT J2CxlMNhqa1BXoO3skC0/V/CeK6kjhZTtXJbjEGqJjNM8qKmRUn1qZ+zOZFSQJKB5g o6ykZrrwfZs4v1VZlvwEVOEkGKMp0CjxJeE4WkOFd5AtWl+7mcunxOhcTKX0vwkvW4 DRNOCjByY929w== From: Mirsad Goran Todorovac To: linux-kernel@vger.kernel.org Cc: "David S. Miller" , Eric Dumazet , Heiner Kallweit , Jakub Kicinski , Paolo Abeni , netdev@vger.kernel.org, nic_swsd@realtek.com, Mirsad Goran Todorovac Subject: [PATCH net-next v6 0/5] Coalesce mac ocp write/modify calls to reduce spinlock contention Date: Sat, 4 Nov 2023 23:15:11 +0100 Message-Id: <20231104221514.45821-1-mirsad.todorovac@alu.unizg.hr> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Sat, 04 Nov 2023 15:17:22 -0700 (PDT) The motivation for these helpers was the locking overhead of 130 consecutive r8168_mac_ocp_write() calls in the RTL8411b reset after the NIC gets confused if the PHY is powered-down. To quote Heiner: On RTL8411b the RX unit gets confused if the PHY is powered-down. This was reported in [0] and confirmed by Realtek. Realtek provided a sequence to fix the RX unit after PHY wakeup. A series of about 130 r8168_mac_ocp_write() calls is performed to program the RTL registers for recovery, each doing an expensive spin_lock_irqsave() and spin_unlock_irqrestore(). Each mac ocp write is made of: static void __r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data) { if (rtl_ocp_reg_failure(reg)) return; RTL_W32(tp, OCPDR, OCPAR_FLAG | (reg << 15) | data); } static void r8168_mac_ocp_write(struct rtl8169_private *tp, u32 reg, u32 data) { unsigned long flags; raw_spin_lock_irqsave(&tp->mac_ocp_lock, flags); __r8168_mac_ocp_write(tp, reg, data); raw_spin_unlock_irqrestore(&tp->mac_ocp_lock, flags); } Register programming is done through RTL_W32() macro which expands into #define RTL_W32(tp, reg, val32) writel((val32), tp->mmio_addr + (reg)) which is further (on Alpha): extern inline void writel(u32 b, volatile void __iomem *addr) { mb(); __raw_writel(b, addr); } or on i386/x86_64: #define build_mmio_write(name, size, type, reg, barrier) \ static inline void name(type val, volatile void __iomem *addr) \ { asm volatile("mov" size " %0,%1": :reg (val), \ "m" (*(volatile type __force *)addr) barrier); } build_mmio_write(writel, "l", unsigned int, "r", :"memory") This obviously involves iat least a compiler barrier. mb() expands into something like this i.e. on x86_64: #define mb() asm volatile("lock; addl $0,0(%%esp)" ::: "memory") This means a whole lot of memory bus stalls: for spin_lock_irqsave(), memory barrier, writel(), and spin_unlock_irqrestore(). With about 130 of these sequential calls to r8168_mac_ocp_write() this looks like a lock storm that will stall all of the cores and CPUs on the same memory controller for certain time I/O takes to finish. In a sequential case of RTL register programming, the writes to RTL registers can be coalesced under a same raw spinlock. This can dramatically decrease the number of bus stalls in a multicore or multi-CPU system. Macro helpers r8168_mac_ocp_write_seq() and r8168_mac_ocp_modify_seq() are provided to reduce lock contention: static void rtl_hw_start_8411_2(struct rtl8169_private *tp) { ... /* The following Realtek-provided magic fixes an issue with the RX unit * getting confused after the PHY having been powered-down. */ static const struct recover_8411b_info init_zero_seq[] = { { 0xFC28, 0x0000 }, { 0xFC2A, 0x0000 }, { 0xFC2C, 0x0000 }, ... }; ... r8168_mac_ocp_write_seq(tp, init_zero_seq); ... } The hex data is preserved intact through s/r8168_mac_ocp_write[(]tp,/{ / and s/[)];/ },/ functions that only changed the function names and the ending of the line, so the actual hex data is unchanged. To repeat, the reason for the introduction of the original commit was to enable recovery of the RX unit on the RTL8411b which was confused by the powered-down PHY. This sequence of r8168_mac_ocp_write() calls amplifies the problem into a series of about 500+ memory bus locks, most waiting for the main memory read, modify and write under a LOCK. The memory barrier in RTL_W32 should suffice for the programming sequence to reach RTL NIC registers. [0] https://bugzilla.redhat.com/show_bug.cgi?id=1692075 v6: proceeded according to Jacob Keller's suggestions by creating a cover page and reducing the text within the commits. Applying to the net-next tree as Heiner Kallweit requested. v5: attempted some new optimisations, which were rejected, but not all and not completely. v4: fixed complaints as advised by Heiner and checkpatch.pl. split the patch into five sections to be more easily manipulated and reviewed introduced r8168_mac_ocp_write_seq() applied coalescing of mac ocp writes/modifies for 8168H, 8125 and 8125B v3: removed register/mask pair array sentinels, so using ARRAY_SIZE(). avoided duplication of RTL_W32() call code as advised by Heiner. Mirsad Goran Todorovac (5): r8169: Coalesce r8169_mac_ocp_write/modify calls to reduce spinlock stalls r8169: Coalesce RTL8411b PHY power-down recovery calls to reduce spinlock stalls r8169: Coalesce mac ocp write and modify for 8168H start to reduce spinlocks r8169: Coalesce mac ocp commands for 8125 and 8125B start to reduce spinlock contention r8169: Coalesce mac ocp commands for rtl_hw_init_8125 to reduce spinlocks drivers/net/ethernet/realtek/r8169_main.c | 304 +++++++++++----------- 1 file changed, 150 insertions(+), 154 deletions(-) -- 2.34.1